简体   繁体   中英

iOS AccessoryDetail image doesn't update when reloading UITableView

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

}

你应该重置选择样式和附件类型,你没有其他条款...一旦你设置它,就是它,如果你重复使用单元格,他们永远不会让他们的配件重置....

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM