cellForRowAtIndexPath中的框不会更改

我想在某些情况下更改cellForRowAtIndexPath单元格内的x位置的view框架。 我已经使用了下面的代码。 但是它不会改变视图的x position框架。

 - (void)viewDidLoad { [super viewDidLoad]; UINib *nib = [UINib nibWithNibName:@"GoalDetailsCustomCardCell" bundle:nil]; [goalDetailsTableView registerNib:nib forCellReuseIdentifier:@"CardCell"]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { GoalDetailsTableViewCell *cell = [goalDetailsTableView dequeueReusableCellWithIdentifier:@"CardCell"]; if(cell == nil) { cell = [goalDetailsTableView dequeueReusableCellWithIdentifier:@"CardCell"]; } NSLog(@"%f", cell.cardDetails.frame.origin.x); [UIView animateWithDuration: 0.5 animations:^{ cell.cardDetails.frame = CGRectOffset(cell.cardDetails.frame, -322.0, 0.0); // CGRectOffset(cell.cardView.frame, -320.0, 0.0); cell.actionCardReminder.frame = CGRectOffset(cell.actionCardReminder.frame, -322.0, 0.0); }]; NSLog(@"%f", cell.cardDetails.frame.origin.x); return cell; } 

细胞中的观点如预期的那样移动,但不是消极的一面。 它仍然显示cardDetails视图而不是actionCardReminder视图。

注意:NSLog将视图框架显示为更改x位置,但视图不移动到-322.0位置。

由于iOS布局渲染过程,您的自定义UITableViewCell的layoutSubviews可能会多次调用。 在你的情况下,你可以改变GoalDetailsTableViewCell类的layoutSubviews方法中的项目框架。

 @implementation GoalDetailsTableViewCell -(void)layoutSubviews { [super layoutSubviews]; [UIView animateWithDuration: 0.5 animations:^{ cell.cardDetails.frame = CGRectOffset(cell.cardDetails.frame, -322.0, 0.0); // CGRectOffset(cell.cardView.frame, -320.0, 0.0); cell.actionCardReminder.frame = CGRectOffset(cell.actionCardReminder.frame, -322.0, 0.0); }]; NSLog(@"%f", cell.cardDetails.frame.origin.x); } @end 

不要更改cellForRowAtIndexPath的帧尝试在willDisplayingCell方法中更改它