programing tip

콘센트를 반복 콘텐츠 iOS에 연결할 수 없습니다

itbloger 2020. 6. 24. 07:46
반응형

콘센트를 반복 콘텐츠 iOS에 연결할 수 없습니다


방금 앱을 만들고 @IBOutlet을 스토리 보드에 연결하기 시작했습니다. 기본 스타일을 사용하여 UITableViewCell 프로토 타입 셀의 레이블에 일부를 연결하고 있습니다. 연결하면 스토리 보드 에이 오류가 발생합니다.

TableViewController에서 UILabel 로의 detailText 아웃렛이 유효하지 않습니다. 콘센트는 반복 컨텐츠에 연결할 수 없습니다.

누군가 나를 도울 수 있습니까? 나는 항상 성공적인 방식으로 설정했지만 이번에는이 오류를 해결했습니다.


테이블 뷰 셀 서브 클래스를 작성하여 프로토 타입의 클래스로 설정하십시오. 해당 클래스에 콘센트를 추가하고 연결하십시오. 이제 셀을 구성하면 콘센트에 액세스 할 수 있습니다.


스토리 보드를 통해 제공되는 두 가지 유형의 테이블 뷰 셀이 있습니다. 이들은 동적 프로토 타입정적 셀입니다.

여기에 이미지 설명을 입력하십시오

1. 동적 프로토 타입

이름에서이 유형의 셀은 동적으로 생성됩니다. 스토리 보드가 아닌 코드를 통해 제어됩니다. 테이블 뷰의 대리자 및 데이터 소스를 사용하여 프로그래밍 방식으로 셀 수, 셀 높이, 셀 프로토 타입을 지정할 수 있습니다.

셀을 테이블보기로 끌면 셀 프로토 타입이 선언됩니다. 그런 다음이 프로토 타입을 기반으로 셀을 얼마든지 작성하고 cellForRow프로그래밍 방식으로 메소드를 통해 테이블보기에 추가 할 수 있습니다 . 이 방법의 장점은 모든 뷰를 직접 추가 한 각 셀을 만드는 대신 하나의 프로토 타입 만 정의하면된다는 것입니다 (정적 셀 참조).

따라서이 경우 셀 프로토 타입의 UI 요소를보기 컨트롤러에 연결할 수 없습니다. 하나의 뷰 컨트롤러 오브젝트 만 시작되지만 많은 셀 오브젝트가 시작되어 테이블보기에 추가 될 수 있습니다. 하나의 뷰 컨트롤러 연결로 여러 셀을 제어 할 수 없으므로 셀 프로토 타입을 뷰 컨트롤러에 연결하는 것은 의미가 없습니다. 그렇게하면 오류가 발생합니다.

여기에 이미지 설명을 입력하십시오

이 문제를 해결하려면 프로토 타입 레이블을 UITableViewCell객체 에 연결해야 합니다. A UITableViewCell는 또한 셀의 프로토 타입이므로 원하는 수의 셀 오브젝트를 시작할 수 있으며, 각 오브젝트는 스토리 보드 테이블 셀 프로토 타입에서 생성 된보기에 연결됩니다.

여기에 이미지 설명을 입력하십시오

마지막으로, cellForRow메소드에서 UITableViewCell클래스 에서 사용자 정의 셀을 작성 하고 레이블로 재미있는 일을하십시오.

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "yourCellIdentifier") as! YourCell

    cell.label.text = "it works!"

    return cell
}

2. 정적 셀

On the other hand, static cells are indeed configured though storyboard. You have to drag UI elements to each and every cell to create them. You will be controlling cell numbers, heights, etc from the storyboard. In this case, you will see a table view that is exactly the same from your phone compared with what you created from the storyboard. Static cells are more often used for setting page, which the cells do not change a lot.

To control UI elements for a static cell, you will indeed need to connect them directly to your view controller, and set them up.

여기에 이미지 설명을 입력하십시오


If you're using a table view to display Settings and other options (like the built-in Settings app does), then you can set your Table View Content to Static Cells under the Attributes Inspector. Also, to do this, you must embedded your Table View in a UITableViewController instance.


Or you don't have to use IBOutlet to refer to the object in the view. You can give the Label in the tableViewCell a Tag value, for example set the Tag to 123 (this can be done by the attributes inspector). Then you can access the label by

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "someID", for: indexPath)

    let label = cell.viewWithTag(123) as! UILabel //refer the label by Tag

    switch indexPath.row {
    case 0:
        label.text = "Hello World!"
    default:
        label.text = "Default"
    }
    return cell 
}

나와 함께 UIViewcontroller, 그리고 그것에 사용자 정의 셀이있는 테이블 뷰가 있습니다. 나는에 UILabel의 내 출구를 매핑 UItableviewcell(가)에 UIViewController다음 오류가 발생했습니다.


대부분의 사람들은 서브 클래 싱 UITableViewCell 이이 문제를 해결 한다고 지적했습니다 . 그러나 프로토 타입 셀 ( UITableViewCell )이 Apple에 의해 정의되어 있고 자신의 콘센트를 추가 할 수 없기 때문에 이것이 허용되지 않는 이유 입니다.

참고 URL : https://stackoverflow.com/questions/26561461/outlets-cannot-be-connected-to-repeating-content-ios

반응형