将对象添加到数组后,tableview不会更新

时间:2012-01-13 21:10:57

标签: objective-c ios uitableview nsarray

我在主视图中的弹出窗口中有一个tableview,它是一个Web浏览器。每当用户访问网页时,我希望它将该页面添加到最近访问过的网站列表中。我将代码设置为将URL作为字符串添加到作为表视图的数据源的数组中。表格视图仅显示访问过的第一个站点,并且不会显示过去的任何内容。但是我知道这些站点正在添加到阵列中,因为该阵列在添加后使用NSLog语句显示了站点。但是他们仍然不会出现在表格中。任何人都可以帮助我吗?

编辑:

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    // Return the number of rows in the section.
    return [recentlyVisitedUrls count];
}

4 个答案:

答案 0 :(得分:2)

尝试

[tableView reloadTable];

添加新数据后。

答案 1 :(得分:2)

还要确保从– tableView:numberOfRowsInSection:

返回正确长度的数组

答案 2 :(得分:0)

如果您不想更新动画,可以致电:

[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO]

如果您需要动画,最好阅读:Table View Programming Guide for iOS,您需要Batch insert…部分。

答案 3 :(得分:0)

如果您有一张大桌子,[tableView reloadData]可能不太理想。它还会将您的视图重置为表格的开头,这可能是也可能不是。您可以尝试这样的事情:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_model.recordCount - 1 inSection:0];
[_tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
[_tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:NO];

BTW,_ model是示例中表的数据委托类。

此外,请确保数据委托在插入之前正确更新其总计数,否则您的应用程序将在insertRowsAtIndexPaths上崩溃。