<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>