您的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];
}];
}
}
確實。最後一個答案不需要幀處理,這在處理自動佈局時不是很健壯。 – Abizern
非常好的答案謝謝,我想約束,但不知道我能夠建立一個出口約束,現在我學會了:) – user1396236
@ user1396236,我已經添加到我的答案,顯示我如何做包括取消檢查單元格,如果你再次點擊它們。 – rdelmar