开发者

iPhone UITableViewController move only specific rows in a table

I have implemented a standard UITableViewController. I have made only some rows in the table to be moveable, ie. canMoveRowAtIndexPath returns true only for some indexPaths (for rows in the first part of the table). I would like to allow exchange of only those rows that are moveable, ie. for which canMoveRowAtIndexPath returns true.

Eg. my table looks like this:

Row 1
Row 2
Row 开发者_StackOverflow社区3
Row 4
Row 5

Rows 1, 2, 3 are moveable. So I want to implement the following behavior: row 1 can be exchanged only with rows 2 or 3. Similary, row 2 can be exchanged only with rows 1 and 3. This is one possible layout:

Row 3
Row 1
Row 2
Row 4
Row 5

However, I don't want this to happen:

Row 3
Row 5
Row 2
Row 4
Row 1

How to achieve this?


Keep in mind that it's actually only moving one row, not exchanging.

What you want is to implement tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath

See How to limit UITableView row reordering to a section


You can control the movement within section and between section.

- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath

{

// Do not allow any movement between section
if ( sourceIndexPath.section != proposedDestinationIndexPath.section)
    return sourceIndexPath;
// You can even control the movement of specific row within a section. e.g last row in a     Section

// Check if we have selected the last row in section
if (   sourceIndexPath.row < sourceIndexPath.length) {
    return proposedDestinationIndexPath;
} else {
    return sourceIndexPath;
}

// you can use this approach or logic to check the index of the rows in section and return either sourceIndexPath or targetIndexPath

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