for (UIView *cellView in cell.subviews){
[cellView removeFromSuperview];
}
避免图片出现问题
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}else{
for (UIView *cellView in cell.subviews){
[cellView removeFromSuperview];
}
}
if (indexPath.row %2 == 0) {
//cell.backgroundColor =[UIColor redColor];
}
cell.backgroundColor =[UIColor clearColor];
NSDictionary *dict = [_resultArray objectAtIndex:indexPath.row];
//创建头像
UIImageView *photo ;
if ([[dict objectForKey:@"name"]isEqualToString:@"rhl"]) {
//我
photo = [[UIImageView alloc]init];//WithFrame:CGRectMake(WIDTH- WIDTH*40/320, cell.bounds.size.height, WIDTH*30/320, WIDTH*30/320)];
photo.layer.masksToBounds =YES;
photo.layer.cornerRadius =15;
[cell addSubview:photo];
[photo mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(30, 30));
make.right.equalTo(@(-10));
make.bottom.equalTo(@(-20));
}];
photo.image = [UIImage imageNamed:@"headImage10.jpg"];
photo.backgroundColor =[UIColor yellowColor];
if ([[dict objectForKey:@"content"] isEqualToString:@"0"]) {
[cell addSubview:[self yuyinView:1 from:YES withIndexRow:indexPath.row withPosition:WIDTH*25/320]];
}else{
[cell addSubview:[self bubbleView:[dict objectForKey:@"content"] from:YES withPosition:WIDTH*65/320]];
}
}else{
//对方
photo = [[UIImageView alloc]initWithFrame:CGRectMake(10,15,30,30)];
photo.layer.masksToBounds =YES;
photo.layer.cornerRadius =15;
[cell addSubview:photo];
photo.image = [UIImage imageNamed:@"headImage2.jpg"];
photo.backgroundColor =[UIColor blackColor];
if ([[dict objectForKey:@"content"] isEqualToString:@"0"]) {
[cell addSubview:[self yuyinView:1 from:NO withIndexRow:indexPath.row withPosition:WIDTH*25/320]];
}else{
[cell addSubview:[self bubbleView:[dict objectForKey:@"content"] from:NO withPosition:WIDTH*65/320]];
}
}
return cell;
}