重新排序单元格后重新加载自定义UITableViewCell

Sko*_*ota 8 iphone cocoa-touch uitableview uikit

我有 UITableView使用自定义UITableViewCell的.单元格可以具有三种类型的背景图像之一(在每个单元格的.backgroundView.image属性中设置):顶部,中间或底部.顶部和底部图像用于第一个和最后一个单元格,并且具有圆角(很像分组UITableView的单元格).其中的所有其他"中间"单元UITableView具有矩形中间背景图像.

我的问题是,在UITableView" 编辑"模式下重新排序单元格时,单元格不会根据新位置刷新不同的背景图像.例如,如果我将第一个单元格移动到表格的中间,它仍然保留其原始的"顶部"背景图像(带有圆角),并且出现在表格视图顶部的新单元格仍然具有其原始"中间"背景图片,这看起来有点奇怪.

我可以通过reloadData在表视图上执行此操作来解决这个问题,但这样做的问题是不能给出更改的优美动画.我注意到,当UITableView移动/拖动单元格时重新排序标准时,相关单元格上的背景图像会发生变化(甚至在移动的单元格被放入新位置之前),这看起来非常好.我想实现同样的目标.

为此,我实现了tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath每次在表视图中拖动行时调用的内容.在此我尝试了各种设置方法,backgroundView.image包括直接设置图像,然后明确告诉它使用重绘单元格和/或图像setNeedsLayout,setNeedsDisplay但似乎没有任何东西让细胞重绘.我已经NSLog'ged这些更改的结果,它们似乎正在提交到单元格,因为正确的值出现在NSLog,但单元格似乎没有在屏幕上更新.

如果有人能指出我做错了什么,或建议一个更好的方法来实现工作结果,我将非常感激.谢谢!

编辑:以下代码从赏金中提取并格式化,以便更容易消化:

UIImage *rowBackground;
UIImage *selectionBackground;
NSInteger sectionRows = [_tableView numberOfRowsInSection:[indexPath section]];
NSInteger row = [indexPath row];
if (row == 0 && row == sectionRows - 1)
{
    rowBackground = [UIImage imageNamed:@"field_title_bkg.png"];
    selectionBackground = [UIImage imageNamed:@"field_title_bkg.png"];
}
else if (row == 0)
{
    rowBackground = [UIImage imageNamed:@"table_row_top_bkg.png"];
    selectionBackground = [UIImage imageNamed:@"table_row_top_bkg.png"];
}
else if (row == sectionRows - 1)
{
    rowBackground = [UIImage imageNamed:@"table_bottom_row_bkg.png"];
    selectionBackground = [UIImage imageNamed:@"table_bottom_row_bkg.png"];
}
else
{
    rowBackground = [UIImage imageNamed:@"table_row_bkg.png"];
    selectionBackground = [UIImage imageNamed:@"table_row_bkg.png"];
}
UIImageView *rowBackGroundImageView = [[UIImageView alloc] initWithImage:rowBackground];
UIImageView *rowBackGroundSelectionImageView = [[UIImageView alloc] initWithImage:selectionBackground];
if (row != sectionRows - 1)
{
    UIImageView *sep = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cell_separator.png"]];
    [sep setFrame:CGRectMake(20, 56, 280, 1)];
    [rowBackGroundImageView addSubview:sep];
}
[cell setBackgroundView:rowBackGroundImageView];
[rowBackGroundImageView release];
Run Code Online (Sandbox Code Playgroud)

Apu*_*urv 5

在您的项目中编写以下代码,它应能按预期工作。

- (NSIndexPath *)tableView:(UITableView *)tableView
targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath
      toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
    NSInteger sectionRows = [tableView numberOfRowsInSection:[sourceIndexPath section]];

    UITableViewCell *sourceCell = [tableView cellForRowAtIndexPath:sourceIndexPath];
    UITableViewCell *destCell = [tableView cellForRowAtIndexPath:proposedDestinationIndexPath];

    if(sourceIndexPath.row == 0 && proposedDestinationIndexPath.row == 1)
    {
        ((UIImageView *)destCell.backgroundView).image = [UIImage imageNamed:@"table_row_top_bkg.png"];

        if(proposedDestinationIndexPath.row == sectionRows - 1)
            ((UIImageView *)sourceCell.backgroundView).image = [UIImage imageNamed:@"table_bottom_row_bkg.png"];
        else
            ((UIImageView *)sourceCell.backgroundView).image = [UIImage imageNamed:@"table_row_bkg.png"];
    }

    else if(sourceIndexPath.row == sectionRows - 1 && proposedDestinationIndexPath.row == sectionRows - 2)
    {
        ((UIImageView *)destCell.backgroundView).image = [UIImage imageNamed:@"table_bottom_row_bkg.png"];

        if(proposedDestinationIndexPath.row == 0)
            ((UIImageView *)sourceCell.backgroundView).image = [UIImage imageNamed:@"table_row_top_bkg.png"];
        else
            ((UIImageView *)sourceCell.backgroundView).image = [UIImage imageNamed:@"table_row_bkg.png"];
    }

    else if(proposedDestinationIndexPath.row == 0)
    {
        ((UIImageView *)sourceCell.backgroundView).image = [UIImage imageNamed:@"table_row_top_bkg.png"];

        destCell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:proposedDestinationIndexPath.section]];
        if(sectionRows == 2)
            ((UIImageView *)destCell.backgroundView).image = [UIImage imageNamed:@"table_bottom_row_bkg.png"];
        else
            ((UIImageView *)destCell.backgroundView).image = [UIImage imageNamed:@"table_row_bkg.png"];
    }

    else if(proposedDestinationIndexPath.row == sectionRows - 1)
    {
        ((UIImageView *)sourceCell.backgroundView).image = [UIImage imageNamed:@"table_bottom_row_bkg.png"];

        destCell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:sectionRows - 1 inSection:proposedDestinationIndexPath.section]];
        if(sectionRows == 2)
            ((UIImageView *)destCell.backgroundView).image = [UIImage imageNamed:@"table_row_top_bkg.png"];
        else
            ((UIImageView *)destCell.backgroundView).image = [UIImage imageNamed:@"table_row_bkg.png"];
    }

    return proposedDestinationIndexPath;
}
Run Code Online (Sandbox Code Playgroud)


Rog*_*Rog 0

我自己还没有尝试过,但为什么不在 cellForRowAtIndexPath 方法上测试您的 indexPath.row 值并为第一行和最后一行提供不同的 backgroundView.image 值?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
Run Code Online (Sandbox Code Playgroud)

我希望这有帮助。干杯,罗格