2016-10-27 5 views
0

나는 대략 3 개 또는 4 개의 테이블 컨트롤러를 가지고 있으며 모두 동일한 테이블 뷰 셀을 사용합니다. 나는 가능한 논리의 네 번째로 되돌아 간다. 동일한 tableViewCell을 여러 tableView 컨트롤러에서 사용할 수 있습니까? 두 테이블 사이의 정보가 같다고 가정합니다. 아니면 각 컨트롤러에 대해 새 셀을 만들어야합니까?여러 테이블 컨트롤러에서 사용할 수있는 하나의 tableViewCell을 만들 수 있습니까?

답변

3

가능합니다.

나는 스위프트를 사용하고 있다고 가정합니다.

파일 -> 새로 만들기로 이동하여 다음과 같이 cocoaTouch 클래스를 선택하십시오.

enter image description here

이제 사용자 지정 셀 클래스의 이름을 지정하고있는 UITableViewCell의 그 서브 클래스를 만든다. 또한 말한다 확인란을 선택 "또한 XIB 파일을 만듭니다"

enter image description here

지금이 XIB에서 셀을 설계하고 .Swift 파일에서 콘센트를 만들 수 있습니다. 당신이 당신의 셀에있는 라벨이나 이미지 뷰 또는 아무것도 포함이

enter image description here

같은 형태의 사용자 정의를 tableView 셀을 갖고 있다고 할 수 있습니다. 이제 사용자 정의 셀의 사용자의 빠른 파일에서 당신은 이제 당신은 그냥 나는 그것이 도움이되기를 바랍니다

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

let cell = YourCustomTableViewCell.cellForTableView(tableView, atIndexPath: indexPath) 
cell.backgroundColor = UIColor.clearColor() 
// do something with your cell 

} 

아래처럼 응용 프로그램에서 어떤있는 tableView이 셀을 사용할 수 있도록

class func cellForTableView(tableView: UITableView, atIndexPath indexPath: NSIndexPath) -> YourCustomTableViewCell { 
let kYourCustomTableViewCellIdentifier = "kYourCustomTableViewCellIdentifier" 
tableView.registerNib(UINib(nibName: "YourCustomTableViewCell", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: kYourCustomTableViewCellIdentifier) 

let cell = tableView.dequeueReusableCellWithIdentifier(kYourCustomTableViewCellIdentifier, forIndexPath: indexPath) as! YourCustomTableViewCell 

return cell 
} 

같은 방법을 쓸 수 있습니다. 스위프트 3 스위프트 4

업데이트 :

class func cellForTableView(tableView: UITableView, atIndexPath indexPath: IndexPath) -> YourCustomTableViewCell { 
    let kYourCustomTableViewCellIdentifier = "kYourCustomTableViewCellIdentifier" 
    tableView.register(UINib(nibName: "YourCustomTableViewCell", bundle: Bundle.main), forCellReuseIdentifier: kYourCustomTableViewCellIdentifier) 

    let cell = tableView.dequeueReusableCell(withIdentifier: kYourCustomTableViewCellIdentifier, for: indexPath) as! YourCustomTableViewCell 

    return cell 
} 
+0

매우 도움이! 궁금한 점은 처음에 IBOutlets tho를 연결하는 방법은 무엇입니까? 연결할 스토리 보드 셀을 하나만 선택하면됩니까? – jonpeter

+0

@jonpeter 무엇의 콘센트? 귀하의 테이블에 귀하의 세포를 연결하는 얘기라면 그 필요가 없습니다. 위 코드를 작성하면 제대로 작동합니다. 그리고 셀에 구성 요소의 출력을 요청하는 경우에는 그에 대한 대답도 업데이트 할 것입니다. –

+0

@jonpeter는 업데이트 된 답변을 확인하십시오. –

0

예 여러 tableView 컨트롤러에서 테이블보기 셀을 사용할 수 있습니다.

관련 문제