【问题标题】:iOS AccessoryDetail image doesn't update when reloading UITableView重新加载 UITableView 时,iOS AccessoryDe​​tail 图像不会更新
【发布时间】:2011-06-16 12:43:26
【问题描述】:

我正在加载 UITableView,有时它会有 5 个单元格,有时会有 4 个单元格。根据它将有多少个单元格,我想为第 2 行或第 3 行设置 AccessoryDe​​tail 按钮。我知道该条件有效,因为我已使用didSelectRowAtIndexPath: 成功尝试过,但由于某种原因,TableView 似乎没有根据显示的行数进行更新。我在viewWillAppear:[tableView reloadData] 中成功地重新加载了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;
 }

}

【问题讨论】:

  • @请详细说明您的问题。通过发布您的 cellForRowAtIndexPath 方法或发布屏幕截图。

标签: objective-c ios cocoa-touch uitableview reloaddata


【解决方案1】:

将 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;
 }

【讨论】:

  • 谢谢,经过反复试验,我终于自己弄清楚了!
【解决方案2】:

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

【讨论】:

  • 我添加了 else 子句,但它似乎没有做任何事情(请参阅我的编辑)。
猜你喜欢
  • 2011-09-18
  • 1970-01-01
  • 2013-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多