[英]Setting an Image on Custom TableViewCell
我想知道如何將圖像放在自定義單元格的背景上
我使用的代碼就是這個,但是這會將所有表格設置為同一張圖片,我希望每個單元格都有自己的背景,具體取決於誰是消息所有者,我希望您能對我有所幫助!
- (UITableViewCell *)tableView:(UITableView *)myTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = (UITableViewCell *)[self.messageList dequeueReusableCellWithIdentifier:@"ChatListItem"];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ChatListItem" owner:self options:nil];
cell = (UITableViewCell *)[nib objectAtIndex:0];
}
NSDictionary *itemAtIndex = (NSDictionary *)[messages objectAtIndex:indexPath.row];
UILabel *textLabel = (UILabel *)[cell viewWithTag:1];
textLabel.text = [itemAtIndex objectForKey:@"text"];
UILabel *userLabel = (UILabel *)[cell viewWithTag:2];
userLabel.text = [itemAtIndex objectForKey:@"user"];
if(usernameText=userLabel){
UIImageView* img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"chat2@2x.png"]];
[cell setBackgroundView:img];
[img release];
}
else{
UIImageView* img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"chat1@2x.png"]];
[cell setBackgroundView:img];
[img release];
}
return cell;
}
這行對我來說不太好:
if (usernameText=userLabel)
應該是:
if ([usernameText isEqualToString:userLabel.text])
嘗試這個,
tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"chat1@2x.png"]];
要么,
cell.backgroundView = [[UIImageView分配] initWithImage:[UIImage imageNamed:@“ chat1@2x.png”]]];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.