0

I have a list of contacts, app can add/view details. I added a star button in my custom UITableViewCell, It is for "Favorites" functionality. I want my button to be color yellow if the contact is 'Favorited' and Gray if not. Here is my code.

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier
                                                           forIndexPath:indexPath];

ContactUtilities *cu = [[ContactUtilities alloc]init];

NSMutableDictionary *listToUse = [NSMutableDictionary new];

if([self.txtSearchContacts.text isEqualToString:@""]){
    listToUse = self.contactList;
} else {
    listToUse = searchContactList;
}


NSArray *keyList = [listToUse allKeys];
NSString *search = [keyList objectAtIndex:indexPath.row];
Contact *contact = [[Contact alloc]init];
contact = [cu searchContact:search :listToUse];

//Tagging
UILabel *lblContactName    = [cell.contentView viewWithTag:ContactCellElementName];
UIImageView *imgContactPic = [cell.contentView viewWithTag:ContactCellElementImage];
UILabel *lblContactCompany = [cell.contentView viewWithTag:ContactCellElementCompany];
UILabel *lblContactNumber  = [cell.contentView viewWithTag:ContactCellElementNumber];
UIButton *isFavorite       = (UIButton *)[cell viewWithTag:ContactCellElementButton];

UIImage* image = [UIImage imageNamed:contact.imageName];

CGImageRef cgref = [image CGImage];
CIImage *cim = [image CIImage];

if (cim == nil && cgref == NULL)
{
    image = [UIImage imageNamed:@"Person"];
}

lblContactName.text    = contact.contactName;
imgContactPic.image    = image;
lblContactCompany.text = contact.contactCompanyName;
lblContactNumber.text  = contact.contactPhoneNumber;

//This is where it doesn't work..
if([contact.isFavorite isEqualToString:@"YES"]){
    [isFavorite setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
} else {
    [isFavorite setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
}

//I also have a property that will check if you will show the button. This is working already.

if([contact.shouldShow isEqualToString:@"No"]){
    [isFavorite removeFromSuperview];
}

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}

That is my cellForRowAtIndexPath code. I am currently trying the setTitleColor property but it doesn't work. Same as the setTintColor it doesn't work either.

Ajay Gabani
  • 1,168
  • 22
  • 38
EdBer
  • 69
  • 8
  • You are changing title color of those buttons but i think you are not setting any title from coding part.. What is title for those buttons? and if you set some image or background-image or background-color for favorite and non-favorite buttons than that might be more useful to identify buttons. – Ajay Gabani May 18 '16 at 04:11
  • I only saw that answer to other reference. I also tried tintcolor and didnt work. – EdBer May 18 '16 at 04:39

1 Answers1

0

Tint color is what you want, but it only works if the image is a PNG with an alpha channel, and the rendering mode of the image is set to template. Refer to this answer. If you're assigning the image to the button in a storyboard, you can set the rendering mode in the asset library.

Community
  • 1
  • 1
Brett Donald
  • 6,745
  • 4
  • 23
  • 51
  • What does setting it to template exactly do? or what is the importance? – EdBer May 18 '16 at 04:43
  • This explains image templates https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/MobileHIG/BarIcons.html – Brett Donald May 18 '16 at 04:44
  • Anyway. It worked well for me! Thanks a lot dude! :) – EdBer May 18 '16 at 04:47
  • When an image is rendered as "original", the color information from the image is used, and tintColor has no effect. When an image is rendered as "template", the color information from the image is ignored (only the alpha channel is used) and tintColor works. – Brett Donald May 18 '16 at 04:47
  • Wow. Great info! :) Thanks again @Brett :) – EdBer May 18 '16 at 04:50