5

我有一个占据位置的人的名单。我希望用户能够将这些人重新排列到不同的位置,但是,有些位置是不受限制的。我认为使用 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
4

5 回答 5

9

尝试这个:

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

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

于 2012-04-18T04:14:08.763 回答
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];
}
于 2012-04-19T02:35:41.827 回答
2

我遇到了类似的问题并解决了。

创建 2 部分并将所有灰色行放在第 2 部分中。

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

if (proposedDestinationIndexPath.section == 1)
    return sourceIndexPath;
else
    return proposedDestinationIndexPath;

}

于 2012-04-18T06:03:49.263 回答
0

您可以使用以下方法执行此操作:

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath{

在此方法中,您需要检查 from indexIndexPath 和 toIndexPath 以检查用户是否移动到无效位置。修改 tableview 的数据源以将单元格放回或将其移动到正确的位置。

于 2012-04-18T02:15:44.287 回答
0

快速实施

class TableViewMovable: UITableView, UITableViewDelegate, UITableViewDataSource {

var arrDataSource = [[String:Int]]()

override func awakeFromNib() {
    super.awakeFromNib()
    commonInit()
}

func commonInit(){

    dataSource = self
    delegate = self
    isEditing = true

    register(UITableViewCell.self, forCellReuseIdentifier: "cell")
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return arrDataSource.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if let cell = tableView.dequeueReusableCell(withIdentifier: "cell") {
        cell.textLabel?.text = arrDataSource[indexPath.row].keys.first!
        let val = arrDataSource[indexPath.row].values.first!
        cell.backgroundColor = val % 2 == 0 ? UIColor.gray.withAlphaComponent(0.5) : UIColor.lightGray.withAlphaComponent(0.5)
        return cell
    }else{
        return UITableViewCell()
    }
}

func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
    return .none
}
func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool {
    return false
}

func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
    let movedObject = arrDataSource[sourceIndexPath.row]
    arrDataSource.remove(at: sourceIndexPath.row)
    arrDataSource.insert(movedObject, at: destinationIndexPath.row)
    debugPrint("\(sourceIndexPath.row) => \(destinationIndexPath.row)")

}

func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
    return true
}

}

希望这可能对某人有所帮助。

于 2018-07-24T10:15:54.867 回答