swift : TableView 셀이 올바르게 선택되지 않음

브라 힘

데이터베이스의 셀로 채워진 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] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Swift : UIButton이 올바르게 호출되지 않음

분류에서Dev

선택 정렬이 올바르게 정렬되지 않음

분류에서Dev

iOS 7.1 tableView reloadData가 tableView를 올바르게 업데이트하지 않음

분류에서Dev

현재 선택된 옵션이 올바르게 표시되지 않음

분류에서Dev

.load ajax 후 선택 옵션이 올바르게 표시되지 않음

분류에서Dev

선택 및 텍스트 영역이 올바르게 정렬되지 않음

분류에서Dev

Swift TableView 삭제 셀이 표시되지 않음

분류에서Dev

HTML 선택이 올바르게 작동하지 않음

분류에서Dev

CSS 열이 올바르게 스택되지 않음

분류에서Dev

Swift Tableview 검색 결과 선택된 셀 확인 표시가 지속되지 않음

분류에서Dev

사용자 지정 셀이 올바르게로드되지 않음

분류에서Dev

올바르게 작동하지 않는 표에서 셀 선택

분류에서Dev

border-collapse시 셀 너비 속성이 올바르게 복사되지 않음

분류에서Dev

R에서 올바르게 업데이트되지 않는 입력 선택

분류에서Dev

Java에서 바코드 판독이 픽셀을 올바르게 읽지 않음

분류에서Dev

뷰 모델이 올바르게 바인딩되지 않음 C #

분류에서Dev

바람둥이가 올바르게 배포되지 않음

분류에서Dev

음수 값이 올바르게 인쇄되지 않음

분류에서Dev

선택기에 따라 텍스트가 올바르게 업데이트되지 않음

분류에서Dev

Keycloak 등록-사용자 정의 사용자 속성 값이 올바르게 선택되지 않음

분류에서Dev

ng-model이 선택 입력에서 올바르게 작동하지 않음

분류에서Dev

Shuffle 기능이 Swift 1.2에서 올바르게 구현되지 않습니다.

분류에서Dev

선택한 jquery가 올바르게 표시되지 않음

분류에서Dev

배경 이미지가 올바르게 표시되지 않음

분류에서Dev

FancyBox 이미지가 올바르게로드되지 않음

분류에서Dev

우선 순위 대기열이 올바르게 비교되지 않음 C ++

분류에서Dev

SVG 아이콘이 올바르게 표시되지 않음

분류에서Dev

모델이 올바르게 업데이트되지 않음

분류에서Dev

이온 탭 색상이 올바르게 표시되지 않음

Related 관련 기사

  1. 1

    Swift : UIButton이 올바르게 호출되지 않음

  2. 2

    선택 정렬이 올바르게 정렬되지 않음

  3. 3

    iOS 7.1 tableView reloadData가 tableView를 올바르게 업데이트하지 않음

  4. 4

    현재 선택된 옵션이 올바르게 표시되지 않음

  5. 5

    .load ajax 후 선택 옵션이 올바르게 표시되지 않음

  6. 6

    선택 및 텍스트 영역이 올바르게 정렬되지 않음

  7. 7

    Swift TableView 삭제 셀이 표시되지 않음

  8. 8

    HTML 선택이 올바르게 작동하지 않음

  9. 9

    CSS 열이 올바르게 스택되지 않음

  10. 10

    Swift Tableview 검색 결과 선택된 셀 확인 표시가 지속되지 않음

  11. 11

    사용자 지정 셀이 올바르게로드되지 않음

  12. 12

    올바르게 작동하지 않는 표에서 셀 선택

  13. 13

    border-collapse시 셀 너비 속성이 올바르게 복사되지 않음

  14. 14

    R에서 올바르게 업데이트되지 않는 입력 선택

  15. 15

    Java에서 바코드 판독이 픽셀을 올바르게 읽지 않음

  16. 16

    뷰 모델이 올바르게 바인딩되지 않음 C #

  17. 17

    바람둥이가 올바르게 배포되지 않음

  18. 18

    음수 값이 올바르게 인쇄되지 않음

  19. 19

    선택기에 따라 텍스트가 올바르게 업데이트되지 않음

  20. 20

    Keycloak 등록-사용자 정의 사용자 속성 값이 올바르게 선택되지 않음

  21. 21

    ng-model이 선택 입력에서 올바르게 작동하지 않음

  22. 22

    Shuffle 기능이 Swift 1.2에서 올바르게 구현되지 않습니다.

  23. 23

    선택한 jquery가 올바르게 표시되지 않음

  24. 24

    배경 이미지가 올바르게 표시되지 않음

  25. 25

    FancyBox 이미지가 올바르게로드되지 않음

  26. 26

    우선 순위 대기열이 올바르게 비교되지 않음 C ++

  27. 27

    SVG 아이콘이 올바르게 표시되지 않음

  28. 28

    모델이 올바르게 업데이트되지 않음

  29. 29

    이온 탭 색상이 올바르게 표시되지 않음

뜨겁다태그

보관