【问题标题】:If I slide the cells I customed, some cells with be confusion,but slide slowly the cells with be normal如果我滑动自定义的单元格,有些单元格会混乱,但慢慢滑动正常的单元格
【发布时间】:2015-09-17 11:09:46
【问题描述】:

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;

}

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];
}

【问题讨论】:

  • 请原谅我无法发布图片

标签: ios objective-c xcode6.4


【解决方案1】:

您需要在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];
     }

【讨论】:

  • 那没用!,同样的结果
  • 您是否在 Xib 文件中设置单元标识符?
  • 我找到问题的原因了!细胞重复使用!你可以看到我的答案!
【解决方案2】:

我找到了问题的原因!在我的自定义单元格文件中,更改方法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];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-19
    • 1970-01-01
    • 2021-09-18
    • 1970-01-01
    • 1970-01-01
    • 2014-03-21
    相关资源
    最近更新 更多