【发布时间】: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