Ladessa
Ladessa

Reputation: 1003

Dynamically change UITableViewCell's accessory view image

I have a CustomTableView Cell and it has an accessory view. How can I change the accessory view's image dynamically?

Upvotes: 0

Views: 3897

Answers (4)

Subbu
Subbu

Reputation: 2101

modifications to the cell except textColor and detailTextColor of cell should be done in the willDisplayCell: method and not the cellForRowAtIndexPath method , the solution for your question is as follows --

 -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cellforIndexPath:(NSIndexPath *)indexPath
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:[UIImage imageName:@"image.png" forControlState:UIControlStateNormal];
    btn.frame = CGRectMake(0,0,28,28) ;

    cell.accessoryView = btn ;

}

this code will definitely work !!

Upvotes: 0

Erik Tjernlund
Erik Tjernlund

Reputation: 1983

In the init method of the custom cell, you can do something like (assuming ARC btw):

UIImage *customBtnImg = [UIImage imageNamed:@"custom_btn"];
UIButton *customBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, customBtnImg.size.width, customBtnImg.size.height)];
customBtn.backgroundColor = [UIColor clearColor];
[customBtn setBackgroundImage:customBtnImg forState:UIControlStateNormal];
self.accessoryView = customBtn;

Upvotes: 0

Harish
Harish

Reputation: 2512

Use your custom button for accessoryview like..

// stick the pencil icon on the accessory view

UIButton* pencil = [UIButton buttonWithType:UIButtonTypeCustom];
[pencil setImage:[UIImage imageNamed:@"icon-pencil.gif"] forState:UIControlStateNormal];
pencil.frame = CGRectMake(0, 0, 15, 15);
pencil.userInteractionEnabled = YES;
[pencil addTarget:self action:@selector(didTapEditButton:)      forControlEvents:UIControlEventTouchDown];
cell.accessoryView = pencil; 

Upvotes: 2

Ivor Prebeg
Ivor Prebeg

Reputation: 978

do not use accessoryView, just place another imageView and change accordingly

Upvotes: 0

Related Questions