【Swift】TableViewのdetailTextLabelとaccessoryType
完成図
ソースコード
import UIKit class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource { @IBOutlet weak var tableView: UITableView! override func viewDidLoad() { super.viewDidLoad() tableView.delegate = self tableView.dataSource = self } //セクションの数 func numberOfSections(in tableView: UITableView) -> Int { return 1 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 4 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { var cell = UITableViewCell() switch indexPath.row { case 0: //detailTextLabelが表示されない cell = UITableViewCell(style: .default, reuseIdentifier: "cell1") //右向き矢印 cell.accessoryType = .disclosureIndicator case 1: //detailTextLabelが右側に cell = UITableViewCell(style: .value1, reuseIdentifier: "cell1") //右向き矢印+詳細ボタん cell.accessoryType = .detailDisclosureButton case 2: //textLabelが左に青く、detailTextLabelが中央に cell = UITableViewCell(style: .value2, reuseIdentifier: "cell1") //詳細ボタン cell.accessoryType = .detailButton case 3: //textLabelを上に、detailTextLabelを下に cell = UITableViewCell(style: .subtitle, reuseIdentifier: "cell1") //チェックマーク cell.accessoryType = .checkmark default: break } //textLabelにテキストを代入 cell.textLabel?.text = "text" //detailTextLabelにテキストを代入 cell.detailTextLabel?.text = "detail\(indexPath.row)" return cell } }