UITableViewCell - overlapping with previous cells contents

后端 未结 5 1444
名媛妹妹
名媛妹妹 2021-02-08 15:22

I have this wierd problem with my table

  1. i Have about 20 cells to display
  2. Each cell is about 84px in height
  3. When i click no the cell, i have set a
相关标签:
5条回答
  • 2021-02-08 15:31

    I know this is a bit late, but I had a similar issue where UILabels created for a cell were still part of the cell when it was reused. So each successive update of the tableview created another UILabel on top of the existing one. I moved the creation of the Labels into the if condition as below and it resolved my issue. Hope it helps someone else. Also note no release as I am using ARC.

        static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
        if (cell == nil) 
    
        {
    
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    
        cityText = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 20)];
        cityText.font = [UIFont fontWithName:@"Arial" size:20];
        cityText.textAlignment = UITextAlignmentLeft; 
        cityText.backgroundColor = [UIColor clearColor];
    
        regionText = [[UILabel alloc] initWithFrame:CGRectMake(10, 40, 100, 20)];
        regionText.font = [UIFont fontWithName:@"Arial" size:20];
        regionText.textAlignment = UITextAlignmentLeft; 
        regionText.backgroundColor = [UIColor clearColor];
    
        }
    
    0 讨论(0)
  • 2021-02-08 15:34

    I also had the same issue.I also had composed tableView with labels.And when I scroll it down the contents got overlapped.But it got solved simply by editing one statement in cellForRowAtIndexPath.

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
    

    I think this will solve your problem.

    0 讨论(0)
  • 2021-02-08 15:37

    I figured it out with some trial and error; if this can help some one

    In cellforRowAtIndexPath i tried to clear all the cells subview before drawing the cell

    //TRY TO REMOVE ALL CONTENT
        for(UIView *view in cell.contentView.subviews){
            if ([view isKindOfClass:[UIView class]]) {
                [view removeFromSuperview];
            }
        }
    

    I would be happy if someone can point me to some easier way

    Thanks

    0 讨论(0)
  • 2021-02-08 15:45

    You can use

    [[[cell contentView] subviews] makeObjectsPerformSelector: @selector(removeFromSuperview)];
    

    If the cell in not nil, the above code will reduce the time for using the for loop similar to this

    for(UIView *eachView in [cell subviews])
        [eachView removeFromSuperview];
    
    0 讨论(0)
  • 2021-02-08 15:49

    Setting nil to label text on UITableViewCell subclass 's method prepareForReuse() solved my problem -

      override func prepareForReuse() {
        super.prepareForReuse()
        self.label1.text = nil
        self.label2.text = nil
        ....
    }
    

    Shared if it helps anyone!

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