I am trying to refresh a UITableView with new cells every 30 seconds. My array contains 30 elements, and for every new 开发者_C百科item I add to my array, I want to remove the last cell in my table. How can I go about doing this?
To refresh every 30 seconds, use an NSTimer.
NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:30
target:self selector:@selector(refreshTable)
userInfo:nil repeats:YES];
Add that new item in your -refreshTable
. Make sure your dataSource and the table are in sync.
-(void)refreshTable {
...
// modify the data source.
[items removeLastObject];
[items insertObject:newItem atIndex:0];
// modify the table.
[tableView beginUpdates];
[tableView insertRowsAtIndexPaths:
[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]]
withRowAnimation:withRowAnimation:UITableViewRowAnimationRight];
[tableView deleteRowsAtIndexPaths:
[NSArray arrayWithObject:[NSIndexPath indexPathForRow:29 inSection:0]]
withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
...
}
See the Table View Programming Guide for iPhone OS on how to perform batch insertion and deletion.
精彩评论