0

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

}
4

2 回答 2

1

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

于 2011-06-16T12:46:44.463 回答
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;
 }
于 2011-06-16T14:03:52.893 回答