在UITableView中移动单元格时,如何使单元格保持静止?

LDY*_*LDY 4 iphone objective-c uitableview ios

我有一个占据景点的人员名单.我希望用户能够将这些人重新安排到不同的位置,但是,有些地方是不受限制的.我认为使用UITableView的重新排列功能最容易实现.但是,我无法弄清楚如何让不可用的斑点保持静止.

例如,我想将Activia Boulanger移到第5位.灰色细胞应该是不可移动的细胞.

开始观点:

开始

UITableView自动执行的操作:

UITableView的功能

我想要UITableView做什么:

我想要的是

设置tableView:canMoveRowAtIndexPath:似乎只是阻止您移动细胞,但不会阻止细胞移动以响应其他细胞的运动.

任何帮助将不胜感激.谢谢


更新:下面是一些示例代码,其中包含问题的设置.我没有将我的努力包括在一个解决方案中,因为它们都失败了并且会使事情变得混乱.

#import "LDYViewController.h"

static NSString * unmoveableCellId = @"NoMove";
static NSString * moveableCellId = @"OkMove";

@implementation LDYViewController
@synthesize tableView;
@synthesize peopleList;

- (void)viewDidLoad
{
    [super viewDidLoad];

    peopleList = [[NSMutableArray alloc] initWithObjects:
                  @"Belinda Boomer", @"Activia Boulanger", @"Arnold Carter", [NSNull null], @"Attila Creighton", [NSNull null], @"Bruce Cleary", [NSNull null], nil];
    [tableView setEditing:YES];
    // Do any additional setup after loading the view, typically from a nib.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return peopleList.count;
}

- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell;

    if([peopleList objectAtIndex:indexPath.row] == [NSNull null]) {
        cell = [tableView dequeueReusableCellWithIdentifier:unmoveableCellId];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:unmoveableCellId];
            cell.userInteractionEnabled = NO;
            cell.contentView.backgroundColor = [UIColor grayColor];
        }
    } else {
        cell = [tableView dequeueReusableCellWithIdentifier:moveableCellId];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:moveableCellId];
            NSString * name = [peopleList objectAtIndex:indexPath.row];
            cell.textLabel.text = name;
        }        
    }

    return cell;
}

- (void) tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
}

- (NSIndexPath *)tableView:(UITableView *)tableView
targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath
       toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath {
    return proposedDestinationIndexPath;
}

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    if([peopleList objectAtIndex:indexPath.row] == [NSNull null]) {
        return NO;
    }

    return YES;
}    
@end
Run Code Online (Sandbox Code Playgroud)

Son*_*yen 8

试试这个:

- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath*)sourceIndexPath
       toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
Run Code Online (Sandbox Code Playgroud)

您可以检查proposedDestinationIndexPath,如果您不想将行移动到它,则可以返回sourceIndexPath然后将行移回,否则返回proposedDestinationIndexPath.


dan*_*anh 2

对模型执行正常的重新排列逻辑,然后完成后将空值放回您想要的位置。发布的代码不包含模型,因此这里有一个合理的模型来说明:

// always leave our array with a null at indexes 3,5,7
// i'm sure you have better logic about where nulls go, but just to illustrate
- (void)addNulls {

    NSMutableArray *answer = [NSMutableArray array];
    for (id object in self.peopleList) {
        if (![object isKindOfClass:[NSNull self]]) {
            [answer addObject:object];
        }
    }

    assert(answer.count >= 4);
    [answer insertObject:[NSNull null] atIndex:3];
    [answer insertObject:[NSNull null] atIndex:5];
    [answer insertObject:[NSNull null] atIndex:7];
    self.peopleList = answer;
}

// build a pretend model with nulls
- (NSMutableArray *)peopleList {

    if (!_peopleList) {
        _peopleList = [NSMutableArray array];
        [_peopleList addObject:@"Moe"];
        [_peopleList addObject:@"Larry"];
        [_peopleList addObject:@"Curly"];
        [_peopleList addObject:[NSNull null]];
        [_peopleList addObject:@"Groucho"];
        [_peopleList addObject:[NSNull null]];
        [_peopleList addObject:@"Chico"];
        [_peopleList addObject:[NSNull null]];
        [_peopleList addObject:@"Harpo"];
    }
    return _peopleList;
}



// normal rearrange logic, then fix your array up
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {

    // reorder the list with the typical logic, ignoring nulls
    NSString *stringToMove = [self.peopleList objectAtIndex:fromIndexPath.row];
    [self.peopleList removeObjectAtIndex:fromIndexPath.row];
    [self.peopleList insertObject:stringToMove atIndex:toIndexPath.row];

    // now put nulls into the positions you want them
    [self addNulls];
    [self.tableView reloadData];
}
Run Code Online (Sandbox Code Playgroud)