2017-08-21 19 views
0

我有一个关于我的tableView的问题。
当我频繁推/ ChatListViewController和detailViewcontroller之间,我会崩溃,并在ListViewController中得到错误。
但我不知道我的数据在哪里崩溃。
我想我是否使用GCD来实现它?
有什么建议可以避免吗?
谢谢。
获取错误“索引超出范围”当我经常在实际设备中推/拉视图控制器 - Swift

崩溃日志:
致命错误:超出范围的索引 ![enter image description here ![enter image description here

型号:

class ChatroomList:Model { 

    var all:[Chatroom] { 
    var rooms:[Chatroom] = [Chatroom]() 
    self.chatrooms.forEach({ (id,chatroom) in 
     if showType.contains(chatroom.type) { 
      rooms.append(chatroom) 
     } 
    }) 
    return rooms 
    } 
} 

的ViewController:

import RxCocoa 
import RxSwift 
import Alamofire 

class ListViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {      

    let chatrooms:ChatroomList = ChatroomList() 
    var list:[Chatroom] = [Chatroom]() 
    var subscribe:Disposable? 

    override func viewDidLoad() { 
    super.viewDidLoad() 

    self.tableView.dataSource = self 
    self.tableView.delegate = self 

    } 

    override func viewWillAppear(_ animated: Bool) { 
     super.viewWillAppear(animated) 

     subscribe = rooms.notifySubject.subscribe({ json in 
       self.loadContents() 
     }) 
     self.loadContents() 
    } 

    override func viewWillDisappear(_ animated: Bool) { 
     super.viewWillDisappear(animated) 

     subscribe?.dispose() 
    } 

    func loadContents() { 

     var idList:[String] = [] 

     self.list.removeAll() 
     self.list = chatrooms.all 

     guard self.list.isEmpty == false else { 
      return 
     } 

     DispatchQueue.global().async() { 

      self.list = self.list.filter { (chatroom) -> Bool in 
       if chatroom.id.isEmpty { 
        return true 
       } 
       if idList.contains(chatroom.id) { 
        return false 
       } 
       idList.append(chatroom.id) 
       return true 
      } 

      self.list.sort(by: { (a,b) in 
       if a.message.datetime.isEmpty { 
        return false 
       } 

      return a.message.datetime > b.message.datetime 
     }) 

     DispatchQueue.main.async() { 

      self.tableView.reloadData() 
     } 
     } 
    } 
} 
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return list.count 
} 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    if list[indexPath.row].type == .city { 

     let cell: ChatroomCityTableViewCell = ChatroomCityTableViewCell(style: .default, reuseIdentifier: nil) 

     cell.loadByCityChatroom(chatroom: list[indexPath.row], cityId: list[indexPath.row].cityId) 

     return cell 
    }else{ 

     let cell: ChatroomTableViewCell = ChatroomTableViewCell(style: .default, reuseIdentifier: nil) 
     cell.loadByChatroom(chatroom: list[indexPath.row]) 

     return cell 
    } 
} 
+0

发布您的崩溃日志和您的崩溃stacktrace –

+0

我没有太多的崩溃日志。我更新。 –

+0

ChatroomListViewController发生错误? – 3stud1ant3

回答

0

启用僵尸在你的计划的对象。

选择您的应用程序方案 - >运行 - >诊断 - >检查僵尸对象。

立即运行您的应用程序。这会给你更准确的关于你的崩溃的信息。

enter image description here

希望这有助于:)

+0

对我没有帮助。谢谢 –

+0

@tolerate_Me_Thx好的。启用僵尸对象将为您提供发生崩溃的原因和方法。你提到索引超出范围,是否发生在cellForRowAt方法中? – Basheer

0

你可以找到很多相关的信息参考self闭合及其影响内部在互联网上。在没有深入细节的情况下,我只是建议使用[weak self],在这里你可以在关闭中引用self。类似于

closure { [weak self] _ in 
    self?.whatever() 
}