我正在尝试在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)

最佳答案

UITableViewCell的默认标识符是“ Cell”,而不是“ cell”,这就是为什么当您尝试重新加载tableView数据时您的应用程序崩溃的原因。

更改此:

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


有了这个:

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

关于ios - 如何在TableViewController中显示Firestore文档名称?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53241580/

10-16 10:56