UITableView - 不同部分的多选和单选

时间:2015-08-06 16:01:09

标签: ios objective-c uitableview

我有一个UITableView,有两个不同的部分。在一个部分中有多个选择而在另一个部分中有单个选择是否可行?如果我使用:

self.tableView.allowsMultipleSelection = YES;

它会影响整个表格。

7 个答案:

答案 0 :(得分:2)

我将公开我自己的解决方案,这与您发布的解决方案几乎相同。我认为这有点好,因为我手动管理选定的行,以便稍后我可以使用tableView.indexPathsForSelectedRows属性。

它是用Swift 2编写的。

解决方案

我首先声明一个NSIndexPath,它将存储先前选择的项目

var indexPathOfPreviouslySelectedRow: NSIndexPath?

然后我使用这些tableView委托函数(我的单选部分为0,我的多选为1)

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    switch indexPath.section {
    case 0:
        if let previousIndexPath = indexPathOfSelectedRowPaidBy {
            tableView.deselectRowAtIndexPath(previousIndexPath, animated: false)
            tableView.cellForRowAtIndexPath(previousIndexPath)?.accessoryType = UITableViewCellAccessoryType.None
        }
        indexPathOfSelectedRowPaidBy = indexPath
        tableView.cellForRowAtIndexPath(indexPath)?.accessoryType = UITableViewCellAccessoryType.Checkmark

    case 1:
        tableView.cellForRowAtIndexPath(indexPath)?.accessoryType = UITableViewCellAccessoryType.Checkmark

    default:
        break
    }


}

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
    switch indexPath.section {
    case 0:
        if let previousIndexPath = indexPathOfSelectedRowPaidBy {
            tableView.deselectRowAtIndexPath(previousIndexPath, animated: false)
            tableView.cellForRowAtIndexPath(previousIndexPath)?.accessoryType = UITableViewCellAccessoryType.None
        }
        indexPathOfSelectedRowPaidBy = nil

    case 1:
        tableView.cellForRowAtIndexPath(indexPath)?.accessoryType = UITableViewCellAccessoryType.None

    default:
        break
    }

}

答案 1 :(得分:1)

您必须通过实施tableView:willSelectRowAtIndexPath:

来管理您的单一选择部分

如果当前选择了不同的单节行,您可以在选择新的单节行之前取消选择它。

这将在一个部分中保留单个选择的错觉,同时允许在另一个部分中进行多个选择。

答案 2 :(得分:1)

此解决方案似乎正常!我使用字典来处理单个选择,保留最后一个索引的记录,并将节号作为键

    - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

        // SINGLE SELECTION - SECTIONS 0 and 2
    if( indexPath.section == 0 || indexPath.section == 2 ){
        if( cell.accessoryType == UITableViewCellAccessoryNone ){
            cell.accessoryType = UITableViewCellAccessoryCheckmark;

            if(indexDict.count ==0){
                NSNumber *sec = [NSNumber numberWithInteger:indexPath.section];
                [indexDict setObject:indexPath forKey:sec];
            }
            else{
                NSNumber *sec = [NSNumber numberWithInteger:indexPath.section];
                NSIndexPath *lastIndexPath = indexDict[sec];

                UITableViewCell *lastCell = [tableView cellForRowAtIndexPath:lastIndexPath];
                lastCell.accessoryType = UITableViewCellAccessoryNone;

                [indexDict removeObjectForKey:sec];
                [indexDict setObject:indexPath forKey:sec];
            }
        }
        else{
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    }
        // MULTIPLE SELECION - SECTION 1
    if( indexPath.section == 1 ){
        if( cell.accessoryType == UITableViewCellAccessoryNone ){
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
        else{
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    }
    return nil;
}

答案 3 :(得分:1)

我会暴露自己的解决方案,这与您发布的解决方案几乎相同。我认为它有点好,因为我手动管理选定的行,所以我可以稍后使用tableView.indexPathsForSelectedRows属性。

用Swift 3.0编写。

我首先声明一个NSIndexPath,它将存储先前选择的项目

var indexPathOfSelectedRowPaidBy:NSIndexPath?

func tableView(_ tableView:UITableView,didSelectRowAt indexPath:IndexPath){

    switch indexPath.section {
    case 0:
        if let previousIndexPath = indexPathOfSelectedRowPaidBy {
            tableView.deselectRow(at: previousIndexPath as IndexPath, animated: false)
            tableView.cellForRow(at: previousIndexPath as IndexPath)?.accessoryType = UITableViewCellAccessoryType.none
        }
        indexPathOfSelectedRowPaidBy = indexPath as NSIndexPath?
        tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.checkmark

    case 1:
        tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.checkmark

    default:
        break
    }

} func tableView(_ tableView:UITableView,didDeselectRowAt indexPath:IndexPath){

switch indexPath.section {
case 0:
    if let previousIndexPath = indexPathOfSelectedRowPaidBy {
        tableView.deselectRow(at: previousIndexPath as IndexPath, animated: false)
        tableView.cellForRow(at: previousIndexPath as IndexPath)?.accessoryType = UITableViewCellAccessoryType.none
    }
    indexPathOfSelectedRowPaidBy = nil

case 1:
    tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.none

default:
    break
}

}

答案 4 :(得分:0)

func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath?
{
    // Allow only 1 selected row in a section #0.
    let singleSelectionSection = 0
    if indexPath.section == singleSelectionSection, let index = tableView.indexPathsForSelectedRows?.filter({ $0.section == singleSelectionSection }).first {
        tableView.deselectRow(at: index, animated: false)
    }

    return indexPath
}

答案 5 :(得分:0)

在表格视图中,相同的方法

  

collectionView.allowsMultipleSelection = true

     

在indexPath.section == 1的部分将是单个选择

var lastIndexPath: IndexPath? // store previous index path 

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

            if indexPath.section != 1 {return}

            guard lastIndexPath != nil else {lastIndexPath = indexPath; return}

            if lastIndexPath != indexPath{
                collectionView.deselectItem(at: lastIndexPath!, animated: true)
            }
            lastIndexPath = indexPath
        }

答案 6 :(得分:0)

SWIFT 5 最简单和最好的方法:

var indexPathOfPreviouslySelectedRow: IndexPath?

tableview 中的代码如下

  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    
    if (indexPath.section == 1){
        if (indexPath.row == 0) {
           print("test")
            if (indexPathOfPreviouslySelectedRow != nil ){
                tableView.deselectRow(at: indexPathOfPreviouslySelectedRow!, animated: true)
            }
            indexPathOfPreviouslySelectedRow = indexPath
        }
        if (indexPath.row == 1) {
           print("test")
            if (indexPathOfPreviouslySelectedRow != nil ){
                tableView.deselectRow(at: indexPathOfPreviouslySelectedRow!, animated: true)
            }
            indexPathOfPreviouslySelectedRow = indexPath
        }
    }
    self.footerButton.isEnabled = tableView.indexPathForSelectedRow != nil
}