重新加载 UITableView 时 iOS AccessoryDe​​tail 图像不会更新

发布于 2024-11-15 12:21:30 字数 1545 浏览 4 评论 0原文

我正在加载 UITableView,有时它有 5 个单元格,有时有 4 个单元格。根据它有多少个单元格,我想为第 2 行或第 3 行设置 AccessoryDe​​tail 按钮。我知道该条件有效,因为我已经使用 didSelectRowAtIndexPath: 成功尝试过,但由于某种原因TableView 似乎不会根据显示的行数进行更新。我正在使用 [tableView reloadData]viewWillAppear: 中成功重新加载 TableView 数据,但这并不能解决我的 AccessoryDe​​tail 问题。我尝试使用 [tableView reloadInputViews] 无济于事。问题是 AccessoryDe​​tail 图像始终设置为第 2 行或第 3 行,具体取决于我开始从应用程序加载哪个视图。

这是 cellForRowAtIndexPath: 方法的逻辑:

if ((row == 2) && ([[self.office boxAddress] length] == 0) || (row == 3) && ([[self.office boxAddress] length] != 0)) {
            cell.selectionStyle = UITableViewCellSelectionStyleGray;
            cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
        }

编辑: 我已经根据 Simon Lee 的建议更改了我的方法,并使用 else 子句,使其看起来像这样,但它似乎也不起作用:

if (cell == nil) {

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:OfficeCellIdentifier] autorelease];


 if ((row == 2) && ([[self.office boxAddress] length] == 0) || (row == 3) && ([[self.office boxAddress] length] != 0)) {
     cell.selectionStyle = UITableViewCellSelectionStyleGray;
     cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
 //NSLog(@"row == 2 && [[self.office boxAddress] length] == 0 || row == 3");
 } else {
     cell.selectionStyle = UITableViewCellSelectionStyleNone;
     cell.accessoryType = UITableViewCellAccessoryNone;
 }

}

I'm loading a UITableView sometimes it will have 5 cells and sometimes 4 cells. Depending on how many cells it will have I want to set the AccessoryDetail button for either row 2 or row 3. I know that the condition works because I've tried it successfully with didSelectRowAtIndexPath: but for some reason the TableView doesn't seem to get updated depending on how many rows that are displayed. I'm reloading the TableView data successfully in viewWillAppear:with [tableView reloadData] but that doesn't take care of the AccessoryDetail problem for me. I've tried using [tableView reloadInputViews]to no avail. The problem is that the AccessoryDetail image is always set to either row 2 or row 3 depending on which view I start to load from the application.

Here's the logic from the cellForRowAtIndexPath: method:

if ((row == 2) && ([[self.office boxAddress] length] == 0) || (row == 3) && ([[self.office boxAddress] length] != 0)) {
            cell.selectionStyle = UITableViewCellSelectionStyleGray;
            cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
        }

EDIT:
I've changed my method according to Simon Lee's suggestion with an else clause to look like this but it doesn't seem to work either:

if (cell == nil) {

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:OfficeCellIdentifier] autorelease];


 if ((row == 2) && ([[self.office boxAddress] length] == 0) || (row == 3) && ([[self.office boxAddress] length] != 0)) {
     cell.selectionStyle = UITableViewCellSelectionStyleGray;
     cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
 //NSLog(@"row == 2 && [[self.office boxAddress] length] == 0 || row == 3");
 } else {
     cell.selectionStyle = UITableViewCellSelectionStyleNone;
     cell.accessoryType = UITableViewCellAccessoryNone;
 }

}

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

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

发布评论

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

评论(2

风蛊 2024-11-22 12:21:30

将 if-else 语句放在 if( cell == nil ) 代码块之外。如果您重复使用该单元,则不会调用您的任何代码。

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:OfficeCellIdentifier] autorelease];
}

 if ((row == 2) && ([[self.office boxAddress] length] == 0) || (row == 3) && ([[self.office boxAddress] length] != 0)) {
     cell.selectionStyle = UITableViewCellSelectionStyleGray;
     cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
 } else {
     cell.selectionStyle = UITableViewCellSelectionStyleNone;
     cell.accessoryType = UITableViewCellAccessoryNone;
 }

Put the if-else statement outside of the if( cell == nil ) block of code. If you're re-using the cell, none of your code is getting called.

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:OfficeCellIdentifier] autorelease];
}

 if ((row == 2) && ([[self.office boxAddress] length] == 0) || (row == 3) && ([[self.office boxAddress] length] != 0)) {
     cell.selectionStyle = UITableViewCellSelectionStyleGray;
     cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
 } else {
     cell.selectionStyle = UITableViewCellSelectionStyleNone;
     cell.accessoryType = UITableViewCellAccessoryNone;
 }
琴流音 2024-11-22 12:21:30

您应该重置选择样式和附件类型,其中没有其他子句...一旦设置它,就是这样,如果您重复使用单元格,它们将永远不会重置附件...

You should reset the selection style and accessory type, you have no else clause in there...once you set it, that's it, if you reuse cells they will never get their accessory reset....

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