简体   繁体   English

自定义UITableViewCells的insertRowsAtIndexPaths:以发送到实例的无法识别的选择器结尾

[英]insertRowsAtIndexPaths: of custom UITableViewCells ends in unrecognized selector sent to instance

I want to insert some custom cells in my UITableView . 我想在UITableView插入一些自定义单元格。

So what I'm doing is simply this: 所以我正在做的就是这样:

NSMutableArray *arrayOfCells = [[NSMutableArray alloc] init];
for(int i = 0 ; i < 4 ; i++)
{
    CustomCell *cell = [[CustomCell alloc] init:myText];
    [arrayOfCells addObject:cell];
}
[table insertRowsAtIndexPaths:arrayOfCells withRowAnimation:UITableViewRowAnimationTop];

But the inserting line returns this error: 但是插入行返回此错误:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CustomCell compare:]: unrecognized selector sent to instance 0x1cdebfb0' *由于未捕获的异常'NSInvalidArgumentException'而终止应用,原因:'-[[CustomCell compare:]:无法识别的选择器已发送到实例0x1cdebfb0'

Any idea why? 知道为什么吗? Thanks for your help. 谢谢你的帮助。

insertRowsAtIndexPaths takes as its parameter an array of NSIndexPath objects which represent where in the table the new rows should be. insertRowsAtIndexPaths将一个NSIndexPath对象数组作为其参数,该对象表示新行应在表中的位置。 The table view then calls the delegate and dataSource to get the new cells and content to display. 然后,表视图调用委托和数据源以获取要显示的新单元格和内容。

You can not directly pass the new cells you want to be added... 您无法直接传递您要添加的新单元格...

NSMutableArray *arrayOfIndexPaths = [[NSMutableArray alloc] init];

for(int i = 0 ; i < 4 ; i++)
{
    NSIndexPath *path = [NSIndexPath indexPathForRow:i inSection:0];
    [arrayOfIndexPaths addObject:path];
}

[table insertRowsAtIndexPaths:arrayOfIndexPaths withRowAnimation:UITableViewRowAnimationTop];

This is what you need to do. 这是您需要做的。

In short, you need to add the new cell to your array of cells, and insert them inside updates. 简而言之,您需要将新单元格添加到单元格数组中,并将其插入更新中。
create an array of indexPaths like this: 创建一个indexPath数组,如下所示:

NSMutableArray *indexPaths = [[NSMutableArray alloc]init];
for (int i = firstIndexPathValue; i < lastIndexPathValue; i++) [indexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];

then.. 然后..

[myTableView beginUpdates];
//here is where you need to add objects to your array, then..
[myTableView insertRowsAtIndexPaths:index withRowAnimation:chooseARowAnimation];
[myTableView endUpdates];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM