尝试扩展UITableView单元时断言失败

时间:2014-09-03 13:03:11

标签: ios objective-c uitableview assertion

我想在点击时展开UITableViewCell。

之前我在应用程序中完成了这个操作,使用了我现在使用的相同步骤,但我想我正在监督某些事情,而我无法弄清楚它是什么。

我想显示一些标题,存储在一个可变数组中

NSMutableArray* topicsArray;
_topicsArray = [[NSMutableArray alloc]init];
[_topicsArray addObject:@"Percent"];
[_topicsArray addObject:@"Decimal numbers and fractions"];
[_topicsArray addObject:@"Variables and expressions"];
[_topicsArray addObject:@"Geometry - Area and perimeter"];
[_topicsArray addObject:@"Mathematics in use"];
[_topicsArray addObject:@"Reduction of expression"];
[_topicsArray addObject:@"Chance and probability"];
[_topicsArray addObject:@"Academic reading - Find century"];

这是我在点击时用于扩展/缩小单元格的代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([self.topicsArray containsObject:indexPath])
{
    [self.topicsArray removeObject:indexPath];
}
else
{
    [self.topicsArray addObject:indexPath];

}
[_tableView beginUpdates];
[_tableView endUpdates];

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat kExpandedCellHeight = 210;
CGFloat kNormalCellHeigh = 80;

if ([self.topicsArray containsObject:indexPath])
{
    return kExpandedCellHeight;
}
else
{
    return kNormalCellHeigh; 
}
}

当我制作自定义单元格时,我选择了“清除图形内容”和“剪辑子视图”。

单击单元格时发生错误。 发生山雀的代码行是

[_tableView endUpdates];

in - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath方法。

另外,我收到的消息是:

  

由于未捕获的异常而终止应用   'NSInternalInconsistencyException',原因:'无效更新:无效   第0节中的行数。包含在中的行数   更新后的现有部分(9)必须等于数量   更新前的该部分中包含的行(8),加号或减号   从该部分插入或删除的行数(插入0,   删除0)加上或减去移入或移出的行数   该部分(0移入,0移出)。'

可能是问题的根源是什么?

1 个答案:

答案 0 :(得分:0)

我实际上已经弄明白了如何解决这个问题。

将所有对象添加到数组后,您将从中填充tableView中的数据,将该数组计数保存为常量。

@property int counter;

_counter = _array.count;

然后,在numberOfRowsInSection中,传递该计数器:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _counter;
}

瞧,问题解决了。