[swift] tableview複数セクションでカスタムセルを実装する
目次
tableview複数セクションでカスタムセルを実装する
複数セッションを持つtableiViewでセクションごとにカスタムcellを実装したく、tableViewコード内で最初はif section〜で分岐させようとしたがうまくいかなかったので、以下の通りに変更
cellを定義
var customCell1 : CustomCell1! var customCell2 : CustomCell2!
viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
customCell1 = tableView.dequeueReusableCellWithIdentifier("customCell1", forIndexPath: NSIndexPath(forRow: 0, inSection: 0)) as! CustomCell1
customCell2 = tableView.dequeueReusableCellWithIdentifier("customCell2", forIndexPath: NSIndexPath(forRow: 0, inSection: 1)) as! CustomCell2
}
tableViewコード内
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
switch indexPath.section {
case 0:
return customCell1
case 1:
return customCell2
default:
return customCell1
}
}