Как я могу показать имена документов Firestore в TableViewController? - PullRequest
0 голосов
/ 10 ноября 2018

Я пытаюсь загрузить имена моих документов в Firestore и показать их в моем tableViewController. Каждый раз, когда я пытаюсь получить сообщение об ошибке, я не знаю, откуда оно. Я много чего перепробовал и не могу найти решение. Методы делегата вызываются до загрузки информации, но если я перезагружаю tableView, приложение вылетает. Если вы можете помочь мне, спасибо! :)

class ViewController: UITableViewController {




var db:Firestore!
var docArray = [String]()




override func viewDidLoad() {
    super.viewDidLoad()

    navigationController?.navigationBar.prefersLargeTitles = true

    db = Firestore.firestore()
    loadData()

    DispatchQueue.main.async {
        self.tableView.reloadData()
    }
}

func loadData(){

    print("loaded")
    db.collection("Producten").getDocuments { (snap, error) in

        for doc in (snap?.documents)! {
            self.docArray.append(doc.documentID)
        }

        DispatchQueue.main.async {
            self.tableView.reloadData()
        }
    }

}

override func numberOfSections(in tableView: UITableView) -> Int {

    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return docArray.count
}

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

    print(docArray)

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    cell.textLabel?.text = docArray[indexPath.row]
    return cell
}
}

журнал сбоев:

2018-11-10 19:31:17.551261+0100 Den Hartog[57326:4767154] *** Assertion 
failure in [UITableView_dequeueReusableCellWithIdentifier:forIndexPath:usingPresentationValues:], 
/BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKitCore_Sim/UIKit- 
3698.93.8/UITableView.m:8054
2018-11-10 19:31:17.559180+0100 Den Hartog[57326:4767154] *** 
Terminating app due to uncaught exception 
'NSInternalInconsistencyException', reason: 'unable to dequeue a cell 
with identifier cell - must register a nib or a class for the 
identifier or connect a prototype cell in a storyboard'
*** First throw call stack:

0   CoreFoundation                      0x000000010fce01bb __exceptionPreprocess + 331
1   libobjc.A.dylib                     0x000000010f27e735 objc_exception_throw + 48
2   CoreFoundation                      0x000000010fcdff42 +[NSException raise:format:arguments:] + 98
3   Foundation                          0x000000010dfef877 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 194
4   UIKitCore                           0x000000011335a5b1 -[UITableView _dequeueReusableCellWithIdentifier:forIndexPath:usingPresentationValues:] + 868
5   UIKitCore                           0x000000011335a219 -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:] + 91
6   Den Hartog                          0x000000010d48b386 $S10Den_Hartog14ViewControllerC05tableC0_12cellForRowAtSo07UITableC4CellCSo0jC0C_10Foundation9IndexPathVtF + 422
7   Den Hartog                          0x000000010d48b5ec $S10Den_Hartog14ViewControllerC05tableC0_12cellForRowAtSo07UITableC4CellCSo0jC0C_10Foundation9IndexPathVtFTo + 108
8   UIKitCore                           0x000000011337566e -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 771
9   UIKitCore                           0x0000000113375bfb -[UITableView _createPreparedCellForGlobalRow:willDisplay:] + 73
10  UIKitCore                           0x000000011333cc36 -[UITableView _updateVisibleCellsNow:isRecursive:] + 2863
11  UIKitCore                           0x000000011335d8ee -[UITableView layoutSubviews] + 165
12  UIKitCore                           0x000000011361b795 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1441
13  QuartzCore                          0x0000000114ba3b19 -[CALayer layoutSublayers] + 175
14  QuartzCore                          0x0000000114ba89d3 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 395
15  QuartzCore                          0x0000000114b217ca _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 342
16  QuartzCore                          0x0000000114b5897e _ZN2CA11Transaction6commitEv + 576
17  QuartzCore                          0x0000000114b596fa _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 76
18  CoreFoundation                      0x000000010fc44c27 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
19  CoreFoundation                      0x000000010fc3f0be __CFRunLoopDoObservers + 430
20  CoreFoundation                      0x000000010fc3f751 __CFRunLoopRun + 1537
21  CoreFoundation                      0x000000010fc3ee11 CFRunLoopRunSpecific + 625
22  GraphicsServices                    0x00000001188c21dd GSEventRunModal + 62
23  UIKitCore                           0x000000011313181d UIApplicationMain + 140
24  Den Hartog                          0x000000010d48e7c7 main + 71
25  libdyld.dylib                       0x0000000110d99575 start + 1
26  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type 
NSException
(lldb) 

1 Ответ

0 голосов
/ 10 ноября 2018

Идентификатор UITableViewCell по умолчанию - «Ячейка», а не «Ячейка», и именно поэтому ваше приложение падает при попытке перезагрузить данные tableView.

Изменить это:

let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

С этим:

let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
...