在我的tableView委托方法:问:通过tableView的indexPath获取行数`尝试将堆放置在不可读的内存中:`error。
我得到numberOfRows
和numberOfSections
在我tableView
delegate
方法:
- (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
在我的tableView
delegate
方法: 错误不会出现。下面
是我的代码:
- (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;
}
}
很好的回答,谢谢@ zylenv – aircraft