Q:Get 按 tableView 的 indexPath 计算的行数出现“尝试将堆栈放入不可读的内存中:”错误

Q:Get rows count by indexPath of tableView comes `Trying to put the stack in unreadable memory at:` error

在我的 tableView 委托方法中:

我在 tableView delegate 方法中得到 numberOfRowsnumberOfSections

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath  ,

但是在- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section方法中,我试过这个方法:

NSString *key = [NSString stringWithFormat:@"%ld", section];

这一行显示error:Thread 1:EXC_BAD_ACCESS(code=2,address=0x) 我拿了 (lldb):

(lldb) po [NSString stringWithFormat:@"%ld", section]
error: Trying to put the stack in unreadable memory at: 0x7fff50739eb0.

如果我在 tableView delegate 方法中没有得到 numberOfRowsnumberOfSections: 错误不会出现。

下面是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    NSInteger numberOfRows = [tableView numberOfRowsInSection:[indexPath section]];

    NSInteger numberOfSections = [tableView numberOfSections];

    if (indexPath.row == 0 && numberOfRows != 1) {

        cell.textLabel.text = [_arr objectAtIndex:indexPath.row];
    }



    return cell;
}

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

        NSInteger numberOfRows = [tableView numberOfRowsInSection:[indexPath section]];

        NSInteger numberOfSections = [tableView numberOfSections];

        if (indexPath.row == 0 && numberOfRows != 1) {

             return 40;
        }else {
            return 5;
        }

    }

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    //NSString *key = [NSString stringWithFormat:@"%d", (int)section];

    NSString *key = [NSString stringWithFormat:@"%ld", section];  // this line shows the error.


    BOOL folded = [[_foldInfoDic objectForKey:key] boolValue];

    if (section == 0) {
        return folded?_arr.count:1;
    } else if (section == 1) {
        return folded?_arr.count:1;
    } else if (section == 2) {
        return folded?_arr.count:1;
    } else {
        return folded?_arr.count:0;
    }
}

这不是由以下代码引起的错误:

NSString *key = [NSString stringWithFormat:@"%ld", section];

你的代码是运行死循环,被系统结束。看下面:

当你打电话时:

 NSInteger numberOfRows = [tableView numberOfRowsInSection:[indexPath section]];
 NSInteger numberOfSections = [tableView numberOfSections];

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath中,这将导致无限循环。

例如(调用其中任何一个都会导致死循环):

- (void)func1 {
    // Code
    [self func2];
}
- (void)func2 {
    // Code
    [self func1];
}

最简单的解决方案是实现您的代码如下:

   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    NSInteger numberOfRows = [tableView numberOfRowsInSection:[indexPath section]];

    NSInteger numberOfSections = [self numberOfSectionsInTableView:tableView];

    if (indexPath.row == 0 && numberOfRows != 1) {

        cell.textLabel.text = [_arr objectAtIndex:indexPath.row];
    }



    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSInteger numberOfRows = [self tableView:self numberOfRowsInSection:[indexPath section]];

    NSInteger numberOfSections = [self numberOfSectionsInTableView:tableView];

    if (indexPath.row == 0 && numberOfRows != 1) {

        return 40;
    }else {
        return 5;
    }

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    //NSString *key = [NSString stringWithFormat:@"%d", (int)section];

    NSString *key = [NSString stringWithFormat:@"%ld", section];  // this line shows the error.


    BOOL folded = [[_foldInfoDic objectForKey:key] boolValue];

    if (section == 0) {
        return folded?_arr.count:1;
    } else if (section == 1) {
        return folded?_arr.count:1;
    } else if (section == 2) {
        return folded?_arr.count:1;
    } else {
        return folded?_arr.count:0;
    }
}