重新加载 UITableView 时 iOS AccessoryDetail 图像不会更新
我正在加载 UITableView,有时它有 5 个单元格,有时有 4 个单元格。根据它有多少个单元格,我想为第 2 行或第 3 行设置 AccessoryDetail 按钮。我知道该条件有效,因为我已经使用 didSelectRowAtIndexPath:
成功尝试过,但由于某种原因TableView 似乎不会根据显示的行数进行更新。我正在使用 [tableView reloadData]
在 viewWillAppear:
中成功重新加载 TableView 数据,但这并不能解决我的 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;
}
}
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将 if-else 语句放在 if( cell == nil ) 代码块之外。如果您重复使用该单元,则不会调用您的任何代码。
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.
您应该重置选择样式和附件类型,其中没有其他子句...一旦设置它,就是这样,如果您重复使用单元格,它们将永远不会重置附件...
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....