xcode 12로 업데이트 한 이후로 UITableViewCell 내부에 UIControl을 배치 할 수 없습니다.

Sep 18 2020

tableview를 사용하는 검색 양식이 있습니다. 오늘 Xcode 12를 업데이트 한 후 UISwitch, UITextField, UISlider가 UITableViewCell 내부에 중첩 될 때 더 이상 작동하지 않습니다. 이 작업을 다시 수행하려면 설정해야하는 변경된 속성이 있습니까?

내 프로젝트가 아닌지 확인하기 위해 새 프로젝트를 만들고 그 안에 UITextField를 중첩 했는데도 작동하지 않습니다.

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell()
    
    let textField = UITextField(frame: CGRect(x: 5, y: 5, width: 400.0, height: 25.0))
    textField.delegate = self
    textField.backgroundColor = .blue
    cell.addSubview(textField)

    return cell
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print("this will get called even when selecting the UITextField")
}

func textFieldDidBeginEditing(_ textField: UITextField) {
    print("this is never called")
}

답변

13 matt Sep 18 2020 at 05:46

귀하의 코드는 항상 잘못되었습니다.

cell.addSubview(textField)

셀에 하위보기를 추가 해서는 안됩니다 . 하위 뷰를 셀의 contentView.

1 Guille Sep 19 2020 at 17:12

iOS 14로 업그레이드 한 이후에도 똑같은 일이 일어났습니다. 이것은 subView를 셀에 직접 추가 할 때 저에게 효과적이었습니다.

cell.contentView.isUserInteractionEnabled = true

Baki Sep 26 2020 at 01:27

비슷한 문제가 있었고 그것에 대해 계속 진행되었습니다 ... 내 코드에서 문제가 된 것은 UITableViewCell에서 내가 이것을하고 있다는 것입니다.

        didSet {
            if contentView.backgroundColor == backgroundColor { return }
            contentView.backgroundColor = backgroundColor
            for v in otherView.subview { v.backgroundColor = backgroundColor }
        }

여기에서이 줄을 제거 contentView.backgroundColor = backgroundColor하면 트릭이 생겼습니다. 이제 셀이 표시되고 중복 된 항목이 없습니다.contentView

어쩌면이 의지 도움말 사람, 내가 직접 파단 추가에 대한 유일한 해답을 발견하기 때문에 cell대신에를cell.contentView

편집 1 : 좋아, 상황에 대해 업데이트하고 싶었습니다. 문제는 유형의 하위 뷰가 실제로 사용되어야 UIStackView했던 subview곳 을 사용했다는 것입니다.arrangedSubviews

이것이 누군가를 도울 수 있기를 바랍니다.