将对象添加到数组后,tableview 将不会更新
我的主视图中的弹出窗口中有一个表格视图,它是一个网络浏览器。每当用户访问网页时,我希望它将该页面添加到最近访问的网站列表中。我设置了代码,将 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];
}
I have a tableview inside a popover in my main view which is a web browser. Whenever the user visits a web page I want it to add the page to a list of recently visited websites. I have the code set up to add the URL as a string to the array that is the data source for the table view. The table view only shows the first site visited and won't show anything past that. However I know the sites are being added to the array because the array shows the Sites using NSLog statements after being added. However they still won't show up in the table. Can anyone help me?
EDIT:
- (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];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
还要确保从
– tableView:numberOfRowsInSection:
返回数组的正确长度Also make sure you are returning the correct length of your array from
– tableView:numberOfRowsInSection:
尝试。
添加新数据后
Try
after you add the new data.
如果你不想动画更新,你可以调用:
[self.tableView PerformSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO]
如果你需要动画,最好阅读:iOS 表格视图编程指南,需要
批量插入…
部分。If you wan't animated updating, you can call:
[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO]
If you need animation, it will be better read: Table View Programming Guide for iOS, you need
Batch insert…
part.如果您有一个大表,
[tableView reloadData]
可能并不理想。它还会将您的视图重置为表格的开头,这可能是也可能不是所需的。您可以尝试这样的操作:顺便说一句,_model 是示例中表的数据委托类。
另外,请确保数据委托在插入之前正确更新其总计数,否则您的应用程序将在 insertRowsAtIndexPaths 上崩溃。
If you have a large table,
[tableView reloadData]
is probably not ideal. It'll also will reset your view to the beginning of the table, which may or may not be desirable. You might try something like this:BTW, _model is the data delegate class for the table in the example.
Also, make sure the data delegate updates it's total count correctly before you insert, or your app will crash on the insertRowsAtIndexPaths.