One Quickie


Controlling UITableView row rearranging (UITableView->General)
Say that you want to let the user rearrange tableview rows except for the first and the last, which can't move. First, inhibit the drawing of the little rearrange indicator on the first and last rows:
- (BOOL) tableView: (UITableView *) tableView
  canMoveRowAtIndexPath: (NSIndexPath *) indexPath {

    if (indexPath.row == 0) return NO;
    else if (indexPath.row == _cues.count - 1) return NO;
    else return YES;

} // canMoveRowAtIndexPath
And then prevent rows from being dragged to the first and last position:
- (NSIndexPath *) tableView: (UITableView *) tableView
targetIndexPathForMoveFromRowAtIndexPath: (NSIndexPath *) source
        toProposedIndexPath: (NSIndexPath *) destination {

    if (destination.row > 0 && destination.row < _cues.count - 1) {
        // No violence necessary.
        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: _cues.count - 2
                                 inSection: 0];
    }

    return indexPath;

} // targetIndexPathForMoveFromRowAtIndexPathPleaseOHaiThanksForMovingKthxBai



borkware home | products | miniblog | rants | quickies | cocoaheads
Advanced Mac OS X Programming book

webmonster@borkware.com