Replace default checkmark for UITableView with custom image

I want to replace the default image that is displayed when the UITableViewCell accessory is installed: UITableViewCellAccessoryCheckmark.

So I would still like to write:

[cell setAccessoryType:UITableViewCellAccessoryCheckmark]; 

But I would like my own image to be displayed, and not by default.

Any help is greatly appreciated.

+6
source share
3 answers

Method 1:

I think you can use

Suppose you have a UIButton with a check mark.

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 took the code above from this link:

Embed a custom accessory view for UITableView on iPhone

Method 2:

There is also a way to do this using a custom tableView cell.

I think you need to create a custom cell and add an image to the right side of the cell.

Then this image will hold the desired image.

On didSelectRowAtRowAtIndexPath: you can add an image and delete it based on the number of taps.

Let me know if you need more help.

Hope this helps.

+4
source

Check out this code:

 - (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath { ... NSMutableDictionary *item = [dataArray bjectAtIndex:indexPath.row]; cell.textLabel.text = [item objectForKey:@"text"]; [item setObject:cell forKey:@"cell"]; BOOL checked = [[item objectForKey:@"checked"] boolValue]; UIImage *image = (checked) ? [UIImage imageNamed:@"checked.png"] : [UIImage imageNamed:@"unchecked.png"]; 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; ... } - (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]; } } - (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]; } 

link

+2
source

Source: https://habr.com/ru/post/913518/


All Articles