UITableview覆盖olddata

时间:2014-08-17 14:26:34

标签: ios uitableview

嗨我正在创建聊天示例,如果用户添加发送按钮在我的arrray中添加新对象并重新加载数据但是它覆盖了旧表。

enter image description here

我的阵列:

arr1 = [[NSMutableArray alloc]  initWithObjects:@"Whats up today ? ", @"Hello.", nil];

这是我的来源:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *simleTableIdentifier = @"TeamCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simleTableIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TeamCell"];
    }


    cell.backgroundColor = [UIColor clearColor];
    tableView.scrollEnabled = YES;

    UIImageView *myRect = [[UIImageView alloc] initWithFrame:CGRectMake(20, 30, 10, 12)];
    myRect.image = [UIImage imageNamed:@"tri.png"];
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"jj.jpg"]];


    UILabel *label = [[UILabel alloc] init];
    label.text = [arr1 objectAtIndex:indexPath.row];
    label.numberOfLines = 0;
    label.lineBreakMode = NSLineBreakByWordWrapping;
    label.font =[UIFont systemFontOfSize:14.0];
    label.text = [arr1 objectAtIndex: indexPath.row];
    label.textColor = [UIColor blackColor];
    label.backgroundColor = [UIColor clearColor];
    label.frame = CGRectMake(40, 40, 210, 80);
    [label sizeToFit];

    UIImageView *imgim = [[UIImageView alloc] initWithFrame:CGRectMake(30, 10, label.frame.size.width+20, label.frame.size.height+20)];
    imgim.image = [UIImage imageNamed:@"bg.png"];

    CGRect myRectANGLE =  CGRectMake(imgim.frame.origin.x+10,imgim.frame.origin.y+10, label.frame.size.width, label.frame.size.height);

    [label setFrame:myRectANGLE];

    CALayer *addRadius = [imgim layer];
    [addRadius setMasksToBounds:YES];
    [addRadius setCornerRadius:5.0];
    [self.MyTableView setSeparatorColor:[UIColor clearColor]];
    [self.MyTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    self.MyTableView.transform = CGAffineTransformMakeScale(1, -1); //in viewDidLoad

    cell.transform = CGAffineTransformMakeScale(1, -1);//in tableView:cellForRowAtIndexPath:

    [[cell contentView] addSubview:myRect];
    [[cell contentView] addSubview:imgim];
    [[cell contentView] addSubview:label];
    return cell;
}

İf点击发送按钮我重新加载数据,但它在旧数据上的重叠看起来像我的照片:

-(IBAction)btnTap:(UIButton *)sender {
    [arr1 insertObject:Textify.text atIndex:0];

    NSLog(@"%i", arr1.count);
    [MyTableView reloadData]; 
}

2 个答案:

答案 0 :(得分:1)

尝试清除单元格的contentView子视图,然后添加新的子视图。

以下是用于删除子视图的代码块,取自此post

if ([cell.contentView subviews]){
   for (UIView *subview in [cell.contentView subviews]) {
       [subview removeFromSuperview];
   }
}

答案 1 :(得分:0)

UITableViewCell将在重新加载时重复使用,您不应再次创建子视图以在cell.contentView上添加。尝试使用下面的代码

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault     reuseIdentifier:@"TeamCell"];
    UILabel *label = [[UILabel alloc] initWithFrame:someFrame];
    label.tag = 1000;
    [cell.contentView addSubview:label];
}
UILabel *label = [cell.contentView viewWithTag:1000];
label.text = @"something";