2014-12-28 8 views
4

현재 표시되는 4 개의 셀을 표시하며 함께 그룹화하려는 UITableViewController가 있습니다. 그러나 어떻게 그 방법을 알 수는 없습니다. 표준 UITableView UIViewController 또는 뭔가를 통해 삽입 할 때 내가 찾은 리소스를 인터페이스 작성기를 사용하여 지침을 제공합니다. 프로그래밍 방식으로 그룹화하려면 어떻게합니까? 여기 프로그래밍 방식으로 그룹화 된 UITableView 만들기

내 현재있는 UITableViewController 클래스입니다 :

import UIKit 

class SecondViewController: UITableViewController { 

    let Items = ["Altitude","Distance","Groundspeed"] 

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return self.Items.count 
    } 

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell 
     cell.textLabel?.text = self.Items[indexPath.row] 
     return cell 
    } 


    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
     self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell") 
    } 

} 

답변

11

테이블보기의 스타일이 초기화의 순간에 설정되고, 나중에 수정할 수 없습니다,하지만 당신이 할 수있는 것은있는 새 테이블 뷰를 생성하는 것입니다 (당신은 또한 numberOfSectionsInTableView (_ :)를 사용하여 테이블에서 섹션의 수와있는 tableView를 사용하여 각각에 대한 제목을 지정해야합니다

self.tableView = UITableView(frame: self.tableView.frame, style: .Grouped) 

: 당신이 원하고있는 viewDidLoad에서 테이블보기로 설정 그룹화 된 스타일 _ : titleFo rHeaderInSection :) 또는 tableView (_ : titleForFooterInSection :).

+0

그러나 스토리 보드에서 어떻게 테이블보기를 만들 수 있습니까? UITableView를 뷰 컨트롤러로 드래그하려고 할 때마다 빈 UIVewController도 다음과 같이 만났습니다. https://imgur.com/vXBp3aS – user3185748

+0

UITableViewController를 직접 사용하고 직접 드래그하면 더 좋을 것입니다. 스토리 보드와 Identity Inspector에서 클래스를 UITableViewController 하위 클래스로 변경합니다. UIViewController에서 UITableView를 드래그 할 수없는 이유는 사용자가 축소되어 스토리 보드의 아무 곳이나 두 번 탭하여 다시 확대하고 다시 시도한다는 것입니다. – bprzemyslaw