1

I have a UITableview that contains a search bar, with my filter everything works perfectly. When I click on a cell to pass values to another UIViewController and open the UIViewController at the same time, everything still works perfectly. But my problem is when I click to back button to get back to the the UITableViewController, I get this error:

Array index out of range

From this line of code :

cell.textLabel?.text = filtered[indexPath.row]

Here's all of my code:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{

    let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell;
    if(searchActive){
        cell.textLabel?.text = filtered[indexPath.row]

    } else {


        var aaa = posts1.objectAtIndex(indexPath.row).valueForKey("speedKPH") as NSString
        let  speeddouble = NSString(string: aaa).intValue
        var aa = String(speeddouble)

        var bb = posts1.objectAtIndex(indexPath.row).valueForKey("timestamp") as NSString


        var now = (NSDate(timeIntervalSince1970:(bb as NSString).doubleValue))
        var formatter = NSDateFormatter()
        formatter.dateFormat = "dd-MM-yyyy' 'HH:mm' '"
        formatter.timeZone = NSTimeZone(forSecondsFromGMT: 0)


        cell.textLabel?.text = posts.objectAtIndex(indexPath.row).valueForKey("description") as NSString + "\n"+"Velocidad : " + aa+" KM/H  /  Ult. Act. :" + formatter.stringFromDate(now)
        cell.detailTextLabel?.text = posts.objectAtIndex(indexPath.row).valueForKey("deviceID") as NSString

    }
    SwiftSpinner.hide()
    return cell;

}

} Please Help!

  • 1
    obviously your filtered array does not have a value for the cell. Try debugging the amount of cells you have and how many items are inside your array – milo526 Apr 30 '15 at 15:54
  • the filter works perfectly, only when i finished to search something, i go to otherviewcontroller, when i want to get back to the previous viewcontroller that contains the filter it shows: fatal error: Array index out of range in uitableview – Joel Felix ST JOY Apr 30 '15 at 15:57
  • Do you clear the array in the mean time or does it get cleared due too memory issues – milo526 Apr 30 '15 at 16:01
  • yes i did it, but it doesn't work – Joel Felix ST JOY Apr 30 '15 at 16:03
  • should searchActive be true when you return or do you want the filtered results to be false? Where are you initalizing your filtered array? I'm guessing it's emptying when returning the the view and the line `cell.textLabel?.text = filtered[indexPath.row]` is causing the crash because indexPath.row is greater than the filtered.count – Chackle May 01 '15 at 15:10
  • Bro i updated my code, i put the complete code of the tableviewcontroller to see if you can help me, how do i control the searchActive. Please help – Joel Felix ST JOY May 01 '15 at 15:31
  • I would like my search active be true when i get back to the previous activity. But it always shows with error. – Joel Felix ST JOY May 01 '15 at 15:58
  • @milo526 I updated my code, I would like my search active be true when i get back to the previous activity. But it always shows with error. – Joel Felix ST JOY May 01 '15 at 18:46

1 Answers1

0

I resolved it, i only change to true the search active like this: func searchBarTextDidEndEditing(searchBar: UISearchBar) { searchActive = true; }