UITableViewCell的accessoryView在可见视图改变后消失

发布于 2024-10-11 14:29:05 字数 609 浏览 4 评论 0原文

我正在经历这种奇怪的情况。我有一个 UITableView,当用户选择一个单元格时,一个漫长的(网络)过程开始。因此,我在后台线程中执行此操作,并放置(在 didSelectRowAtIndexPath 中)UIActivityIndi​​catorView 作为附件视图。这就是我写的:

UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:indexPath];
UIActivityIndicatorView* activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
cell.accessoryView = activityView;
[activityView startAnimating];
[activityView release];

一切似乎都正常工作,除了在加载过程中(当指示器动画时),我使用 UITabBar 切换视图,当我返回到 UITableView 时, UIActivityIndi​​catorView 应该仍然是那里已经消失了。知道我做错了什么吗?谢谢!

I'm experiencing this strange situation. I have a UITableView where, when the user selects a cell, a long (network) process begins. So, I performed this in a background thread and I placed (in the didSelectRowAtIndexPath) a UIActivityIndicatorView as the accessory view. This is what I wrote:

UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:indexPath];
UIActivityIndicatorView* activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
cell.accessoryView = activityView;
[activityView startAnimating];
[activityView release];

everything seems to work correctly, except that, if during a loading process (when the indicator is animated), I switch the view using a UITabBar, when I go back to the UITableView, the UIActivityIndicatorView that should still be there has disappeared. Any idea of what I did wrong? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

相对绾红妆 2024-10-18 14:29:05

我正在回顾这个问题,并且......现在读完问题后,我笑了:-)
我自己的问题的答案是:代码没有任何问题,它工作得很好...但是将 UIActivityIndi​​catorView 设为白色...使得很难在白色背景上看到它:-D :-D 发布的代码是正确的。

I was looking back at this issue, and... after reading the question now, I'm laughing :-)
Answer to my own question is: nothing is wrong with the code, it works very well... but making the UIActivityIndicatorView white... makes it difficult to see it on white background :-D :-D The posted code is correct.

一曲琵琶半遮面シ 2024-10-18 14:29:05

当您在转到另一个选项卡后返回表视图时,cellForRowAtIndexPath: 消息将发送到表视图控制器以显示表的单元格,但活动指示器视图被设置为didSelectRowAtIndexPath: 方法中单元格的辅助视图。因此,本质上,您在选择单元格时更改了单元格(即显示活动指示器视图),但当您离开视图并返回时,使用 cellForRowAtIndexPath: 方法重新显示单元格(因此,没有活动指示器视图)。

您必须跟踪哪些单元格当前具有活动指示器视图,并确保使用 < 中的 UIActivityIndi​​catorView 设置这些单元格的辅助视图。代码>cellForRowAtIndexPath:方法。显然,如果与单元格关联的活动已完成,则不会显示该单元格的活动指示器视图,因此您必须跟踪活动是否已完成。有很多方法可以做到这一点,因此您必须决定哪种方法最适合您的情况。

返回到时发送(至少是必要的)cellForRowAtIndexPath: 消息是不正确的。来自另一个选项卡视图的表格视图,因此我“删除”了上面的答案。

When you go back to the table view after going to another tab, cellForRowAtIndexPath: messages are sent to the table view controller to display the cells of the table but the activity indicator view was set as the accessory view of the cell in the didSelectRowAtIndexPath: method. So, essentially, you changed the cell (i.e., displayed the activity indicator view) when the cell was selected but when you left the view and came back the cellForRowAtIndexPath: method was used to re-display the cell (and, hence, no activity indicator view).

You'll have to keep track of what cells currently have activity indicator views and make sure you set the accessory view of those cells with a UIActivityIndicatorView in the cellForRowAtIndexPath: method. Obviously, if the activity associated with a cell has completed then don't display the activity indicator view for that cell so you'll have to keep track of whether the activities have completed yet or not. There are many ways you can do this so you'll have to decide what works best for your situation.

It's not true that cellForRowAtIndexPath: messages are sent (at least necessarily) when returning to the table view from another tab view so I've "deleted" my answer above.

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