我的问题是,我将行返回为count
,因此,当我尝试运行代码时,UserDefaults中的数组只计算为前4个对象。即使if语句为false。行的标题为title
,detailText为subtitle
问题是:
-
ooo(长数组(在执行indexpath.row时从0开始,然后开始计数
-
if语句总是评估
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "eventO", for: indexPath) rol = (UserDefaults.standard.stringArray(forKey: "data") ?? [String]())[indexPath.row] self.title = dat let ooo = (UserDefaults.standard.stringArray(forKey: "data") ?? [String]()) ///This function starts from 0 when doing indexpath.row and goes to count ///This is the main problem if ooo != []{ if ooo[indexPath.row] == dat{ cell.textLabel?.text = (UserDefaults.standard.stringArray(forKey: "array") ?? [String]())[indexPath.row] cell.detailTextLabel?.text = (UserDefaults.standard.stringArray(forKey: "dae") ?? [String]())[indexPath.row] + ": " + (UserDefaults.standard.stringArray(forKey: "array2") ?? [String]())[indexPath.row] } } return cell }
感谢所有发现问题的人,我意外地插入了一个额外的if语句。生成的代码是:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "eventO", for: indexPath)
let gog = UserDefaults.standard.stringArray(forKey: "array") ?? [String]()
let log = UserDefaults.standard.stringArray(forKey: "array2") ?? [String]()
let rog = UserDefaults.standard.stringArray(forKey: "dae") ?? [String]()
for i in 0...ooo.count-1{
if ooo[i] == dat{
see.append(gog[i])
lee.append(log[i])
mee.append(rog[i])
}
}
rol = (UserDefaults.standard.stringArray(forKey: "data") ?? [String]())[indexPath.row]
self.title = dat
if ooo != []{
cell.textLabel?.text = see[indexPath.row]
cell.detailTextLabel?.text = mee[indexPath.row] + ": " + lee[indexPath.row]
}
return cell
}