2016-05-18 118 views
0

我有一個聯繫人列表,應用程序可以添加/查看詳細信息。我在我的自定義UITableViewCell中添加了一個星形按鈕,它用於「收藏夾」功能。如果聯繫人爲'已收藏',我希望我的按鈕爲黃色,如果不是,請選擇灰色。這是我的代碼。在UITableViewCell onload中更改UIButton顏色

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相同,它也不起作用。

+0

你正在改變這些按鈕的標題顏色,但我認爲你沒有設置編碼部分的任何標題。這些按鈕的標題是什麼?如果你設置了一些圖像或背景圖像或背景顏色的最喜歡和不喜歡的按鈕比可能更有用的標識按鈕。 –

+0

我只看到了其他參考的答案。我也嘗試tintcolor並沒有工作。 – EdBer

回答

0

色調是你想要的,但它只適用於圖像是帶有alpha通道的PNG,並且圖像的渲染模式被設置爲模板。參考this answer。如果您將圖像分配到故事板中的按鈕,則可以在asset library中設置渲染模式。

+0

什麼將它設置爲模板?或者什麼是重要的? – EdBer

+0

這解釋了圖像模板https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/MobileHIG/BarIcons.html –

+0

無論如何。它適合我!多謝老兄! :) – EdBer