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

标签 iphone objective-c ios uitableview

我有一个占据位置的人的列表。我希望用户能够将这些人重新安排到不同的位置,但是,有些位置是禁止使用的。我认为使用 UITableView 的重新排列功能最容易实现这一点。但是,我不知道如何让我的不可用点保持静止。

例如,我想将 Activia Boulanger 移动到点 5。灰色单元格应该是不可移动的单元格。

开始 View :

Beginning

UITableView 自动做什么:

What UITableView does

我希望 UITableView 做什么:

What I want

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

最佳答案

试试这个:

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

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

关于iphone - 在 UITableView 中移动单元格时,如何使单元格保持静止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10201794/

相关文章:

ios - 如何修改 IBOutletCollection 中标签的颜色

ios - 创建播放声音的按钮,收到 NSException 错误

iphone - 电影下载器应用程序如何在 iOS 中运行?

ios - 我们应该将 pod 文件提交到版本控制系统(GIT 还是 SVN)

ios - 如何在ios中管理tableview内的按钮?

iphone - 在 iOS 7 中移动状态栏

objective-c - 刷新 UIWebView

iphone - 带有 iPhone SDK 的 Facebook Graph API

iphone - 使用 NSPredicate 对 NSArray 进行排序,然后存储在一个新数组中

objective-c - 批处理核心数据获取结果