0

我想知道如何将图像放在自定义单元格的背景上

我使用的代码是这个,但是这会将所有表格设置为相同的图像我希望每个单元格都有自己的背景,具体取决于谁是消息所有者我希望你能帮助我,这正在杀死我!

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

2 回答 2

3

这条线对我来说看起来不太好:

if (usernameText=userLabel)

也许应该是:

if ([usernameText isEqualToString:userLabel.text])
于 2012-10-25T03:31:15.350 回答
0

尝试这个,

tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"chat1@2x.png"]];

或者,

cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"chat1@2x.png"]];

于 2012-10-25T03:50:19.797 回答