2010-04-19 113 views
1

我想每30秒刷新一次带有新单元格的UITableView。我的数组包含30个元素,对于添加到数组中的每个新项目,我想要移除表格中的最后一个单元格。我怎么能这样做呢?自动刷新UITableView帮助

回答

6

要每30秒刷新一次,请使用NSTimer。

NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:30 
          target:self selector:@selector(refreshTable) 
         userInfo:nil repeats:YES]; 

将这个新项目添加到您的-refreshTable。确保你的dataSource和表是同步的。

-(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]; 
    ... 
} 

关于如何执行批量插入和删除,请参阅Table View Programming Guide for iPhone OS

+0

@Sheehan:如果您以后不使用它,请不要指定给'timer'变量。但是如果你想在某个时间杀死30秒的刷新,那么保持这个状态。 – kennytm 2010-04-22 20:25:14

+0

为什么你需要删除最后一个对象? – user1872384 2015-03-12 08:58:52