简体   繁体   English

在 TableView 的特定单元格中阻止重新排序

[英]Block reordering in a specific cell of a TableView

I want to block a cell to be reordered.我想阻止要重新排序的单元格。

For example:例如:

I have a tableview with 4 rows but don't want that the first cell can be reordered.我有一个 4 行的 tableview,但不希望第一个单元格可以重新排序。

Is that possible?那可能吗?

I've tried to use:我试过使用:

if(indexPath.row == 0)
{
    [cell setEditing:NO animated:NO];
}

But doesn't work.但不起作用。

Thanks.谢谢。

I found the answer!我找到了答案!

You can use the method targetIndexPathForMoveFromRowAtIndexPath from UITableViewDelegate.您可以使用 UITableViewDelegate 中的targetIndexPathForMoveFromRowAtIndexPath方法。

-(NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath{
    if (proposedDestinationIndexPath.row == 0) {
        return sourceIndexPath;
    }
    return proposedDestinationIndexPath;

} }

Your UITableViewDataSource should implement -(BOOL)tableView:canMoveRowAtIndexPath: and return NO for indexPath.row == 0您的 UITableViewDataSource 应该实现-(BOOL)tableView:canMoveRowAtIndexPath:并为 indexPath.row == 0 返回 NO

- (BOOL)tableView:canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return (indexPath.row != 0);
}

UITableViewDataSource documentation UITableViewDataSource 文档

SWIFT 5 CODE: SWIFT 5 代码:

func tableView(_ tableView: UITableView, targetIndexPathForMoveFromRowAt sourceIndexPath: IndexPath, toProposedIndexPath proposedDestinationIndexPath: IndexPath) -> IndexPath {

    if proposedDestinationIndexPath.row == 0 {
        return sourceIndexPath
    }
    
    return proposedDestinationIndexPath
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM