I have UITableView
which uses custom UITableViewCell
s. The cells can have one of three types of background images (set in each cell\'s .backgrou
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;
}