indexPath抛出EXC错误访问错误的行的高度

时间:2012-06-09 05:15:18

标签: ios objective-c uitableview heightforrowatindexpath

所以当我输入下面的if语句时,我收到一个EXC Bad Access错误。令人沮丧的是,我已经确认indexPath和tableView指向正确的位置。此外,完全相同的if语句在我的代码中的其他地方使用在同一个实现文件中,没有错误。

非常感谢帮助。

-(CGFloat)tableView:(UITableView *)tableView 
heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if([indexPath row] == ([tableView numberOfRowsInSection:[indexPath section]]-1))
    {
        return 44.0;
    } else {
        return 60.0;
    }                       
}

6 个答案:

答案 0 :(得分:0)

if([indexPath row] == ([tableView numberOfRowsInSection:[indexPath section]-1]))
    {
        return 44.0;
    } else {
        return 60.0;
    }            

答案 1 :(得分:0)

根据您的逻辑[indexPath section]-1,第一部分中第0个部分的行数应为-1

答案 2 :(得分:0)

对于索引为“0”的部分,

[indexPath section]-1会给您提供错误的访问权限。从此声明中删除-1。

答案 3 :(得分:0)

解决您的要求的更好的解决方案是不要询问tableview一节中有多少行,而是询问您的数据源有多少行。

因此,例如,如果您的数据源是一个数组,请检查array.count(当然,您有其他数据源,因为您有部分,但您明白了这一点)

答案 4 :(得分:0)

我知道这是一个老问题,但将来可能对其他人有帮助。

<强>解决方案:

Read-only

<强>解释

发生的事情是系统调用- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { NSInteger numOfRowsInSection = [tableView.dataSource tableView:tableView numberOfRowsInSection:indexPath.section]; if (indexPath.row == (numOfRowsInSection - 1)) return 44.0; else return 60.0; } 的每个[tableView numberOfRowsInSection:[indexPath section]]次调用再次创建无限循环,最终导致应用崩溃。

使用[tableView heightForRowAtIndexPath:indexPath]的{​​{1}}属性来阻止循环,如下所示:

dataSource

答案 5 :(得分:-1)

您正在使用tableView Delegate方法。试试这个:

 if([indexPath row] == ([self tableView:tableView numberOfRowsInSection:indexPath.section]-1))