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.

Segueを使わずにセルの値を遷移先へ送る方法/セルの一つにチェックマーク入れる、外す方法

Last updated at Posted at 2021-02-01

送る側

SolvedViewController.swift


func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let storyboard = UIStoryboard(name: "Detail", bundle: nil)
    //DetailVCインスタンスを作成するのと、キャスト
    //ここのasがないとnextViewのデータ型とDetailVCの型が異なるのでエラーが起きる
    if let nextVC = storyboard.instantiateInitialViewController() as? DetailViewController {
        nextVC.questionTitle = quizList[indexPath.row].question
        present(nextVC, animated: true, completion: nil)
    }
}

受け取る側

DetailViewController.swift
@IBOutlet weak var questionLabel: UILabel!
//SolvedVCから送られてくるセルの情報を入れるための箱を作る
var questionTitle: String?
    
override func viewDidLoad() {
    super.viewDidLoad()
    //SolvedViewControllerと接続したLabel.textに、送られてきた情報を入れる
    questionLabel.text = questionTitle
}

すげー悩んだので備忘録的な感じで書きました。なのでだいぶ短いです。
もっと広く書く必要が出たら追記します。

セルにチェックマーク入れる方法

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
}

セルの「一つのみ」にチェックマークを入れるようにするにはこれも追記

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        tableView.cellForRow(at: indexPath)?.accessoryType = .none
}
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?