Replace the default checkmark for UITableView with a custom image
ios, objective-c, uitableview
Solution
Method-1:
I think you can use
Suppose you have a UIButton with checkmark image.
On `cellForRowAtIndexPath:`
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
button.frame = frame;
[button setBackgroundImage:image forState:UIControlStateNormal];
[button addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor clearColor];
cell.accessoryView = button;
checkButtonTapped:event: Method:
- (void)checkButtonTapped:(id)sender event:(id)event
{
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: currentTouchPosition];
if (indexPath != nil)
{
[self tableView: self.tableView accessoryButtonTappedForRowWithIndexPath: indexPath];
}
}
accessoryButtonTappedForRowWithIndexPath: Method
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
NSMutableDictionary *item = [dataArray objectAtIndex:indexPath.row];
BOOL checked = [[item objectForKey:@"checked"] boolValue];
[item setObject:[NSNumber numberWithBool:!checked] forKey:@"checked"];
UITableViewCell *cell = [item objectForKey:@"cell"];
UIButton *button = (UIButton *)cell.accessoryView;
UIImage *newImage = (checked) ? [UIImage imageNamed:@"unchecked.png"] : [UIImage imageNamed:@"checked.png"];
[button setBackgroundImage:newImage forState:UIControlStateNormal];
}
I have taken above code from this link:
Implement a Custom Accessory View For UITableView in iPhone
Method-2:
Also there is a way to make it using custom tableView cell.
I think you have to make a custom cell and add an imageView on right hand side of the cell.
This imageView will then hold the image you want.
On didSelectRowAtRowAtIndexPath: you can add the image and remove based on number of taps.
Do let me know if you want more help.
Hope this helps.
Problem
I'd like to replace the default checkmark image that is shown when a UITableViewCell's accessory is set to: UITableViewCellAccessoryCheckmark. So I'd still like to write: ``` [cell setAccessoryType:UITableViewCellAccessoryCheckmark]; ``` But I'd like my own image to be shown rather than the default. Any help is much appreciated.