LoginSignup
2
2

More than 5 years have passed since last update.

④TableView ViewController 土台

Last updated at Posted at 2016-08-28

④TableView ViewController
土台

Xcode version 7.3.1 And Xcode 8 GM seed

001.png

002.png

003.png

ViewController.swift
class ViewController: UIViewController,UITableViewDelegate, UITableViewDataSource ,NonHeaderCallBack{


    @IBOutlet weak var nonTableView: UITableView!

    var checkAllStatusItems: [Bool] = [false,false,false];

    private let nonHeaderSection: NSArray = ["HEADER 1", "HEADER 2", "HEADER 3"]
    private let nonCategorySection: NSArray = ["Category 1","Category 2","Category 3"]
    private let nonFukuokaItems: NSArray = ["Fukuoka 1", "Fukuoka 2", "Fukuoka 3", "Fukuoka 4", "Fukuoka 5", "Fukuoka 6", "Fukuoka 7", "Fukuoka 8", "Fukuoka 9"]
    private let nonKumamotoItems: NSArray = ["Kumamoto 1", "Kumamoto 2", "Kumamoto 3", "Kumamoto 4", "Kumamoto 5", "Kumamoto 6", "Kumamoto 7"]
    private let nonKagoshimaItems: NSArray = ["Nagasaki 1","Nagasaki 2","Nagasaki 3","Nagasaki 4","Nagasaki 5","Nagasaki 6","Nagasaki 7","Nagasaki 8"]


    override func viewDidLoad() {
        super.viewDidLoad()

        //カスタムセルを設定する
        let nib = UINib(nibName: "nonCustomTableViewCell", bundle: nil)
        nonTableView.registerNib(nib, forCellReuseIdentifier: "nonCell")
        nonTableView.scrollEnabled = true
        nonTableView.layer.borderColor = UIColor.blueColor().CGColor
        nonTableView.layer.borderWidth = 1;

        let headerNib = UINib(nibName: "nonHeaderTableCell", bundle: nil)
        nonTableView.registerNib(headerNib, forCellReuseIdentifier: "nonHeaderCell")

        let categoryNib = UINib(nibName: "nonCategoryTableCell", bundle: nil)
        nonTableView.registerNib(categoryNib, forCellReuseIdentifier: "nonCategoryCell")

        //デリゲート
        nonTableView.dataSource = self
        nonTableView.delegate = self
        self.view.addSubview(nonTableView)

    }

    /*
     選択しちゃうエリアのボタンを押した時のイベント チェックON、OFF画像の差し替え
     */
    func didchckAllTap(sender: AnyObject){
        let button :UIButton = sender as! UIButton
        checkAllStatusItems[button.tag] = !checkAllStatusItems[button.tag]
        print("[button.tag:\(button.tag)")
        print("[checkAllStatusItems[button.tag]:\(checkAllStatusItems[button.tag])")
        //再表示
        self.nonTableView.reloadData()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    /*
     セクションの数を返す.
     */
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return nonHeaderSection.count
    }


    /*
     セクションのタイトルを返す.
     */
    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

        let header = tableView.dequeueReusableCellWithIdentifier("nonHeaderCell") as!
        nonHeaderTableCell
        header.delegate = self

        header.allCheckImageVIew.image = getImageView(checkAllStatusItems[section])
        header.allButton.tag = section
        header.nonHeaderLabel.text = nonHeaderSection[section] as? String

