0
1

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 3 years have passed since last update.

【Swift】TableViewのカスタムCellについて

Last updated at Posted at 2021-05-16

2021/05/23 修正
・修正点 class内に記述していたものを継承させるようにしました。

なんかサイト通りに作っても動かん

こんな経験があるので、自分のために確実に動作するTableViewをメモしておきます。 navigationでtableviewを作ってcellを拡張しやすいxibで作る。

ViewController.swift(tableviewがあるnavigationview)

ViewController.swift
import UIKit

class ViewController: UIViewController{

     @IBOutlet weak var tableView: UITableView!
     //cellListの中身が出力されます
     var cellList : [menu] = [menu]()

     override func viewDidLoad() {
          super.viewDidLoad()
          //tableviewのデータをどこで扱うか
          tableView.dataSource = self
          tableView.delegate = self
          tableView.register(UINib(nibName: "TableViewCell", bundle: nil), forCellReuseIdentifier: "TableViewCell")
          setData()
     }

}

//デリゲートとデータソースを継承させるよ!
extension ViewController:UITableViewDelegate,UITableViewDataSource{
    func setData() {
        cellList = [menu(name: "AAA"),menu(name: "BBB")]
    }
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
          //何個入れるよ!って指示
          return cellList.count
     }
     
     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
          //つかうcellを定義
          let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell
          //そのcellに値を入れる
          cell.setCellData(menu: cellList[indexPath.row])
          return cell
     }

     func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
          // セルの選択を解除
          tableView.deselectRow(at: indexPath, animated: true)

          // 別の画面に遷移(ストーリーボードのIdentityに"secondと書いてある")
          let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "second") as! secondViewController
          //次の画面の.swiftに値を渡す
          secondViewController.number = String(indexPath.row)
          //navigation遷移なのでfullScreenに変更
          secondViewController.modalPresentationStyle = .fullScreen
          //self.navigationController?.pushViewController(,animated)はnavigation限定
          self.navigationController?.pushViewController(secondViewController, animated: true)

     }
}

TableViewCell.swift(xibでcellを作るために必要なもの)

TableViewCell.swift
import UIKit

class TableViewCell: UITableViewCell {

     @IBOutlet weak var nameLabel: UILabel!
     override func awakeFromNib() {
          super.awakeFromNib()
          // Initialization code
     }

     //値をセットする
     //引数にクラスを指定する。
     //これはViewControllerから使う
     func setCellData(menu:menu){
          nameLabel.text = menu.name as String
     }

     override func setSelected(_ selected: Bool, animated: Bool) {
          super.setSelected(selected, animated: animated)
          // Configure the view for the selected state
     }
}

menu.swift(Jsonとかを使ってtableviewに値入れるなら絶対必要)

menu.swift
import Foundation

class menu : NSObject{
     var name :String

     init(name:String){
          self.name = name as String
     }
}

secondViewController.swift(cellを押したときに情報をもって出てくる次の画面)

secondViewController.swift
import UIKit

class secondViewController: UIViewController {
     @IBOutlet weak var label: UILabel!
     var number :String!

     override func viewDidLoad() {
          super.viewDidLoad()

          // Do any additional setup after loading the view.
          label.text = number
     }
}

最後に

iOSアプリ開発をしています。
主にSwiftですが、最近は熱が入ってきてFlutterも🦾
色々やってます。もし良かったら見てってください。

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?