If statement always evaluated, something always prints in cell
If statement always evaluated, something always prints in cell
我的问题是我将行返回为 count
,结果当我尝试 运行 我的代码时,UserDefaults 中的数组仅评估为前 4 [=29] =].而且即使if语句=false,。 row 的标题为 title
,detailText 为 subtitle
问题是:
ooo(长数组)做indexpath.row时从0开始,到count
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
}
我的问题是我将行返回为 count
,结果当我尝试 运行 我的代码时,UserDefaults 中的数组仅评估为前 4 [=29] =].而且即使if语句=false,。 row 的标题为 title
,detailText 为 subtitle
问题是:
ooo(长数组)做indexpath.row时从0开始,到count
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
}