在UITableViewCell onload中更改UIButton颜色

时间:2016-05-18 04:06:14

标签: ios objective-c uitableview uibutton

我有联系人列表,应用可以添加/查看详细信息。我在自定义UITableViewCell中添加了一个星形按钮,它用于“收藏夹”功能。如果联系人为“收藏”,我希望我的按钮为黄色,否则为灰色。这是我的代码。

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;
}

这是我的cellForRowAtIndexPath代码。我目前正在尝试setTitleColor属性,但它不起作用。与setTintColor相同,它也不起作用。

1 个答案:

答案 0 :(得分:0)

色调是您想要的颜色,但它仅在图像是带有Alpha通道的PNG时才有效,并且图像的渲染模式设置为模板。请参阅this answer。如果您要将图像分配到情节提要中的按钮,则可以在asset library中设置渲染模式。