我创建了自己的自定义分组 UITableView。这是一个表格,其中顶部单元格使用“top.png”作为背景,中间单元格“middle.png”和底部单元格“bottom.png”。
我正在使用
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
在重新排序期间拖动单元格时更改单元格的背景图像。但是,我似乎无法找到一个简单的解决方案。无论我如何尝试,我总是会得到一个非常复杂的解决方案,其中包含很多 if-cases,而且感觉非常困惑,过于复杂,就像我没有涵盖所有情况。对于我正在监督的这个问题,有一个简单的解决方案吗?
我找到了这个问题的“解决方案”here和 here 。但它们并不完整,并不适用于所有情况。
应该处理但上述两个解决方案无法处理的特殊情况:
- 移动行,然后返回原始位置
- 在行周围快速移动一行,向下滚动表格 View
最佳答案
我也遇到了同样的问题,我想我已经找到了解决方案。根据我的测试,它在一个部分中运行良好(使用两个到大约十几个单元进行测试),但如果您在不同部分之间拖动单元,则需要进行一些修改。我还没有进行这些修改,因为我不需要该功能。
此方法假设您已经实现了委托(delegate)方法
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
设置单元格的初始背景并确保其正常工作。
此方法将在单元格移动时更新其新位置
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath {
// Get the index paths of the cells above and below our current target cell
NSIndexPath *higherPath = [NSIndexPath indexPathForRow:proposedDestinationIndexPath.row - 1 inSection:proposedDestinationIndexPath.section];
NSIndexPath *lowerPath = [NSIndexPath indexPathForRow:proposedDestinationIndexPath.row + 1 inSection:proposedDestinationIndexPath.section];
// Update our source cell to the appropriate image for its current position
[self tableView:self.tableView willDisplayCell:[tableView cellForRowAtIndexPath:sourceIndexPath] forRowAtIndexPath:proposedDestinationIndexPath];
if (sourceIndexPath.row > proposedDestinationIndexPath.row) {
// Source cell is below the target
// Update the cell that was in our target position
[self tableView:self.tableView willDisplayCell:[tableView cellForRowAtIndexPath:proposedDestinationIndexPath] forRowAtIndexPath:lowerPath];
// Ensure that any cell above our new target is refreshed if the cell is dragged back down the list
[self tableView:self.tableView willDisplayCell:[tableView cellForRowAtIndexPath:higherPath] forRowAtIndexPath:higherPath];
} else if (sourceIndexPath.row < proposedDestinationIndexPath.row) {
// Source cell is above the target
// Update the cell that was in our target position
[self tableView:self.tableView willDisplayCell:[tableView cellForRowAtIndexPath:proposedDestinationIndexPath] forRowAtIndexPath:higherPath];
// Ensure that any cell below our new target is refreshed if the cell is dragged back up the list
[self tableView:self.tableView willDisplayCell:[tableView cellForRowAtIndexPath:lowerPath] forRowAtIndexPath:lowerPath];
} else {
// Source cell is the same as the target - cell is back in its original position
// Update any cells below and above the position
[self tableView:self.tableView willDisplayCell:[tableView cellForRowAtIndexPath:higherPath] forRowAtIndexPath:higherPath];
[self tableView:self.tableView willDisplayCell:[tableView cellForRowAtIndexPath:lowerPath] forRowAtIndexPath:lowerPath];
}
return proposedDestinationIndexPath;
}
我在患流感时想出了这个,所以我在解释中可能犯了一些愚蠢的错误,但我非常有信心这应该可靠地工作,除了单个部分的限制。
关于ios - 重新排序自定义分组 UITableView 时单元格的背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15596738/