Swift 複数のカスタムセルを使いわけたいとき
Swift 複数のカスタムセルを使いわけたいときの実装方法。
具体的には、
①カスタスセルのclassを2種類作成する。
②tableView.registerClassでその2種類のカスタムセルを登録する。
③cellForRowAtIndexPath内で条件により使いわける。
でOKだった。
override func viewDidLoad() { super.viewDidLoad() if let tableView = self.tableView { tableView.registerClass(TableViewCell.classForCoder(),forCellReuseIdentifier: "cell") tableView.registerClass(TableViewCell2.classForCoder(),forCellReuseIdentifier: "cell2") tableView.rowHeight = UITableViewAutomaticDimension } }
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { switch indexPath.row{ case 0: let cell = tableView.dequeueReusableCellWithIdentifier("cell2",forIndexPath: indexPath) as! TableViewCell2 cell.selectionStyle = UITableViewCellSelectionStyle.None let cnt = msgs.count let m = msgs[cnt - indexPath.row - 1] as! Message cell.name.text = "name" cell.comment.text = m.text cell.comment.numberOfLines = 0 cell.transform = self.tableView!.transform return cell default: let cell = tableView.dequeueReusableCellWithIdentifier("cell",forIndexPath: indexPath) as! TableViewCell cell.selectionStyle = UITableViewCellSelectionStyle.None let cnt = msgs.count let m = msgs[cnt - indexPath.row - 1] as! Message cell.name.text = m.name cell.comment.text = m.text cell.comment.numberOfLines = 0 cell.transform = self.tableView!.transform return cell } }