ReusableCellWithIdentifier issue in cellForRowAtIndexPath

前端 未结 2 993
有刺的猬
有刺的猬 2020-12-21 11:41

In my app I have one imageView and one UILabel. For ImageView I have assigned asynch Image to it and it works fine, But the first and Last row\'s Labels get overlapped when

相关标签:
2条回答
  • 2020-12-21 12:22

    You're adding nameLabel to the cell whether it was freshly created or reused. So yeah, every time you scroll a reused cell into view, it's going to pile on another label.

    0 讨论(0)
  • 2020-12-21 12:27

    for every cell use seprate identifier something like below

     NSString *cellIdentifier = [NSString stringWithFormat:@"cell%i",indexpath.row];
    

    or just dont use the reuseability simply make cell and data into them

    -(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [[UITableViewCell alloc]init];
    UIButton *favBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    favBtn.frame = CGRectMake(0, 0, 50, 50);
    favBtn.backgroundColor = [UIColor clearColor];
    favBtn.showsTouchWhenHighlighted = YES;
    favBtn.tag = indexPath.row;
    
    UIButton *arrowBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    arrowBtn.frame = CGRectMake(280, 26, 25, 25);
    arrowBtn.backgroundColor = [UIColor clearColor];
    arrowBtn.showsTouchWhenHighlighted = YES;
    [arrowBtn setImage:[UIImage imageNamed:@"arrow.png"] forState:UIControlStateNormal];
    
    UILabel *date = [[UILabel alloc]initWithFrame:CGRectMake(55, 26, 250, 25)];
    date.font = [UIFont fontWithName:[[UIFont familyNames] objectAtIndex:0]  size:15];
    date.text = ann.title;
    date.textColor = [UIColor blackColor];
    date.backgroundColor = [UIColor clearColor];
    
    [cell.contentView addSubview:favBtn];
    [cell.contentView addSubview:date];
    [cell.contentView addSubview:arrowBtn];
    
    
        return cell;
    }
    

    i am not an expert but the second one worked for me pretty well hope hepls u as well

    0 讨论(0)
提交回复
热议问题