gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 05:04:09 25 4
gpt4 key购买 nike

我有一个占据位置的人的列表。我希望用户能够将这些人重新安排到不同的位置,但是,有些位置是禁止使用的。我认为使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com