2009-09-08 98 views

回答

1

設置一個計時器在你的UITableViewController您的viewDidLoad方法,像這樣:

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    // Schedule a timer to fire every 5 seconds and call our 
    // insertNewObject method 
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:5 
           target:self 
          selector:@selector(insertNewObject) 
          userInfo:nil 
          repeats:YES]; 
} 

然後插入新行到我們的數據源,並告訴的tableView它

- (void)insertRow { 
    // dataSource defined elsewhere 
    [dataSource addObject:@"New row"]; 

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:([dataSource count] - 1) inSection:0]; 
    NSArray *indexPaths = [NSArray arrayWithObject:indexPath]; 

    [[self tableView] beginUpdates]; 
    [[self tableView] insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade]; 
    [[self tableView] endUpdates]; 
} 
+0

我上面的解決方案做了但是我在插入行得到了異常。「由於未捕獲異常'NSRangeException',原因:'*** - [NSMutableIndexSet addIndexesInRange:]:範圍{2147483647,1}超過了NSNotFound - 1'」的最大索引值。 – diana 2009-09-08 08:56:26

+0

工作很好!!! Tanxs很多。 – diana 2009-09-08 09:51:40

+0

對不起indexPathForRow:[dataSource count]應該是indexPathForRow:([dataSource count] - 1) – nduplessis 2009-09-08 13:18:27