[英]iOS AccessoryDetail image doesn't update when reloading UITableView
I'm loading a UITableView sometimes it will have 5 cells and sometimes 4 cells. 我正在加载一个UITableView,有时它将有5个单元格,有时还有4个单元格。 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. 根据它将拥有多少个单元格,我想为第2行或第3行设置AccessoryDetail按钮。我知道条件有效,因为我已经使用
didSelectRowAtIndexPath:
成功地尝试了它didSelectRowAtIndexPath:
但由于某种原因,TableView似乎没有根据显示的行数进行更新。 I'm reloading the TableView data successfully in viewWillAppear:
with [tableView reloadData]
but that doesn't take care of the AccessoryDetail problem for me. 我正在
viewWillAppear:
成功重新加载TableView数据viewWillAppear:
使用[tableView reloadData]
但是我没有处理AccessoryDetail问题。 I've tried using [tableView reloadInputViews]
to no avail. 我尝试使用
[tableView reloadInputViews]
无济于事。 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. 问题是AccessoryDetail映像始终设置为第2行或第3行,具体取决于我从应用程序开始加载的视图。
Here's the logic from the cellForRowAtIndexPath:
method: 这是来自
cellForRowAtIndexPath:
方法的逻辑:
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: 编辑:我根据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;
}
}
你应该重置选择样式和附件类型,你没有其他条款...一旦你设置它,就是它,如果你重复使用单元格,他们永远不会让他们的配件重置....
Put the if-else statement outside of the if( cell == nil ) block of code. 将if-else语句放在if(cell == nil)代码块之外。 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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.