        return header
    }

    func getImageView(target: Bool) ->UIImage {
        return (target ? UIImage(named: "image_check_on.png") : UIImage(named: "image_check_off.png"))!
    }


    /*
     テーブルに表示する配列の総数を返す.
     */
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0 {
            return nonFukuokaItems.count + 1
        } else if section == 1 {
            return nonKumamotoItems.count + 1
        } else if section == 2 {
            return nonKagoshimaItems.count + 1
        } else {
            return 0
        }
    }

    /*
     Cellに値を設定する.
     */
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        // <Cellのカスタムクラス名> という記述を追加
        let cell = tableView.dequeueReusableCellWithIdentifier("nonCell") as? nonCustomTableViewCell
        let categoryCell = tableView.dequeueReusableCellWithIdentifier("nonCategoryCell") as? nonCategoryTableCell

        switch indexPath.section {
        case 0:
            //print("[indexPath.row:\(indexPath.row)")
            if(indexPath.row == 0){
                categoryCell?.nonCategoryLabel.text = "\(nonCategorySection[0])"
                return categoryCell!
            }else{
                cell?.nonLabel.text = "\(nonFukuokaItems[indexPath.row - 1])"
                return cell!
            }
        case 1:
            //print("[indexPath.row:\(indexPath.row)")
            if(indexPath.row == 0){
                categoryCell?.nonCategoryLabel.text = "\(nonCategorySection[1])"
                return categoryCell!
            }else{
                cell?.nonLabel.text = "\(nonKumamotoItems[indexPath.row - 1])"
                return cell!
            }

        case 2:
            //print("[indexPath.row:\(indexPath.row)")
            if(indexPath.row == 0){
                categoryCell?.nonCategoryLabel.text = "\(nonCategorySection[2])"
                return categoryCell!
            }else{
                cell?.nonLabel.text = "\(nonKagoshimaItems[indexPath.row - 1])"
                return cell!
            }
        default:
            return cell!
        }        

    }

    /*
     Cell が選択された時
     */
    func tableView(table: UITableView, didSelectRowAtIndexPath indexPath:NSIndexPath) {
        print("[indexPath.row:\(indexPath.row)")

    }


}

nonHeaderTableCell.swift
import UIKit

protocol NonHeaderCallBack : class {
    func didchckAllTap(sender: AnyObject)
}

class nonHeaderTableCell: UITableViewCell {

    weak var delegate: NonHeaderCallBack! = nil

    @IBOutlet weak var nonHeaderLabel: UILabel!

    @IBOutlet weak var allButton: UIButton!

    @IBOutlet weak var allCheckImageVIew: UIImageView!

    @IBAction func onClickAllButton(sender: AnyObject) {

        self.delegate?.didchckAllTap(sender)
    }
}

Xcode 8 GM seed
⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️

ViewController.swift
import UIKit

class ViewController: UIViewController,UITableViewDelegate, UITableViewDataSource ,NonHeaderCallBack{


    @IBOutlet weak var nonTableView: UITableView!

    var checkAllStatusItems: [Bool] = [false,false,false];

    fileprivate let nonHeaderSection: NSArray = ["HEADER 1", "HEADER 2", "HEADER 3"]
    fileprivate let nonCategorySection: NSArray = ["Category 1","Category 2","Category 3"]
    fileprivate let nonFukuokaItems: NSArray = ["Fukuoka 1", "Fukuoka 2", "Fukuoka 3", "Fukuoka 4", "Fukuoka 5", "Fukuoka 6", "Fukuoka 7", "Fukuoka 8", "Fukuoka 9"]
    fileprivate let nonKumamotoItems: NSArray = ["Kumamoto 1", "Kumamoto 2", "Kumamoto 3", "Kumamoto 4", "Kumamoto 5", "Kumamoto 6", "Kumamoto 7"]
    fileprivate let nonKagoshimaItems: NSArray = ["Nagasaki 1","Nagasaki 2","Nagasaki 3","Nagasaki 4","Nagasaki 5","Nagasaki 6","Nagasaki 7","Nagasaki 8"]


    override func viewDidLoad() {
        super.viewDidLoad()

        //カスタムセルを設定する
        let nib = UINib(nibName: "nonCustomTableViewCell", bundle: nil)
        nonTableView.register(nib, forCellReuseIdentifier: "nonCell")
        nonTableView.isScrollEnabled = true
        nonTableView.layer.borderColor = UIColor.blue.cgColor
        nonTableView.layer.borderWidth = 1;

        let headerNib = UINib(nibName: "nonHeaderTableCell", bundle: nil)
        nonTableView.register(headerNib, forCellReuseIdentifier: "nonHeaderCell")

        let categoryNib = UINib(nibName: "nonCategoryTableCell", bundle: nil)
        nonTableView.register(categoryNib, forCellReuseIdentifier: "nonCategoryCell")

        //デリゲート
        nonTableView.dataSource = self
        nonTableView.delegate = self
        self.view.addSubview(nonTableView)

    }

