-1

My problem is I returned the rows to be count, as a result when I try to run my code, the array in the UserDefaults is only evaluated to the first 4 objects. And even if the if statement = false, the. row will have a title as title and detailText as subtitle

Problems are:

  • ooo (long array) starts from 0 when doing indexpath.row and goes to count

  • if statement always is evaluated

    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
     }
    
  • 1
    Unrelated but using `UserDefaults` as data source is horrible. Don't do that. – vadian Jul 29 '20 at 07:41
  • I do not store very secret information though – Benjamin Sloutsky Jul 29 '20 at 07:42
  • It isn't clear what you are trying to do. You should fetch your array once and have that in a property. What is `dat`? Are you trying to filter your array? You should filter once and ensure that `numberOfRows` returns the correct number of rows. – Paulw11 Jul 29 '20 at 07:56
  • dat is the date that was pressed on calendar – Benjamin Sloutsky Jul 29 '20 at 07:56
  • I am trying to take the index of dat and use it for ooo – Benjamin Sloutsky Jul 29 '20 at 07:57
  • Why are you reading UserDefaults so many times instead of using a class property and why many arrays instead of using a custom struct for your data, this code will be very hard to maintain. You really need to look over your data model – Joakim Danielson Jul 29 '20 at 07:57
  • 1
    Right, so you should use that to produce a filtered array that you save in a property and that array should provide the data for your table. You then reload the whole table – Paulw11 Jul 29 '20 at 07:57
  • @Paulw11, I do not understand, I tried doing this ```func filterCont(){ self.newArr = self.ooo.filter{ user in let usern = user as? String return(usern?.lowercased().contains(dat.lowercased()))! } tableView.reloadData() print("yarr \(newArr)") }``` can you please demonstrate? – Benjamin Sloutsky Jul 29 '20 at 17:34
  • That looks about right; You then need to ensure that your `numberOfRows` and `cellForRow` use `self.newArr` – Paulw11 Jul 29 '20 at 23:37
  • I solved my problem thanks Paul! – Benjamin Sloutsky Jul 30 '20 at 02:40

1 Answers1

0

Thanks to everyone I found a problem,I accidentally inserted an extra if statement. The resulting code is:

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
}