【问题标题】:creating a custom TableViewCell in swift快速创建自定义 UITableViewCell
【发布时间】:2015-03-24 14:39:29
【问题描述】:

我为程序中的单元格创建了一个自定义类。当我尝试在另一个类中使用它来创建单元格时,我不断收到错误无法使用类型为“(样式:UITableViewCellStyle,reuseIdentifier:StringLiteralConvertible)”的参数列表调用“init”。有人可以在这里指出我正确的方向吗?我真的很感激任何帮助。我尝试将类更改为从 UITableViewController 继承,这样我就可以使用这个var cell: bookCell = self.tableView.dequeueReusableCellWithIdentifier("cell1") as bookCell,但是如果我尝试让类从 tableviewcontroller 继承,它会使程序崩溃。

import UIKit

class bookCell: UITableViewCell {

    @IBOutlet var bookImage: UIImageView!

    @IBOutlet var bookDescription: UILabel!

    @IBOutlet var bookPosterUsername: UILabel!
}


 import UIKit

    class SubjectBooksViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {


        @IBOutlet var navigationBarTitle: UINavigationBar!

        override func viewDidLoad() {
            super.viewDidLoad()

            self.navigationBarTitle.topItem?.title = "\(selectedCourse)"
        }

        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
        }


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

            return 3

        }

         func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

            return 100

        }

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


        var cell : bookCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "subjectCell")

            //var cell: bookCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "subjectCell")
            //var cell: bookCell = self.tableView.dequeueReusableCellWithIdentifier("cell1") as bookCell

            return cell
        }



    }

【问题讨论】:

  • 您应该使用 dequeueReusableCellWithIdentifier 来创建您的单元格。您无需继承 UITableviewController 即可使用该方法。如果这不起作用,请更新您的问题以显示您在自定义单元格类中的代码。
  • 我尝试使用 dequeueReusableCellWithIdentifier 但这给了我一个错误,提示“(UITableView, numberOfRowsInSection: Int) -> Int”没有名为“dequeueReusableCellWithIdentifier”的成员
  • 我以前也看到过这个错误;这不是真的。我想你可能缺了一个“!”在那一行或上面的某处。此外,您应该在 tableView(传入的那个)上调用该方法,而不是 self.tableView。

标签: ios xcode uitableview swift


【解决方案1】:

更新代码:

import UIKit

class SubjectBooksViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {


    @IBOutlet var navigationBarTitle: UINavigationBar!
    @IBOutlet var myTableView: UITableView! //Outlet for your table View 
    override func viewDidLoad() {
        super.viewDidLoad()
        self.myTableView.dataSource = self //If you have not done in IB
        self.myTableView.registerNib(yourCellNib, forCellReuseIdentifier: "subjectCell")

        self.navigationBarTitle.topItem?.title = "\(selectedCourse)"
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }


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

        return 3
    }

     func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 100
    }

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

    var cell : bookCell = tableView.dequeueReusableCellWithIdentifier("subjectCell", forIndexPath: indexPath)

        return cell
    }
}

在 viewDidLoad() 中:

self.myTableView.registerNib(yourCellNib, forCellReuseIdentifier: "subjectCell")

将 yourCellNib 替换为自定义单元格的已加载 nib 文件。

如果您打算在表格视图中重复使用单元格,则需要注册您的 nib 文件。重复使用单元格总是一个好主意。

【讨论】:

    【解决方案2】:

    您需要重写此函数并以这种方式获取您的自定义单元格:

    override func tableView(tableView: UITableView, 
        cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
    {
    
        var cell = tableView.dequeueReusableCellWithIdentifier(
            "subjectCell", 
            forIndexPath: indexPath) as bookCell
    

    不过,您的班级名称确实应该是BookCell,并带有大写的“B”。只是为了保持现有标准

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-29
      • 1970-01-01
      • 2020-02-05
      • 1970-01-01
      • 2011-12-29
      • 2011-12-13
      • 2014-04-12
      相关资源
      最近更新 更多