我有一个 tableView,其中填充了数据库中的单元格,我需要选择一个单元格来编辑它。当我选择一个单元格时,该单元格的索引路径将被发送到我创建的一个函数,该函数采用该索引路径并在我单击的单元格下注入(inject)一个新单元格(通过在indexpath.row
处的数据数组)。
当我拥有的所有单元格都可见时,一切正常;意思是当我的 tableView
中没有滚动时。但是当有滚动时,有一些隐藏的向下单元格,当快速向下滚动并选择一个单元格时,我的函数在其他地方注入(inject)一个新单元格,然后事情开始出现问题,并且没有按预期工作
在我的自定义单元格中单击标签时调用函数 getCollCell
:
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") }
我开始注意到这个问题
最佳答案
您可能会弄乱重用单元机制。
滚动时,注入(inject)的不参与
int 重用,因此重用时不会出现
将再次发生。
重写自定义单元类中的 prepareForReuse()
然后再次将单元格设置为其默认模式(在您的
我猜想删除注入(inject)的情况)
关于ios - swift : TableView Cells not getting selected correctly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45461633/