Ошибка UITableVIewCell Xcode 7/Swift 2
В этом коде я встретил следующую ошибку:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell: UITableViewCell? = tableView.dequeueReusableCellWithIdentifier("Cell") as? UITableViewCell
ОШИБКА: Вниз из "UITableViewCell"? для "UITableViewCell" только разворачивает опции; Вы хотели использовать '!'?
Любые идеи?
Ответы
Ответ 1
В Swift2.0
метод dequeueReusableCellWithIdentifier
объявляется как:
@available(iOS 6.0, *)
func dequeueReusableCellWithIdentifier(identifier: String, forIndexPath indexPath: NSIndexPath) -> UITableViewCell
Не следует отбрасывать UITableViewCell
в UITableViewCell?
. См. Код ниже.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)
// Configure the cell...
return cell
}
Надеюсь, это поможет!
Ответ 2
Начиная с Xcode 7, dequeueReusableCellWithIdentifier
всегда будет возвращать необязательный UITableViewCell
.
Вам даже не нужно указывать тип, его можно написать лаконично:
let cell = tableView.dequeueReusableCellWithIdentifier("Cell")
или если у вас есть собственный подкласс UITableViewCell
guard let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as? SomeOtherCell else { fatalError("unexpected cell dequeued from tableView") }
Ответ 3
Используйте это вместо
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell")
Ответ 4
CMessageCell=self.MessageTable.dequeueReusableCellWithIdentifier("CustomMessageCell") as! CustomMessageCell
Ответ 5
если вы хотите использовать идентификатор, вы должны использовать эти методы:
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 100
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("WHAT-EVER-YOU-WANT-TO-CALL-IT", forIndexPath: indexPath)
let label = cell.viewWithTag(1000) as! UILabel
}
return cell
Ответ 6
var cell:UITableViewCell! = tableView.dequeueReusableCellWithIdentifier(identifier) as UITableViewCell!