在表视图部分中交换单元格

时间:2012-09-23 15:13:01

标签: ios uitableview

我有一个包含两个自定义单元格的表格视图部分。当其他一些动作触发时,我想交换该部分中的单元格。

enter image description here

顶部单元格应滑到底部,底部单元格应滑到顶部。

我当前的代码导致空白单元格区域

enter image description here

cellForRowAtIndexPath返回时:

    // flag is set to YES when reloadSections is called 
    if (flag) {             

        if (self.currentAddressingMode == AddressingModeToSelf) {

                if (indexPath.row == 0) {
                    return self.outNumberCell;
                }
                else if (indexPath.row == 1) {
                    return self.inNumberCell;
                }

            }
            else if (self.currentAddressingMode == AddressingModeToCounterpart) {

                if (indexPath.row == 0) {
                    return self.inNumberCell;
                }
                else if (indexPath.row == 1) {
                    return self.outNumberCell;
                }
            }
        }

有解决方案吗?

3 个答案:

答案 0 :(得分:0)

如果需要,您可以在(UICell*) cellForRowAtIndexPath中返回所需的单元格,然后reloadSections动画。

答案 1 :(得分:0)

为了实现这一点,您需要在dataSource中自己更改数据的顺序。然后你可以这样做reloadData:

[_ tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];

答案 2 :(得分:0)

我通过在cellForRowAtIndexPath:方法中创建自定义单元格(通过向其添加子视图)而不是子类化来解决这个问题。对于动画细胞交换,我使用UITableView的{​​{1}}方法(iOS 5 SDK)。其他相关方法也可以正常工作(它取决于您需要的交换动画)。我发布到这个问题的代码块保持完全不变。