我尝试这种情况,但不工作。如果senderlabel = key @“sender”对每一行使用green.png或grey.png。uitableview问题
if (senderLabel.text = [tempMsg objectForKey:@"sender"])
{
[cell.msgText setText:[tempMsg objectForKey:@"message"]];
[messTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
//[cell setFrame:CGRectMake(0,0, size.width, size.height)];
[cell.msgText setFrame:CGRectMake(15,3, size.width, result)];
UIImage* balloon = [[UIImage imageNamed:@"grey.png"] stretchableImageWithLeftCapWidth:24 topCapHeight:15];
UIImageView *newImage = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, size.width+10, result+10)];
UIView *newView =[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, cell.frame.size.width, cell.frame.size.height)];
[newImage setImage:balloon];
[newView addSubview:newImage];
[cell setBackgroundView:newView];
}else {
[cell.msgText setText:[tempMsg objectForKey:@"message"]];
[messTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
//[cell setFrame:CGRectMake(0,0, size.width, size.height)];
[cell.msgText setFrame:CGRectMake(15,3, size.width, result)]; //propriété du texte
UIImage* balloon = [[UIImage imageNamed:@"green.png"] stretchableImageWithLeftCapWidth:24 topCapHeight:15];
UIImageView *newImage = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, size.width+10, result+10)];
UIView *newView =[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, cell.frame.size.width, cell.frame.size.height)];
[newImage setImage:balloon];
[newView addSubview:newImage];
[cell setBackgroundView:newView];
}
return cell;
}
THX但我所有的数据用绿色。在每一行中,我有不同的数据 – XcodeMania
我想我必须使用indexPath.row但是如何? – XcodeMania
您可以继承UITableViewCell并在其中添加自己的背景,它会更容易,更快速。请记住重写setFrame:更改背景的大小。 – EmilioPelaez