【发布时间】:2014-12-25 22:33:24
【问题描述】:
我知道有一个名为[tableView cellForRowAtIndexPath:indexPath]; 的方法,我们可以使用它来获取给定 indexPath 处的单元格。但是我想在行点击时扩展行。使用此代码-
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Deselect cell
[tableView deselectRowAtIndexPath:indexPath animated:TRUE];
// Toggle 'selected' state
BOOL isSelected = ![self cellIsSelected:indexPath];
// Store cell 'selected' state keyed on indexPath
NSNumber *selectedIndex = [NSNumber numberWithBool:isSelected];
int sec = indexPath.section;
int row = indexPath.row;
NSString* indexpathStr = [NSString stringWithFormat:@"%d-%d", sec, row];
[selectedIndexes setObject:selectedIndex forKey:indexpathStr];
// This is where magic happens...
[tableView beginUpdates];
[tableView endUpdates];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat height = 0.0;
if([self cellIsSelected:indexPath]) {
//problem here
CalEventCell* cell = (CalEventCell*)[tableView cellForRowAtIndexPath:indexPath];
[self setDescriptionFrame:cell.descriptionLbl];
height = cell.descriptionLbl.frame.size.height+cell.eventLbl.frame.size.height +20;
}
else
height = 60;
return height;
}
当点击一行时,我在 tableview 上调用 beginUpdates 和 endUpdates 方法,并在 heightForAtIndexPath 方法中更改行高。问题是,如您所见,我从 heightForAtIndexPath 调用 cellForAtIndexPath,而在 cellForRow 中有 @ 987654327@ 方法调用变成了对 heightForRow 的调用,这个循环永远不会结束,程序会因EXC-BAD-EXCEP 而崩溃。
那么从 indexPath 获取单元格的任何其他方法或任何其他方式呢?任何帮助将不胜感激。
【问题讨论】:
-
从 didSelectRow 重新加载表并从那里获取 indexpath 并尝试仅为特定索引设置不同的高度。
标签: ios objective-c uitableview