fatal error: Array index out of range in uitableview on back to previous controller swift

谁说胖子不能爱 提交于 2019-12-11 11:47:02

问题


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:


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



来源:https://stackoverflow.com/questions/29972336/fatal-error-array-index-out-of-range-in-uitableview-on-back-to-previous-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!