<p>
在UITableView中,我们可以使用- (BOOL) tableView: (UITableView *) tableView canMoveRowAtIndexPath: (NSIndexPath *) indexPath方法来禁止移动某一行。下面的例子是禁止移动最后一行。但是,虽然不能移动最后一行,却可以将其他行移动至最后一行下方。
</p>
<code>
-
(BOOL)tableView:(UITableView *)tableView
canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
NSArray *items = [[BNRItemStore sharedStore] allItems];if (indexPath.row + 1 == [items count]) {
return NO;
}
return YES;
}
</code>
<p>我们可以使用- (NSIndexPath *) tableView: (UITableView *) tableView
targetIndexPathForMoveFromRowAtIndexPath: (NSIndexPath *) source
toProposedIndexPath: (NSIndexPath *) destination 方法来彻底禁止移动最后一行,使最后一行始终位于试图的底部。例子如下:</p>
<code>
//prevent rows from being dragged to the last position:
-(NSIndexPath *) tableView: (UITableView *) tableView
targetIndexPathForMoveFromRowAtIndexPath: (NSIndexPath *) source
toProposedIndexPath: (NSIndexPath *) destination
{
NSArray *items = [[BNRItemStore sharedStore] allItems];
if (destination.row < [items count] - 1) {
return destination;
}
NSIndexPath *indexPath = nil;
// If your table can have <= 2 items, you might want to robusticize the index math.
if (destination.row == 0) {
indexPath = [NSIndexPath indexPathForRow: 1 inSection: 0];
} else {
indexPath = [NSIndexPath indexPathForRow: items.count - 2
inSection: 0];
}
return indexPath;
}
</code>