Can an icon on a UITableViewAccessory be replaced?

I have a table view, in one of the conditions I want to use an accessory using UITableViewCellAccessoryDetailDisclosureButton, but I want to change this icon using my icon, can I do this?

+3
source share
1 answer

yes .... you can do it easily by adding a custom button ...

  • Create a custom button,
  • Button Image Adjustment
  • Add this button to accessView
  • Set the method for the button

I give you some help with the code:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"Cell";
   cell =(Custom *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[[Custom alloc]  initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
        [cell setBackgroundColor:[UIColor clearColor]];
        menuTable.separatorColor =[UIColor clearColor];

    }
    UIImage *image = [UIImage imageNamed:@"ButtonClickOrder.png"];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
    button.frame = frame;   // match the button size with the image size

    //[button setBackgroundImage:image forState:UIControlStateNormal];
    [button setImage:image forState:UIControlStateNormal];
    // set the button target to this table view controller so we can interpret touch events and map that to a NSIndexSet
    [button addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
    button.backgroundColor = [UIColor clearColor];
    cell.accessoryView = button;

       return cell;
}

//These are the methods which are called with the AccessoryView is Clicked

- (void)checkButtonTapped:(id)sender event:(id)event
{

    NSSet *touches = [event allTouches];
    UITouch *touch = [touches anyObject];
    CGPoint currentTouchPosition = [touch locationInView:menuTable];
    NSIndexPath *indexPath = [menuTable indexPathForRowAtPoint: currentTouchPosition];
    NSLog(@"Section:%d",indexPath.section);
    NSLog(@"Index:%d",indexPath.row);
    if (indexPath != nil)
    {
         [ self tableView: menuTable accessoryButtonTappedForRowWithIndexPath: indexPath];

    }

}

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{   

        NSLog(@"%d",indexPath.section);

}

Hope this surely works for u ....

+8
source

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


All Articles