タイトルとは少し違いますが下記のコードの if cell.skillName.text! == i { print("確認3") }
のコードでデバックエリアに「確認3」と出力したいのですが出ない理由はなんですか
確認のコード
import UIKit
class TableViewController: UITableViewController {
@IBOutlet var myTableView: UITableView!
var array : Array<String> = ["\(UserDefaults.standard.object(forKey: "fastTitleKey") as! String)"]
let arrayNameKey = "arrayNameKey"
override func viewDidLoad() {
super.viewDidLoad()
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "customTableViewCell", for: indexPath) as! costomViewCell
cell.skillName.text = "\(array[indexPath.row])"
cell.goalCountLabel.text = secondsToGoalTimerLabel(UserDefaults.standard.integer(forKey: "goalCountKey"))
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let storyboard : UIStoryboard = self.storyboard!
let nextView =
storyboard.instantiateViewController(withIdentifier: "ViewController")
let navi = UINavigationController(rootViewController: nextView)
let cell = tableView.dequeueReusableCell(withIdentifier: "customTableViewCell", for: indexPath) as! costomViewCell
print("確認1")
if let arr = UserDefaults.standard.array(forKey: self.arrayNameKey) {
print("確認2")
let arrConversion = arr as! [String]
print(arrConversion)
for i in arrConversion {
if cell.skillName.text! == i {
print("確認3")
}
}
}
present(navi, animated: true, completion: nil)
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
array.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
UserDefaults.standard.set(self.array, forKey: self.arrayNameKey)
}
}
@IBAction func addSkillButton(_ sender: UIBarButtonItem) {
var alertTextFeld: UITextField?
let alert = UIAlertController(title: "Skill Name", message: "Enter new name", preferredStyle: UIAlertController.Style.alert)
alert.addTextField { (textField: UITextField!) in
alertTextFeld = textField
}
alert.addAction(UIAlertAction(title: "キャンセル", style: .cancel, handler: nil))
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { _ in
if let text = alertTextFeld?.text {
self.array.append(text)
UserDefaults.standard.set(self.array, forKey: self.arrayNameKey)
self.myTableView.reloadData()
}
}))
self.present(alert, animated: true, completion: nil)
}
@IBAction func secret(_ sender: UIBarButtonItem) {
}
func saveDate() {
}
}
カスタムセルコード
import UIKit
class costomViewCell: UITableViewCell {
@IBOutlet weak var skillName: UILabel!
@IBOutlet weak var goalCountLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}