简体   繁体   English

Swift-具有多个自定义单元格类别时选择单元格

[英]Swift - Selecting cells when having multiple custom cell-classes

I have 2 custom cells that I need to set the selection properties for. 我需要设置2个自定义单元格的选择属性。 Right now, I've used a boolean to tell the program that the cell is either type 1 or type 2, because I was just checking if the code was working. 现在,我使用了一个布尔值来告诉程序该单元格是1型还是2型,因为我只是在检查代码是否正常工作。

Now I need the program to find out which type of cell it is by itself, and I don't know how it's done. 现在,我需要该程序来找出它本身是哪种类型的单元格,而我不知道它是如何完成的。

My code looks like this: 我的代码如下所示:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        if istrue{
        let mySelectedCell: CustomTableCell = tv.cellForRowAtIndexPath(indexPath)! as CustomTableCell

        if (mySelectedCell.accessoryType == UITableViewCellAccessoryType.Checkmark) {
            mySelectedCell.accessoryType = UITableViewCellAccessoryType.None

            if let tx = mySelectedCell.customLabel.text as String!{

                var textLabel:String = String()
                textLabel = tx

                println(textLabel)

                if let ind = find(tempList, textLabel) {
                    tempList.removeAtIndex(ind)
                    println(tempList)

                }
            }
        }

        else {

            mySelectedCell.accessoryType = UITableViewCellAccessoryType.Checkmark
            mySelectedCell.tintColor = UIColor.blackColor()

            if let tx = mySelectedCell.customLabel.text as String!{

                var textLabel:String = String()
                textLabel = tx
                tempList.append(textLabel)

                println(tempList)
            }
        }

        }else{
            let mySelectedCell: CustomTableCell2 = tv.cellForRowAtIndexPath(indexPath)! as CustomTableCell2

            if (mySelectedCell.accessoryType == UITableViewCellAccessoryType.Checkmark) {
                mySelectedCell.accessoryType = UITableViewCellAccessoryType.None

                if let tx = mySelectedCell.titleLabel.text as String!{

                    var textLabel:String = String()
                    textLabel = tx

                    println(textLabel)

                    if let ind = find(tempList, textLabel) {
                        tempList.removeAtIndex(ind)
                        println(tempList)

                    }
                }
            }

            else {

                mySelectedCell.accessoryType = UITableViewCellAccessoryType.Checkmark
                mySelectedCell.tintColor = UIColor.blackColor()

                if let tx = mySelectedCell.titleLabel.text as String!{

                    var textLabel:String = String()
                    textLabel = tx
                    tempList.append(textLabel)

                    println(tempList)
                }
            }
        }
        }

I don't know if this is the best way of doing it either, so please give me some tips if you have a better way to set this "checkmark" selection-style. 我也不知道这是否是最好的方法,因此,如果您有更好的方法来设置此“选中标记”选择样式,请给我一些提示。

All suggestions on how to achieve this would be appreciated. 关于如何实现这一点的所有建议将不胜感激。

Here is a Objective example of this: 这是一个客观的例子:

id cell = [tableView cellForRowAtIndexPath:indexPath];

if ([cell isKindOfClass:[PhotoCell2x class]]) {
    NSLog(@"1");
}
else if([cell isKindOfClass:[EffectCell2x class]]){
    NSLog(@"2");
}

If I get time i'll add a swift version. 如果我有时间,我会添加一个快速版本。 Hope this helps.. :) 希望这可以帮助.. :)

Here it is updated for Swift 4 for anyone that needs it. 在这里,它针对需要它的任何人针对Swift 4进行了更新。

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

    let cell = tableView.cellForRow(at: indexPath)

    if (cell?.isMember(of: CLASSNAME.self))!{
        print("hello")
    } else {

    }
}

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

相关问题 Swift 3 - 重复使用具有多个自定义单元格的单元格的问题 - Swift 3 - Problems in reusing cell with multiple custom cells Swift CollectionView设置为false时选择多个单元格 - Swift CollectionView is selecting multiple cell when is set to false Swift:如果让多个单元格类 - Swift: If Let with Multiple Cell Classes Swift-使用自定义类作为TableView中的单元格 - Swift - Using custom classes as cells in a TableView 选择一个单元格并更改tableView中所有单元格的Alpha-Swift - Selecting a cell and changing the alpha of all cells in tableView - Swift 有多个自定义UITableViewCells时如何快速设置动态高度 - swift how to set dynamic height when having multiple custom UITableViewCells 当在Storyboard中的UITableView中具有带有静态单元格的XIB自定义单元格时,插座为零 - Outlets are nil when having custom cell with XIB in UITableView in Storyboard with static cells iOS 遇到 UITableView 以编程方式选择多个单元格并显示检查的问题 - iOS having issues with UITableView programmatically selecting multiple cells and displaying a check iOS:didSelectRowAtIndexPath选择单个单元格时选择多个单元格 - IOS: didSelectRowAtIndexPath select multiple cells while selecting single cell 用户使用Swift在UICollectionView中选择一个单元格时如何修改多个单元格 - How to modifiy multiple cells when user select a cell in UICollectionView using Swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM