iOS UiTableViewCell selective cells to show accessoryView
-
12-11-2019 - |
Question
My problem is similar as this:
But I want to show accessoryView with DetailDisclosureButton only for cells that have details.
I have written some codes, but I don´t know in which method to put it.
if (totalcount == 0 && totalcount2 == 0)
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
else
{
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
}
totalcount and totalcount2 is count of two mutable arrays that tell me whether cells have details or not.
Please ask me if my question is not clear, thank you.
Solution
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString* identifierString;
if(totalCount == 0 && totalCount2 == 0) {
identifierString = @"0";
} else {
identifierString = @"1";
}
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:identifierString];
if(cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:YOUR_CELL_STYLE
reuseIdentifier:identifierString] autorelease];
if ([identifierString intValue] == 0) {
cell.accessoryType = UITableViewCellAccessoryNone;
} else {
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
}
//do whatever setup you need to do
}
return cell;
}
Though to me, it seems that if totalCount and totalCount2 both equal 0, then all of the cells are going to have the same accessory type. Is that what you what? If not, you might want to rethink your logic.
OTHER TIPS
You should do that in tableView:cellForRowAtIndexPath:
, while you're setting up your UITableViewCell
add this to tableView:cellForRowAtIndexPath:
after the cell has been dequeued or initialised
You can use this library . with MSCellAccessory, UITableViewCell's accessoryType can easily customizing the colors. support Flat Design same as iOS7. https://github.com/bitmapdata/MSCellAccessory