在 SWIFT 的表视图中填充自定义单元格时出错

Posted

技术标签:

【中文标题】在 SWIFT 的表视图中填充自定义单元格时出错【英文标题】:Error in populating custom cell in Table View in SWIFT 【发布时间】:2015-12-09 10:40:15 【问题描述】:

我有一个表格视图,我在从我的 API 获取数据后填充它。但现在我收到一条错误消息“源文件中的编辑器占位符”。我只是不知道我在哪里做错了。请给出一些想法?

这是我的代码 -

import UIKit

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource


    @IBOutlet weak var tblvww: UITableView!
    var dict = [:]

    override func viewDidLoad()
    
        super.viewDidLoad()



    func fetchdata()
    
        let url = NSURL(string: "***")
        let request = NSURLRequest(URL: url!)
        let urlsession = NSURLSession.sharedSession()
        let jsonquery = urlsession.dataTaskWithRequest(request)  (data, response, error) -> Void in

            if let retrieveddata = data
            
                self.dict = (try! NSJSONSerialization.JSONObjectWithData(retrieveddata, options: NSJSONReadingOptions.MutableContainers)) as! NSDictionary
            


        
        jsonquery.resume()

    


    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    
        return dict.count
    

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    

       let cell = tableView.dequeueReusableCellWithIdentifier("cellreuse", forIndexPath: indexpath) as! CustomTableViewCell `[![//here I get the error "Editor placeholder in source file"][1]][1]`

        let vendorname = dict.valueForKey("vendor")?.objectAtIndex(0).valueForKey("name")
        cell.vndrlbl.text = vendorname as? String
        return cell


    



我还附上了我的项目截图,让事情更清楚

【问题讨论】:

也许这两个链接对你有帮助? ***.com/questions/31968278/…***.com/questions/33088305/… 我已经读过,但这里的情况有所不同。 您最初是从其他地方粘贴此代码吗? 【参考方案1】:

如果您使用的是 xcode 版本 7.x,请尝试删除 as! CustomTableViewCell 向下转换。只需使用let cell = tableView.dequeueReusableCellWithIdentifier("cellreuse", forIndexPath: indexpath) 就足够了。

【讨论】:

好的,我的编译器如何识别“cell”是我的 customTableView 子类的一种类型?【参考方案2】:

你没有使用indexPath,所以删除它。

使用这个sn-p:

let cell: TestTableViewCell = tableView.dequeueReusableCell(withIdentifier: "CellMate") as! TestTableViewCell

清理并运行项目,尽情享受吧!

【讨论】:

以上是关于在 SWIFT 的表视图中填充自定义单元格时出错的主要内容,如果未能解决你的问题,请参考以下文章

重用自定义 tableView 单元格时重复显示相同的子视图(Swift 4.1)

滚动时自定义uitableviewcell空白

Swift 中带有自定义 TableViewCell 的核心数据图像

为啥在制作自定义表格视图单元格时将可重用单元格出列两次

UITableview 单元格保留自定义图像视图过滤器

在tableView swift的单元格页脚视图中复制行