问:使用tableView的indexPath获取行数`试图将堆栈放在不可读的内存中:`error

时间:2016-07-28 02:05:07

标签: ios uitableview rows exc-bad-access sections

在我的tableView委托方法中:

我在numberOfRows numberOfSections方法中获得了tableViewdelegate

- (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];

在此行中,它显示错误:线程1:EXC_BAD_ACCESS(代码= 2,地址= 0x) 我拿走了(lldb)

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

如果我的numberOfRows numberOfSections方法无法获得tableViewdelegate:   错误不会出现。

以下是我的代码:

- (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;
    }
}

1 个答案:

答案 0 :(得分:3)

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

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

您的代码在无限循环中运行并由系统结束。看下面的内容: enter image description here

致电时:

 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;
    }
}