0

Xcode 6.4 IOS 8.4,如果我滑动自定义的单元格,有些单元格会混乱,但慢慢滑动单元格会正常!我认为原因是单元格的重用。但我不知道如何解决它!

- (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;

}

自定义Cell.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];
}
4

2 回答 2

0

您需要在中添加以下代码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];
     }
于 2015-09-17T12:59:14.970 回答
0

我找到问题的原因了!在我的自定义单元格文件中,更改方法updateConstraints

如果我需要显示一些视图我应该保持视图没有高度约束,如果我不需要显示一些视图我应该将高度约束添加到视图中,并且值必须为 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];
}
于 2015-09-19T01:57:26.157 回答