I am trying to refresh a UITableView with new cells every 30 seconds. My array contains 30 elements, and for every new item I add to my array, I want to remove the last cell in my table. How can I go about doing this?
+3
A:
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.
KennyTM
2010-04-19 19:33:25
@Sheehan: Don't assign to a `timer` variable if you're not using it later. But keep that in some ivar if you want to kill the 30-second refresh at some time.
KennyTM
2010-04-22 20:25:14