2010-04-19 112 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