【发布时间】:2015-03-31 14:39:50
【问题描述】:
我正在尝试创建一个UITableViewCell,当滚动离开时,它会折叠成预览。
我设法做到了,使用布尔值 - 即 - 在 cellForRowAtIndexPath 中,我更改了当前单元格之前所有单元格的布尔数组。
在 iOS8 中它可以完美运行 - 问题是在 iOS7 中 heightForRowAtIndexPath 仅在 tableView 加载时被调用。
我知道我能做到:
[tableView beginUpdates];
[tableView endUpdates];
但我无法在 cellForRowAtIndexPath 中执行此操作(它崩溃了)
我可以在哪里运行它?
代码:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([[collapsedChapters objectAtIndex:indexPath.row] boolValue] == YES)
{
return 73;
}
//else - calculate
return 200; //the number is actually calculated - but simplifying
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell =[self generateChapterCell:tableView indexPath:indexPath];
[self handleLastVisible];
return cell;
}
- (void)handleLastVisible
{
NSInteger lastVisibleChapter = [self getLastVisibleRowOfSection:self.tableView section:chaptersSectionNum];
if (lastVisibleChapter > lastViewedChapter) //should work for -1 and 9999 as well
{
lastViewedChapter = lastVisibleChapter;
}
NSInteger firstVisibleChapter = [self getFirstVisibleRowOfSection:self.tableView section:chaptersSectionNum];
if (firstVisibleChapter == -1)
{
//we are before - don't touch
return;
}
NSNumber* yesObj = [NSNumber numberWithBool:YES];
//in case of 9999 - we will stop at the end of the for loop - it's OK
for (int i=0; i < [collapsedChapters count]; i++)
{
if (i == firstVisibleChapter)
{
break;
}
if ([[collapsedChapters objectAtIndex:i] boolValue] == NO)
{
[chapterFirstCollapse replaceObjectAtIndex:i withObject:yesObj];
}
[collapsedChapters replaceObjectAtIndex:i withObject:yesObj];
}
}
【问题讨论】:
-
实际上每次显示单元格时都应该调用
heightForRowAtIndexPath。如果它在 iOS7 上不起作用,那你一定是做错了什么
标签: ios uitableview ios7