[英]How to add dictionary inside of an array to UItableview using insertRowsAtIndexPaths?
如何使用insertRowsAtIndexPaths將getnewdata的結果添加到現有tableView中? 我找到了幾個例子,但無法弄清楚。
- (void)getfirstdata {
[...] //get JSON data
self.data = jsonResult; //a dictionary inside of an array
[self.tableView reloadData];
}
- (void)getnewdata {
[...] //get new JSON data which has to be added to top of the table
self.data = jsonnewResult; //a dictionary inside of an array
[self.tableView reloadData];
}
(UITableViewCell *) tableView: (UITableView *) table_view cellForRowAtIndexPath: (NSIndexPath *) index_path
static NSString *CellIdentifier = @"Cell";
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
id celldata = [self.data objectAtIndex:[index_path row]];
cell.textLabel.text = [celldata valueForKeyPath:@"user.name"];
cell.detailTextLabel.text = [celldata objectForKey:@"text"];
return cell;
}
對於新手,您想添加到數據中,因此...
- (void)getnewdata {
[...] //get new JSON data which has to be added to top of the table
[self.data addObjectsFromArray:jsonResult];
[self.tableView reloadData];
}
確保self.data是NSMutableArray。
編輯
在我看來,您可能希望將新數據放在最上面。
NSMutableArray *newModel = [NSMutableArray arrayWithArray:jsonResult];
[newModel addObjectsFromArray:self.data];
self.data = newModel;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.