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 met开发者_开发问答hod 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;
}
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....
精彩评论