Reloading custom UITableViewCell after reordering cells

后端 未结 6 1446
忘了有多久
忘了有多久 2021-02-15 16:04

I have UITableView which uses custom UITableViewCells. The cells can have one of three types of background images (set in each cell\'s .backgrou

6条回答
  •  粉色の甜心
    2021-02-15 16:43

    Write below code in your project and it should work as per expectations.

    - (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;
    }
    

提交回复
热议问题