5
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

TableViewのSection管理はどうするか

Posted at

#はじめに
初投稿です。お手柔らかにお願いしますー!

#TableViewのSection管理どうしてます?
開発しててどう管理するか。。。と頭を悩ませることよくありませんか?
ハンバーガーボタンを押すといわゆるサイドメニューが出てくるアプリをよく見かけると思います。
そのサイドメニュー内の表示処理って動的じゃないので同じような処理がごちゃごちゃしがちだと思います。

そこで僕はセクション管理には基本enumを使っています。
こんな感じ。

SideMenuViewController.swift
//セクション配列に使うenumの定義
enum SideMenuSecitonType {
    case profile //プロフィール項目
    case home //ホーム項目
}

open class SideMenuViewController:UIViewController,UITableViewDataSource {
    
    @IBOutlet weak var tableView: UITableView!
    var sections = [SideMenuSecitonType]() //Enumのセクション配列を定義
    
    open override func viewDidLoad() {
        super.viewDidLoad()
        createSectionData() //画面読み込み時にセクション配列を作成する
    }
    
    /// セクション配列を作成する
    func createSectionData() {
        sections = [.profile,
                    .home]
    }
    
    //セクション数を返却
    public func numberOfSections(in tableView: UITableView) -> Int {
        return sections.count //sectionsの総数を返却するだけなので便利
    }
    
    //セクションに応じたセル数を返却する。今回は1固定
    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    //セル生成
    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "Snorlax", for: indexPath)
        
        let section = sections[indexPath.section] //セクション配列から引っ張る
        var titleString = ""
        
        switch section {
        case .profile:
            titleString = "プロフィール"
        case .home:
            titleString = "ホーム"
        }
        
        cell.textLabel?.text = titleString
        
        return cell
    }
    
}

こんな感じでEnumで管理することで急なクライアントの仕様変更にもなんなく対応できます。

#仕様変更に対応
プロフィールとホームの間にお知らせを追加して!
あと、ログインボタンと利用規約を下に追加して!

よくあります。
サイドメニューへの項目追加くらい簡単でしょ?と思われているからなんですねー。
これを今回の管理方法を適用して先程のコードを改修するとこうなります。

※変更箇所だけ抜粋

SideMenuViewController.swift
//セクション配列に使うenumの定義
enum SideMenuSecitonType {
    case profile    //プロフィール項目
    case news       //お知らせ
    case home       //ホーム項目
    case login      //お知らせ
    case tos        //利用規約
}

~~
    /// セクション配列を作成する
    func createSectionData() {
        sections = [.profile,
                    .news,
                    .home,
                    .login,
                    .tos]
    }

    //セル生成
    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "Snorlax", for: indexPath)
        
        let section = sections[indexPath.section] //セクション配列から引っ張る
        var titleString = ""
        
        switch section {
        case .profile:
            titleString = "プロフィール"
        case .home:
            titleString = "ホーム"
        case .news:
            titleString = "お知らせ"
        case .login:
            titleString = "ログイン"
        case .tos:
            titleString = "利用規約"
        }
        
        cell.textLabel?.text = titleString
        
        return cell
    }

基本的には追加されたものをsections配列に追加して
後はそのcaseに応じた処理を追加する。ってだけで対処できると思います。

コード全文を見たい方はこちらからどぞ
https://github.com/y-satou/SampleSectionTableView

#最後に
もっと良いsection/rowの管理方法があればコメントにて教えてくださると嬉しいです(´・ω・`)

今回が初投稿となりました。これから実践的な業務で使えそうなものをつらつらと書いて投稿していきたいなーって思っています!
では。

5
7
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?