본문 바로가기
2023년 이전/iOS

iOS - .xib에서 TableView 와 User Interaction Enabled

by JeongUPark 2020. 6. 14.
반응형

회사에서 업무로 ios를 할 기회가 생겨서 ios 개발을 시작하였습니다. swift 흥미진진한 언어 입니다.

 

만일 자바만 사용했다 했다면 어려웠을 것 같지만 kotlin을 하다 swift를 하니 그렇게 어렵지는 않았습니다. (아마 나중에 대부분의 언어들이 이런 형태를 취하지 않을까 조심스럽게 예상해 봅니다.)

 

아무튼 이번 글에서는 ViewController가 아닌 곳에서 TableView 만드는 방법과 User Interaction Enabled에 대하여 정리해보려 합니다.

.xib에서 TableView 사용하기

 

이글을 쓰게된 이유는 다음과 같은 상황 때문 입니다.

 

 

 

위와 같이 xib에 Table BottomView 라는 것을 만들고 그 안에 Table을 넣었습니다. 그리고 아래 TableBottomViewCell로 Table의 cell을 채웠습니다.

 

그럼 이제 TableView를 만들어 보겠습니다.

class  TableBottomView : UITableViewDelegate, UITableViewDataSource {

    ...
    
    //TableView에 나타날 항목 갯수
     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return list.cont
    }
    //TableView 항목의 높이. 
    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat{
        return 100
    }
    // TableView 각 항목 data 셋팅
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell: TableBottomViewCell! = tableView.dequeueReusableCell(withIdentifier: "TableBottomViewCell") as? TableBottomViewCell
        cell.setData(data: contentlist[indexPath.row])
        return cell
    }
    
    ....
}

위와 같이 만드니 계속해서 cell에 대해 nil이 뜨는 현상이 있었습니다. ViewController에 TableView를 만들고 그 안에 customCell을 만들었을 때는 위의 code가 정상 동작을 하였습니다.

그래서 왜 그럴까 생각해보니 .xib에서는 TableView를 만들면 다음과 같이 뜨고 안에 

TableView안에 TableViewCell을 넣을 수 없었습니다. 그래서 위 code에서 TableBottomViewCell을 찾지 못해서 nil이 뜨는 현상이 었습니다. 그래서 해결책을 찾아보니

 

class  TableBottomView : UITableViewDelegate, UITableViewDataSource {

    ...
    
    //TableView에 나타날 항목 갯수
     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return list.cont
    }
    //TableView 항목의 높이. 
    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat{
        return 100
    }
    // TableView 각 항목 data 셋팅
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell: TableBottomViewCell! = tableView.dequeueReusableCell(withIdentifier: "TableBottomViewCell") as? TableBottomViewCell
        if cell == nil {
            cell = Bundle.main.loadNibNamed("BottomView", index : 7)
        }
        cell.setData(data: contentlist[indexPath.row])
        return cell
    }
    
    ....
}

위와 같이 cell이 nil일 때 

if cell == nil {
	cell = Bundle.main.loadNibNamed("BottomView", index : 7)
}

BottomView.xib에서 8번째 View를 load하여 cell로 사용합니다.

 

 

User Interaction Enabled

이제 TableView를 만들었으니 그 TableView 안에 cell을 클릭했을 때 동작을 만들어 주려고 하였습니다. 그래서 action을 만들고 테스트를 했봤는데 클릭에 대한 동작이 발생하지 않았습니다.

 

그래서 구글링을 해보니 

cell.contentView.userInteractionEnabled = true 

위와 같이 설정을 해주면 된다고 하는데, 전혀 동작이 되지 않았습니다. 그래서 다른 iOS 개발자 분께 물어보니cell 상단인 TableView 또는 그 TableView 상단의 View가 User Intercation Enabled가 체크 되어 있지 않아서 그럴 수 있다고 하셨고, 그래서 storyboard를 확인해보니 TableView에 User Intercation Enabled가 체크되어 있지 않았습니다. 그래서 체크 해주니 정상 동작을 하였습니다. 

 

그럼 이 User Interaction Enabled가 무엇인지 찾아보니 

NO로 설정하면보기 용 터치, 프레스, 키보드 및 포커스 이벤트가 무시되고 이벤트 큐에서 제거되고,  YES로 설정하면 이벤트가 정상적으로전달되는 설정이라고 합니다. 그리고 이 특성의 기본값은 YES입니다. (사실 apple developer의 내용을 번역했습니다.)

 

다음은 Apple Developer에 나온 문서 원문입니다.

When set to NO, touch, press, keyboard, and focus events intended for 
the view are ignored and removed from the event queue. 
When set to YES, events are delivered to the view normally.
The default value of this property is YES.
반응형