ios - swift : TableView Cells not getting selected correctly

标签 ios swift uitableview

我有一个 tableView,其中填充了数据库中的单元格,我需要选择一个单元格来编辑它。当我选择一个单元格时,该单元格的索引路径将被发送到我创建的一个函数,该函数采用该索引路径并在我单击的单元格下注入(inject)一个新单元格(通过在indexpath.row 处的数据数组)。

当我拥有的所有单元格都可见时,一切正常;意思是当我的 tableView 中没有滚动时。但是当有滚动时,有一些隐藏的向下单元格,当快速向下滚动并选择一个单元格时,我的函数在其他地方注入(inject)一个新单元格,然后事情开始出现问题,并且没有按预期工作

在我的自定义单元格中单击标签时调用函数 getCollCell:

This my Table

func getCollCell(_ tableViewCellIndexPath:IndexPath,_ collectionViewCellIndexPath:IndexPath)  {
    /// this function that inject new cell
    /// this function get the indexpath of the cell that was clicked from collection of tableViewCell
    /// and indexPath of tableViewCell
    /// tableViewCellIndexPath is indexpath of tableViewCell
    /// collectionViewCellIndexPath is indexPath of collectionViewCell that was clicked  from tableViewCell



    let lignetype = data[tableViewCellIndexPath.row]["st"]!

    let stTypes = [
            Constant.storyBoard.prcplSeul,
            Constant.storyBoard.prcplMenu,
            Constant.storyBoard.tiersAvecSupp,
            ]

    if stTypes.contains(lignetype) {

        setHeightToNormal()

        let tmp_table =  clickedTableCell["tableCell"]



        let tmp_collection =  clickedTableCell["collectionCell"]

        /// save indexPath of last clicked cell of tableViewCell.collection
        clickedTableCell["tableCell"] = tableViewCellIndexPath
        clickedTableCell["collectionCell"] = collectionViewCellIndexPath


    if data?.count == dataT?.count {
     /// this condition means that there is no cell injected (no element is added to data array)
        insertTableRow(tableViewCellIndexPath)
        heighlightCell(tableViewCellIndexPath, tmp_table!, collectionViewCellIndexPath,1)

    }else{

        if tmp_table == tableViewCellIndexPath {

            /// if we are still navigating in the same tableViewcell
            /// clicked tableViewCell is equal to last clicked tableViewCell

            if tmp_collection != collectionViewCellIndexPath {
                /// if we chosed different collectionViewCell in the same tableViewCell

                let index = IndexPath(row:(tmp_table?.row)! + 1, section:(tmp_table?.section)!)
                heightOfCell?[(tmp_table?.row)! + 1] = 150

                heighlightCell(tableViewCellIndexPath, tmp_table!, collectionViewCellIndexPath,1)

                tableView.reloadRows(at: [index], with: .fade)

            }else{

                removeTableRow(tmp_table!)
                heighlightCell(tableViewCellIndexPath, tmp_table!, collectionViewCellIndexPath,2)
            }

        }else{
            /// if we try to open a tableViewCell while other tableViewcell was expanded
            /// we close the expanded tableViewCell and open the new one

            removeAndInsertTableRow(tableViewCellIndexPath, tmp_table!, collectionViewCellIndexPath)
        }

    }
    }

}



    func removeTableRow(_ tableViewCellIndexPath:IndexPath)  {

    dataT?.remove(at: tableViewCellIndexPath.row + 1)
    tableView.deleteRows(at:[IndexPath(row: tableViewCellIndexPath.row + 1, section: tableViewCellIndexPath.section)] , with: .fade)
}

func insertTableRow(_ tableViewCellIndexPath:IndexPath)  {

    dataT?.insert(["":""], at: tableViewCellIndexPath.row + 1)
    heightOfCell?[tableViewCellIndexPath.row + 1] = 150
    tableView.insertRows(at:[IndexPath(row: tableViewCellIndexPath.row + 1, section: tableViewCellIndexPath.section)] , with: .fade)
}

