开发者

Required steps to Add a Row in a tableView

开发者 https://www.devze.com 2023-02-20 16:50 出处:网络
I have ViewController with TableView embeded in. When this ViwController is launched i fire a NSURLConnection and grab 10 first results with the query from the server, this results include simply an i

I have ViewController with TableView embeded in. When this ViwController is launched i fire a NSURLConnection and grab 10 first results with the query from the server, this results include simply an image url and a text. I store everything in a mutabelArray and load the image then display all this results in my tableView and add the "load 10 more cell" cell.

Well the problem is when i try to load 10 more, the error " The number of rows contained in an existing section after the update (12) must be equal to the number of rows contained in that section before the update" appears

[theTableView insertRowsAtIndexPaths:indexes withRowAnimation:UITableViewRowAnimationRight];

i read that i have to remove cells first before adding others from the DataSource, but if i remove the first 10 c开发者_如何学Cells from my array i have to go and get them back from the server when the user wants to scrol up to the previous results... it's really heavy and a little stupide -_-'... i'm sure there is a way to add simply new rows to the table view without loosing previous data.

Please if someone know a good and simple way or have sample code, it will really help, i'm stuck on it since 4 hours :/ thx


Well, I had once an equal problem and solved it as following:

1) Use a NSMutableArray or NSMutableDictionary as Datasource for your tableView instead of NSArray or NSDictionary

// old
// NSArray *dataArray = [[NSArray alloc] initWithObjects:@"Object1",...,nil];

// new
NSMutableArray *dataArray = [[NSMutableArray alloc] initWithObjects:@"Object1",...,nil];

2) If you catch up the the ten new results just add them to the dataArray

// Possibility 1:
for (Result *r in resultSet)
{
    [dataArray addObject:r]
}

// Possibility 2:
[dataArray addObjectsFromArray:resultSet];

3) Now reload your table:

NSMutableIndexSet *indexes = [NSMutableIndexSet indexSet];
[indexes addIndex:0];
[indexes addIndex:1];
[self.myTable reloadSections:indexes withRowAnimation:UITableViewRowAnimationRight];
0

精彩评论

暂无评论...
验证码 换一张
取 消