2009-09-08 101 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