데이터베이스의 셀로 채워진 tableView가 있습니다. 편집 할 셀을 선택해야합니다. 셀을 선택하면이 셀의 indexPath가 내가 만든 함수로 전송되고,이 함수는이를 가져와 indexpath
내가 클릭 한 셀 아래에 새 셀을 삽입합니다 (데이터 배열에 새 요소를 추가하여 indexpath.row
).
내가 가진 모든 세포가 보이면 모든 것이 잘 작동합니다. 내 tableView
. 하지만 스크롤이있을 때 숨겨진 셀이 몇 개있을 때 빠르게 스크롤하여 셀을 선택하면 내 함수가 다른 곳에 새 셀을 주입 한 다음 문제가 발생하고 예상대로 작동하지 않습니다.
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") }
하자 문제를인지하기 시작했습니다
왜이 behadior? (이 이미지는 아래로) 클릭하여 셀을 추가 한 다음 다시 클릭하여 제거 할 때 발생했습니다.
재사용 셀 메커니즘을 엉망으로 만들고 있습니다. 스크롤시 삽입 된 항목은 재사용에 참여하지 않으므로 재사용이 다시 발생할 때 나타나지 않습니다. prepareForReuse()
사용자 정의 셀 클래스에서 재정의 하고 셀을 다시 기본 모드로 설정하십시오 (귀하의 경우 주입을 제거하려고합니다)
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다