将 Parse.com 数据加载到 TableView 单元格时应用终止

App Terminated when Loading Parse.com data into TableView Cell

将解析数据加载到 tableView 单元格时出现问题。我认为问题是因为 tableView 单元格。

我这几年用ObjectC写iOS app

我已检查故事板中的所有连接和标识符是否已正确连接和写入。


import UIKit

class TrendingTableViewCell: UITableViewCell {
    
    @IBOutlet weak var Label1: UILabel!
    @IBOutlet weak var ImageView1: UIImageView!
}


class Trending: UIViewController {
    
    @IBOutlet weak var TableView: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        getdatafromparse()
    }
    
    var Saveddata: [UIImage] = []
    var Savetext1: [String] = []
    
    func getdatafromparse(){
        let query = PFQuery(className: "Content")
        //query.whereKey("userId", equalTo: (PFUser.current()?.objectId!)!)
        
        query.findObjectsInBackground(block: { (objects, error) in
            
            if let posts = objects{
                for object in posts {
                    if let post = object as? PFObject {
                        
                        self.Savetext1.append(post["heading"] as! String)
                        
                        //self.taskId.append(object.objectId!)
                        
                        
                        
                        
                    }
                    
                }
             self.TableView.reloadData()
            }
            
        })
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier:"PostCell")as! TrendingTableViewCell
        
        cell.Label1!.text = Savetext1[indexPath.row]
        //cell.ImageView1?.image = Saveddata[indexPath.row]
        return cell
        
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.Savetext1.count
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
}

错误日志:

*** Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3600.8.1/UITableView.m:8174

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView (<UITableView: 0x101039000; frame = (0 121; 375 546); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x17024d770>; layer = <CALayer: 0x170033560>; contentOffset: {0, 0}; contentSize: {375, 8700}>) failed to obtain a cell from its dataSource ()'

试试这个 UITableViewDelegateUITableViewDataSource

class Trending: UIViewController, UITableViewDelegate, UITableViewDataSource {


}

您忘记将 Parse 库导入文件。