Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在iOS7中更新UITableViewCell高度_Ios_Uitableview_Ios7 - Fatal编程技术网

在iOS7中更新UITableViewCell高度

在iOS7中更新UITableViewCell高度,ios,uitableview,ios7,Ios,Uitableview,Ios7,我正在尝试创建一个UITableViewCell,当滚动到别处时,它会折叠成预览 我设法做到了这一点,在cellforrowatinexpath中使用布尔值更改了当前单元格之前所有单元格的布尔数组 在iOS8中,它工作得非常好-问题是在iOS7中,仅在tableView加载时调用heightForRowAtIndexPath 我知道我能做到: [tableView beginUpdates]; [tableView endUpdates]; 但我无法在CellForRowatineXpath中

我正在尝试创建一个
UITableViewCell
,当滚动到别处时,它会折叠成预览

我设法做到了这一点,在
cellforrowatinexpath
中使用布尔值更改了当前单元格之前所有单元格的布尔数组

在iOS8中,它工作得非常好-问题是在iOS7中,仅在tableView加载时调用heightForRowAtIndexPath

我知道我能做到:

[tableView beginUpdates];
[tableView endUpdates];
但我无法在
CellForRowatineXpath中执行此操作(它崩溃了)

我在哪里可以运行它

守则:

- (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];
    }
}
-(CGFloat)tableView:(UITableView*)tableView rowatinexpath的高度:(nsindepath*)indepath{
if([[collapsedChapters objectAtIndex:indexPath.row]布尔值]==YES)
{
返回73;
}
//否则-计算
return 200;//实际计算的是这个数字,但会简化
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
UITableViewCell*单元格=[self-GenerateCapterCell:tableView indexPath:indexPath];
[自携式可视];
返回单元;
}
-(无效)可手动显示
{
NSInteger lastVisibleChapter=[self-getLastVisibleRowOfSection:self.tableView section:chaptersSectionNum];
if(lastVisibleChapter>lastViewedChapter)//也适用于-1和9999
{
lastViewedChapter=lastVisibleChapter;
}
NSInteger firstVisibleChapter=[self getFirstVisibleRowOfSection:self.tableView section:chaptersSectionNum];
如果(第一次访问章节==-1)
{
//我们在前面-不要碰
返回;
}
NSNumber*yesObj=[NSNumber numberWithBool:YES];
//在9999的情况下-我们将在for循环结束时停止-没有问题
对于(int i=0;i<[collapsedChapters count];i++)
{
如果(i==firstVisibleChapter)
{
打破
}
if([[collapsedChapters objectAtIndex:i]布尔值]==否)
{
[ChapterFirstCollapseReplaceObjectAtIndex:i with Object:yesObj];
}
[collapsedChapters replaceObjectAtIndex:i with object:yesObj];
}
}

我注意到
heightforrowatinexpath:
调用
[[collapsedChapters objectAtIndex:indexPath.row]boolValue]==YES
。我假设这意味着布尔值是一个开关,表示某个特定单元格是否“展开”,对吗

如果是这样的话,几年前我也有同样的问题。不是让整个表只有一个部分,然后每个“章节”有一行,而是让每个“章节”有一个部分,然后根据“章节”是展开还是折叠更改每个部分的行数-1行表示折叠,2行表示展开。对于展开的“章节”,您将展开的信息放在章节的第二行


通过这种方式展开章节,您可以使用
insertRowsAtIndexPaths:
和折叠,
removowsatindexpaths:
,一切都会正常工作,包括用户期望的所有漂亮的表视图动画。

是的。。。但在我的例子中,单元格是不同的(它不仅仅是添加的信息),所以这个技巧不起作用……好吧——这是几年前的事了,但我确实记得当我尝试这样做时,感觉API真的在与我作对。你确定不能重新设计它,这样额外的信息就会从底部弹出吗?实际上,每次显示单元格时都应该调用
heightforrowatinexpath
。如果它在iOS7上不起作用,那么你一定是做错了什么