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

半腔热情 提交于 2020-01-17 02:21:34

问题


I want to insert some custom cells in my 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'

Any idea why? Thanks for your help.


回答1:


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.




回答2:


insertRowsAtIndexPaths takes as its parameter an array of NSIndexPath objects which represent where in the table the new rows should be. 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...




回答3:


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:

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];


来源:https://stackoverflow.com/questions/17014495/insertrowsatindexpaths-of-custom-uitableviewcells-ends-in-unrecognized-selector

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!