我正在平移UITableViewCell contentView,如下面的代碼。我想知道正在移動的UITableViewCell contentView背後存在什麼視圖?平移UITableViewCell時顯示的是什麼視圖?
我問的原因是因爲我想改變這個「透視圖」的顏色。我想在單元格被刷過時自定義每個tableViewCell下的顏色。例如,如果您左移第三行,則會在單元格後面看到藍色。如果你離開第四排,你會看到單元格後面的綠色。
- (void)panGestureRecognizer:(UIPanGestureRecognizer *)recognizer {
//...
if ((recognizer.state == UIGestureRecognizerStateBegan
|| recognizer.state == UIGestureRecognizerStateChanged)
&& [recognizer numberOfTouches] > 0) {
CGPoint location1 = [recognizer locationOfTouch:0 inView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location1];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
CGPoint translation = [recognizer translationInView:self.tableView];
cell.contentView.frame = CGRectOffset(cell.contentView.bounds, translation.x, 0);
}
//...
}
我試過的一件事是創建一個UIView並將其添加到contentView後面的單元格中。這有效,但不能令我滿意。如果您將代碼添加到使用UITableViewRowAnimationLeft進行動畫刪除的動畫中,背景的顏色將與單元一起向左移動。這是有道理的,因爲當動畫移出單元格以刪除它時,我創建的背景視圖與整個單元格一起移動。我想要的行爲是讓背景顏色實際位於單元格的後面,因此當單元格在刪除動畫中移動時它不會移動。
以下代碼是我如何將一個背景視圖添加到單元格並將其放置在contentView下方。
[cell.contentView setBackgroundColor:[UIColor whiteColor]];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, self.tableView.rowHeight)];
view.backgroundColor = [UIColor redColor];
[cell addSubview:view];
[cell bringSubviewToFront:cell.contentView];