Swift TableView Section ごとに表示内容を分ける

セクションごとに表示したい内容を変えたい時

PurposeItemはtext,photo,map,linkを持つ それを表示させる。 ポイントは引数のデフォルトにnilを指定しておく。 こうすることにより、photoだけ入った配列を作れる。=>tableviewに一つだけ表示できる。

class PurposeItem {
    
    enum ItemType: Int {
        case text = 1
        case photo = 2
        case map = 3
        case link = 4
    }
    

    var photo:UIImage?
    var map:CLLocationCoordinate2D?
    var link:URL?
    var itemType:ItemType!
    
    init(photo: UIImage, link:URL? = nil, map:CLLocationCoordinate2D? = nil, itemType:ItemType){
        self.photo = photo
        self.link = link
        self.map = map
        self.itemType = itemType
    }

独自セルの登録を済ませておく。 これはハマってしまった。

tableView.register このメソッドで登録しないとだめ

xibジブファイルの作り方は別のサイトを参考にして下さい。 これをViewDidloadで呼び出す。

    func registerTableViewCell() {
        let photoXib = UINib(nibName: "PhotoViewCell", bundle: nil)
        tableView.register(photoXib, forCellReuseIdentifier: "photo")
        
        let mapXib = UINib(nibName: "MapViewCell", bundle: nil)
        tableView.register(mapXib,forCellReuseIdentifier: "map")

        let linkXib = UINib(nibName: "LinkViewCell", bundle: nil)
        tableView.register(linkXib, forCellReuseIdentifier: "link")
        
        let textXib = UINib(nibName: "LinkViewCell", bundle: nil)
        tableView.register(textXib, forCellReuseIdentifier: "text")
        
    }

ここが結構良いサイトです 2.XIBに別ファイルとして出してそれを使う を参照下さい。圧倒的にこちらの方法がいいです。 qiita.com

一つ抜けがありますので、そこだけ記載しておきますね。

ストーリーボードでは、2箇所の設定が必要です

1-TableViewCellを継承した自分が作成したクラスを指定

f:id:happy_teeth_ago:20181208134531p:plain

このようにCellを何種類でも登録できます。便利です。

2-Cellのidentifierを設定

f:id:happy_teeth_ago:20181208134705p:plain

この文字をテーブルを表示するデリゲートメソッドの中で利用します。 この絵は、一応xibファイルの接続の状態ですね。

f:id:happy_teeth_ago:20181208134344p:plain
xibを接続しておきます。上記サイトに詳しく書いてあります。

セクションを分けます。今回は2つに分けます。

通常は、配列を用意してその数だけセクションを分けたりします。

    func numberOfSections(in tableView: UITableView) -> Int {
        return 2

  //配列がある場合はこんな感じ       
  //return items.count
    }

各テーブルのセルの数を返す関数

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        //もし初めのセクションなら
        if section == 0{
            return items.count
        //もし2番めのセクションなら
        }else if section == 1{
            return articles.count
        }else{
            return 0
        }

    }

セルの内容を表示する

   
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //indexPathがrowとsectionを持っているので、sectionで切り分ける。ここがポイント
        if indexPath.section == 0{
            let item = items[indexPath.row]
            switch item.itemType{
            case .photo?:
                let photoCell = tableView.dequeueReusableCell(withIdentifier: "photo") as! PhotoViewCell
                photoCell.photo.image = item.photo
                tableView.rowHeight = UITableView.automaticDimension
                return photoCell
                
            case .map?:
                let photoCell = tableView.dequeueReusableCell(withIdentifier: "photo") as! PhotoViewCell
                photoCell.photo.image = item.photo
                tableView.rowHeight = UITableView.automaticDimension
                return photoCell
                
            default:
                let cell = UITableViewCell()
                return cell
        }
        
        }else if indexPath.section == 1{
            
         //ここに2番めのセクションに表示したいものを記載する
            //すみません、時間の関係で最後までかけませんでした。
            
            
        }
        
    }