2017-08-02 63 views
0

我有一個tableView充滿了來自數據庫的單元格,我需要選擇一個單元格來編輯它。當我選擇一個單元格時,這個單元格的indexPath將被髮送到我創建的一個函數中,該函數採用該函數並在我單擊的單元格下注入一個新單元格(通過在該數組數組中添加一個新元素,在該indexpath.row處) 。swift:TableView單元格未被正確選擇

當我擁有的所有單元格都可見時,一切正常;這意味着當我的tableView沒有滾動時。但是當有一個滾動所以有一些潛伏下來的細胞,當向下滾動快,選擇一個單元格,我的功能注入新的細胞在其他地方,然後事情開始工作錯了,並沒有按預期工作

功能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") }我開始注意到這個問題

爲什麼這個行爲? (這些圖像向下) 這happend當我點擊添加一個單元格,然後再次單擊將其刪除 enter image description here enter image description here

+1

請添加代碼實現了更好地理解problem.thanks –

+0

@brahimm - 請分享一些代碼,以便更好地理解。 – itsaboutcode

+0

您是否使用didSelectRowAtIndexPath添加新單元格? 這似乎是當不想要的事件發生時... 請張貼您的代碼。 – smukamuka

回答

0

你與可能的複用小區機制搞亂。 在滾動時,注入的內容在重用時不參與 ,因此當重用 將再次發生時不會出現。 覆蓋prepareForReuse()在您的自定義單元格類 並有再次小區設置到其默認模式(在你的 情況下,我猜取出注射)

+0

添加到'prepareForReuse()'函數中,我添加了'collectionView.ReloadData',它像一個魅力 – brahimm