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