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

发布于 2024-12-27 02:23:53 字数 543 浏览 1 评论 0原文

我的主视图中的弹出窗口中有一个表格视图,它是一个网络浏览器。每当用户访问网页时,我希望它将该页面添加到最近访问的网站列表中。我设置了代码,将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你爱我像她 2025-01-03 02:23:53

还要确保从 – tableView:numberOfRowsInSection: 返回数组的正确长度

Also make sure you are returning the correct length of your array from – tableView:numberOfRowsInSection:

朕就是辣么酷 2025-01-03 02:23:53

尝试。

[tableView reloadTable];

添加新数据后

Try

[tableView reloadTable];

after you add the new data.

此生挚爱伱 2025-01-03 02:23:53

如果你不想动画更新,你可以调用:

[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.

刘备忘录 2025-01-03 02:23:53

如果您有一个大表,[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];

顺便说一句,_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:

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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文