    /*
     選択しちゃうエリアのボタンを押した時のイベント チェックON、OFF画像の差し替え
     */
    func didchckAllTap(_ sender: AnyObject){
        let button :UIButton = sender as! UIButton
        checkAllStatusItems[button.tag] = !checkAllStatusItems[button.tag]
        print("[button.tag:\(button.tag)")
        print("[checkAllStatusItems[button.tag]:\(checkAllStatusItems[button.tag])")
        //再表示
        self.nonTableView.reloadData()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    /*
     セクションの数を返す.
     */
    func numberOfSections(in tableView: UITableView) -> Int {
        return nonHeaderSection.count
    }


    /*
     セクションのタイトルを返す.
     */
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

        let header = tableView.dequeueReusableCell(withIdentifier: "nonHeaderCell") as!
        nonHeaderTableCell
        header.delegate = self

        header.allCheckImageVIew.image = getImageView(checkAllStatusItems[section])
        header.allButton.tag = section
        header.nonHeaderLabel.text = nonHeaderSection[section] as? String

        return header
    }

    func getImageView(_ target: Bool) ->UIImage {
        return (target ? UIImage(named: "image_check_on.png") : UIImage(named: "image_check_off.png"))!
    }


    /*
     テーブルに表示する配列の総数を返す.
     */
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0 {
            return nonFukuokaItems.count + 1
        } else if section == 1 {
            return nonKumamotoItems.count + 1
        } else if section == 2 {
            return nonKagoshimaItems.count + 1
        } else {
            return 0
        }
    }

    /*
     Cellに値を設定する.
     */
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        // <Cellのカスタムクラス名> という記述を追加
        let cell = tableView.dequeueReusableCell(withIdentifier: "nonCell") as? nonCustomTableViewCell
        let categoryCell = tableView.dequeueReusableCell(withIdentifier: "nonCategoryCell") as? nonCategoryTableCell

        switch (indexPath as NSIndexPath).section {
        case 0:
            //print("[indexPath.row:\(indexPath.row)")
            if((indexPath as NSIndexPath).row == 0){
                categoryCell?.nonCategoryLabel.text = "\(nonCategorySection[0])"
                return categoryCell!
            }else{
                cell?.nonLabel.text = "\(nonFukuokaItems[(indexPath as NSIndexPath).row - 1])"
                return cell!
            }
        case 1:
            //print("[indexPath.row:\(indexPath.row)")
            if((indexPath as NSIndexPath).row == 0){
                categoryCell?.nonCategoryLabel.text = "\(nonCategorySection[1])"
                return categoryCell!
            }else{
                cell?.nonLabel.text = "\(nonKumamotoItems[(indexPath as NSIndexPath).row - 1])"
                return cell!
            }

        case 2:
            //print("[indexPath.row:\(indexPath.row)")
            if((indexPath as NSIndexPath).row == 0){
                categoryCell?.nonCategoryLabel.text = "\(nonCategorySection[2])"
                return categoryCell!
            }else{
                cell?.nonLabel.text = "\(nonKagoshimaItems[(indexPath as NSIndexPath).row - 1])"
                return cell!
            }
        default:
            return cell!
        }        

    }

    /*
     Cell が選択された時
     */
    func tableView(_ table: UITableView, didSelectRowAt indexPath:IndexPath) {
        print("[indexPath.row:\((indexPath as NSIndexPath).row)")

    }
nonHeaderTableCell.swift
import UIKit

protocol NonHeaderCallBack : class {
    func didchckAllTap(_ sender: AnyObject)
}

class nonHeaderTableCell: UITableViewCell {

    weak var delegate: NonHeaderCallBack! = nil

    @IBOutlet weak var nonHeaderLabel: UILabel!

    @IBOutlet weak var allButton: UIButton!

    @IBOutlet weak var allCheckImageVIew: UIImageView!

    @IBAction func onClickAllButton(_ sender: AnyObject) {

        self.delegate?.didchckAllTap(sender)
    }
}
2
2
0

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
2
2