如果我滑动我自定义的单元格,有些单元格会混乱,但慢慢滑动单元格是正常的
If I slide the cells I customed, some cells with be confusion,but slide slowly the cells with be normal
Xcode 6.4 IOS 8.4、滑动自定义的单元格,有些单元格会乱,慢慢滑动的单元格是正常的!我想原因是重用了cell.but不知道怎么解决!
- (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];
}
您需要在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];
}
我找到问题的原因了!在我的自定义单元格文件中,更改方法 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];
}
Xcode 6.4 IOS 8.4、滑动自定义的单元格,有些单元格会乱,慢慢滑动的单元格是正常的!我想原因是重用了cell.but不知道怎么解决!
- (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];
}
您需要在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];
}
我找到问题的原因了!在我的自定义单元格文件中,更改方法 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];
}