在swift中创建自定义UITableViewCell

时间:2015-03-24 01:34:15

标签: ios xcode uitableview swift

我在程序中为单元格创建了一个自定义类。当我尝试在另一个类中使用它来创建一个单元格时,我一直收到错误无法调用' init'使用类型'的参数列表(样式:UITableViewCellStyle,reuseIdentifier:StringLiteralConvertible)'。任何人都能指出我在正确的方向吗?我真的很感激任何帮助。我尝试更改类继承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
        }



    }

2 个答案:

答案 0 :(得分: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文件。重用细胞总是一个好主意。

答案 1 :(得分:1)

您需要覆盖此功能并以这种方式获取自定义单元格:

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

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

你的班级名称确实应该是BookCell,大写" B"。只是为了与现有标准保持一致

相关问题