我嘗試這種情況,但不工作。如果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