func removeAndInsertTableRow(_ tableViewCellIndexPath:IndexPath,_ tmp_row:IndexPath, _ collectionViewCellIndexPath:IndexPath)  {

    tableView.beginUpdates()

    removeTableRow(tmp_row)

    heighlightCell(tableViewCellIndexPath,tmp_row, collectionViewCellIndexPath ,2)

    insertTableRow(tableViewCellIndexPath)

    heighlightCell(tableViewCellIndexPath,tmp_row, collectionViewCellIndexPath ,3)

    tableView.endUpdates()
}



func heighlightCell(_ tableViewCellIndexPath:IndexPath,_ tmp_row:IndexPath,_ collectionViewCellIndexPath:IndexPath, _ bool:Int){

    if bool == 1 {

        if let tcell = tableView.cellForRow(at: tableViewCellIndexPath) as? TableViewCell{

            for t in tcell.collectionView.visibleCells  {
                t.backgroundColor = UIColor(hex: "#f2f2f2")
            }

            setBGColorOfCell(tableViewCellIndexPath, collectionViewCellIndexPath, "#f2f2f2")
        }else{
            print("tableview return nil")
        }
    }else if bool == 2{
        if let tcell = tableView.cellForRow(at: tmp_row) as? TableViewCell {

            for t in tcell.collectionView.visibleCells  {
                t.backgroundColor = UIColor(hex: "#ffffff")
            }

            setBGColorOfCell(tmp_row, collectionViewCellIndexPath, "#ffffff")
        }else{
            print("tableview return nil")
        }

    } else if bool == 3{

        if tableViewCellIndexPath.row > tmp_row.row {
            heighlightCell(IndexPath(row:tableViewCellIndexPath.row + 1,section:tableViewCellIndexPath.section), tmp_row, collectionViewCellIndexPath, 1)
        }else{
            heighlightCell(tableViewCellIndexPath, tmp_row, collectionViewCellIndexPath, 1)

        }
    }

}

func setBGColorOfCell(_ index:IndexPath, _ indexColl:IndexPath, _ color:String) {

    //let index = IndexPath(row:row, section:0)
    //let indexColl = IndexPath(row:cell, section:0)

    let cell = tableView.cellForRow(at: index) as! TableViewCell


    let cellcoll = cell.collectionView.cellForItem(at: indexColl) as! CollectionViewCell


    cellcoll.backgroundColor = UIColor(hex: "#ffffff")
    cell.collectionView.backgroundColor = UIColor(hex: color)
}

如果您查看 heighlightCell 函数,一开始应用程序就会崩溃,但是当我添加条件 }else{ print("tableview return nil") } 我开始注意到这个问题

为什么会有这种行为? (这些图片向下) 当我单击添加单元格然后再次单击将其删除时会发生这种情况 enter image description here enter image description here

最佳答案

您可能会弄乱重用单元机制。 滚动时,注入(inject)的不参与 int 重用,因此重用时不会出现 将再次发生。 重写自定义单元类中的 prepareForReuse() 然后再次将单元格设置为其默认模式(在您的 我猜想删除注入(inject)的情况)

关于ios - swift : TableView Cells not getting selected correctly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45461633/

相关文章:

ios - UIViewController 变量在解除分配后仍然存在

Swift:10.10 中的 NSStatusItem 菜单行为(例如,仅在鼠标右键单击时显示)

ios - 带有 UIBlurEffect 的表格 View 变得迟钝

SwiftUI - 随着列表滚动的进行,使列表下方的内容可滚动

json - 将 Swift Codable 类迁移到 Realm

swift - 在 Swift 中通过代码调整 tableView 的高度

iOS 检索和排序具有关系的核心数据实体

ios - 框架背景中的信标检测

ios - 水平设置 UICollectionView header 时应用程序崩溃

ios - CoreBluetooth 不会在重新连接时发现服务