您的 playerRatingLabel 应该对单元格的右边缘有一个约束。您的自定义单元格需要为该约束创建一个 IBOutlet。然后在单元格点击上,为该约束的常量参数设置动画(我在示例中将插座称为 rightCon):
[UIView animateWithDuration:1.0 animations:^{
cell.rightCon.constant = 30; // change this value to meet your needs
[cell layoutIfNeeded];
}];
这是我用来执行此操作的完整实现。我的自定义单元格有两个标签,当您单击一个单元格并添加复选标记时,我会为右侧的标签设置动画。我创建了一个属性 selectedPaths (一个可变数组)来跟踪检查的单元格。如果单击已选中的单元格,它将取消选中它,并将标签动画返回到其原始位置。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
RDCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
cell.leftLabel.text = self.theData[indexPath.row];
cell.accessoryType = ([self.selectedPaths containsObject:indexPath])? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
cell.rightCon.constant = ([self.selectedPaths containsObject:indexPath])? 40 : 8;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
RDCell *cell = (RDCell *)[tableView cellForRowAtIndexPath:indexPath];
if (! [self.selectedPaths containsObject:indexPath]) {
[self.selectedPaths addObject:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[UIView animateWithDuration:.3 animations:^{
cell.rightCon.constant = 40;
[cell layoutIfNeeded];
}];
}else{
[self.selectedPaths removeObject:indexPath];
cell.accessoryType = UITableViewCellAccessoryNone;
[UIView animateWithDuration:.3 animations:^{
cell.rightCon.constant = 8;
[cell layoutIfNeeded];
}];
}
}