我希望 SetEdit 始终设置为 YES,以便用户始终可以对行重新排序。但是,我不想缩进空间



我有一个允许用户对行重新排序的场景。我在viewDidLoad中调用[self.tableView setEdit:YES]。

这是我还有什么...

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    BOOL canEdit = NO;
    if (indexPath.section == 0)
        canEdit = NO;
    else if (indexPath.section == 1)
        canEdit = YES;
    return canEdit;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCellEditingStyle style = UITableViewCellEditingStyleNone;
    if (indexPath.section == 0)
        style = UITableViewCellEditingStyleNone;
    else if (indexPath.section == 1)
        style = UITableViewCellEditingStyleNone;
    return style;
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1)
        return YES;
    return NO;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    FFFPlayer *data = [self.myData objectAtIndex:sourceIndexPath.row];
    [self.myData removeObjectAtIndex:sourceIndexPath.row];
    [self.myData data atIndex:destinationIndexPath.row];
}

我只希望用户能够对第 1 节中的行重新排序。

我有 2 个问题。

1)重新排序控件一直在行上,并且可以重新排序...但该行上的所有 UILabel 数据都被缩进,以便为不存在的"删除/插入"按钮腾出空间。尽管使用了UITableViewCellEditStyleNone,但所有内容都保持缩进。我该如何解决/解决此问题?我希望行始终保持可重新排序,但没有缩进。

2)现在我可以从第1节中取一行,然后重新排序并将其移动到第0节。我该如何防止这种情况。我只希望第 1 节中的行在其自己的部分周围移动。

谢谢!

对于问题 1,您执行以下操作:

- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
    return NO;
}

您的editingStyle...方法可以简单地是:

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleNone;
}

canEdit...方法可以简单地是:

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return indexPath.section == 1;
}

对于问题 2,您需要实现以下内容:

- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath {
    if (proposedDestinationIndexPath.section == sourceIndexPath.section) {
        return proposedDestinationIndexPath;
    } else {
        return [NSIndexPath indexPathForRow:0 inSection:1];
    }
}

该代码假设您只能移动第 1 部分中的行,并且只有 2 个部分。

相关内容

最新更新