var title = ["cell"] var subname = [String]() @IBAction func addbtr(_ sender: Any) { let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert) let okAction = UIAlertAction(title: "OK", style: .default, handler: { (action:UIAlertAction!) -> Void in if let textFields = alert.textFields { for textField in textFields { self.subtitlename.insert(textField.text!, at:0) } self.mytableView.reloadData() } }) alert.addAction(okAction) let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) alert.addAction(cancelAction) alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in textField.placeholder = "テキスト" }) alert.view.setNeedsLayout() self.present(alert, animated: true, completion: nil) }
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) cell.textLabel?[1].text = subtitlename[indexPath.row] cell.textLabel?[0].text = title[indexPath.row] return cell }
alerttextで入力したものとは別にvar title = ["cell"]
であらかじめ設定した内容と一緒にcellにデータを反映させ方が分からなくて困っています。
試したこと
cell.textLabel?[1].text = subtitlename[indexPath.row] cell.textLabel?[0].text = title[indexPath.row]
このようにtextLabel?[0],[1]にしてみればいいんじゃないかと試したら以下のようなエラーが起きました。
イメージとしてはこんな感じです。
alerttextで入力したのが猫で、cellが毎回入力したものと一緒に出てくるようにしたいです。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/09/27 03:05
2017/09/27 03:48
2017/09/27 04:49
2017/09/27 04:55
2017/09/27 13:13