0

Xcode 6.4 IOS 8.4, If I slide the cells I custom, some cells with be confusion,but slide slowly the cells with be normal!I think the reason is the reuse of cell.but I don't know how to solve it!

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerNib:[UINib nibWithNibName:@"MyTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    self.tableView.estimatedRowHeight = 200;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.allowsSelection = NO;
    self.tableData = @[@"1\n2\n3\n4\n5\n6", @"123456789012345678901234567890", @"1\n2", @"1\n2\n3", @"1", @"1\n2\n3\n4\n5\n6", @"123456789012345678901234567890", @"1\n2", @"1\n2\n3", @"1", @"1\n2\n3",@"1\n2\n3"];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.tableData.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    cell.textView.text = self.tableData[indexPath.row];

    cell.isShowView = YES;
    cell.button.hidden = NO;
    cell.intextView.hidden = NO;
    if(indexPath.row % 2 == 0) {
        cell.isShowView = NO;
        cell.button.hidden = YES;
        cell.intextView.hidden = YES;
    }

    [cell setNeedsUpdateConstraints];
    [cell updateConstraintsIfNeeded];

    return cell;

}

customCell.m

- (void)awakeFromNib {
    self.isShowView = YES;
}

-(void)updateConstraints {

    if(self.isShowView == NO) {
        [self.content mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@0);
        }];
        NSLog(@"self.isShowView == NO");
    }

    [super updateConstraints];
}
Peppo
  • 1,107
  • 1
  • 12
  • 19
imokoi
  • 19
  • 5

2 Answers2

0

You need to add following code in cellForRowAtIndexPath:

static NSString *simpleTableIdentifier = @"infoTableCell";

     MyTableViewCell *cell = (MyTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
     if (cell == nil)
     {
         NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyTableViewCell" owner:self options:nil];
         cell = [nib objectAtIndex:0];
     }
Idrees Ashraf
  • 1,363
  • 21
  • 38
0

I find the reason for the question! In my custom cell file,change the method updateConstraints

if I need to show some view I should keep the view have no height Constrains, if I needn't to show some View I should add the height Constrains to the view,and the value must be 0!

-(void)updateConstraints {
    //remove Constraints
    [self.height uninstall];

    //add Constraints
    [self.content mas_makeConstraints:^(MASConstraintMaker *make) {
       self.height = make.height.equalTo(@60);//any number
    }];

    if(self.isShowView == NO) {

        //change Height Constrains to be 0
        [self.content mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@0);

        }];
    }
    else {

        //remove Constraints
        [self.height uninstall];
    }

    [super updateConstraints];
}
imokoi
  • 19
  • 5