Swift中的自定义UITableViewCell寄存器类 [英] Custom UITableViewCell register class in Swift

查看:423
本文介绍了Swift中的自定义UITableViewCell寄存器类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我根据数组填充表格。该表使用自定义UITableViewCell。一切正常,桌子就满了。然后我将搜索显示控制器添加到我的UITableViewController,没有写代码来处理搜索,只需添加控制器。当您运行该应用程序时,该表仍然填充。但如果我尝试单击搜索栏,我会收到错误:

In my application, I fill the table on the basis of the array. The table uses custom UITableViewCell. Everything works fine, table is filled. Then I add Search Display Controller to my UITableViewController, no write code to handle the search, simply add the controller. When you run the application, the table is still filled.But if I try to click on the search bar, I get the error:

终止应用程序由于未捕获的异常'NSInternalInconsistencyException',原因:'无法使用标识符CitiesListCell对单元格进行出列 - 必须为标识符注册一个nib或类或在故事板中连接原型单元格'

我尝试在UITableViewController行中添加到我的viewDidLoad函数:

I try to add to my viewDidLoad function in UITableViewController line:

self.tableView.registerClass (MyCell.classForCoder(),forCellReuseIdentifier:kCellIdentifier)

启动应用程序并立即收到错误:

Launch the application and immediately get the error:

致命错误:在展开可选值时意外发现nil

在行 cell.cellMyCity.text = cellText

我做错了什么?

这是我的自定义UITableViewCell类:

This is my Custom UITableViewCell class:

import UIKit

class MyCell: UITableViewCell {

@IBOutlet var cellMyImage: UIImageView
@IBOutlet var cellMyCity: UILabel
@IBOutlet var cellMyCountry: UILabel
@IBOutlet var cellMyTemp: UILabel

init(style: UITableViewCellStyle, reuseIdentifier: String!) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
}

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

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

}
}

这是单元格的代码:

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

    var cell = tableView.dequeueReusableCellWithIdentifier(kCellIdentifier, forIndexPath: indexPath) as MyCell!

    if cell == nil {
        cell = MyCell(style: UITableViewCellStyle.Default, reuseIdentifier: kCellIdentifier)
    }

    let cellText: String? = dataWeatherSelectedCity[indexPath.row].name as  String
    println("Строка: \(cellText)")
    cell.cellMyCity.text = cellText

    let cellSubtitle: String? = dataWeatherSelectedCity[indexPath.row].country as String
    cell.cellMyCountry.text = cellSubtitle

    cell.cellMyImage.image = UIImage(named: "Blank52")

    var currentCityWeatherURL = "http://api.wunderground.com/api/\(self.myAPI.kWundergroundApiKey)/conditions/lang:RU/q/zmw:\(self.dataWeatherSelectedCity[indexPath.row].zmv).json"

    var fullObservation:NSDictionary = self.myAPI.jsonParsingWeather(currentCityWeatherURL)
    var currentObservation:NSDictionary = fullObservation.valueForKey("current_observation") as NSDictionary

    var currentWeather = self.myAPI.parsingOneCityCurrentCondition(currentObservation)       
    cell.cellMyImage.image = currentWeather.image
    cell.cellMyTemp.text = currentWeather.temp
    return cell
}

TableViewCell中的属性:


Property in TableViewCell:

没有 self.tableView.registerClass(MyCell.classForCoder(),forCellReuseIdentifier:kCellIdentifier)或 self.tableView.registerClass(MyCell.self,forCellReuseIdentifier:kCellIdentifier)

< img src =https://i.stack.imgur.com/Y91EJ.pngalt =在此处输入图像说明>

推荐答案

由于错误提示您获得单元格 UITestField nil Swift 不支持对nil对象的方法调用,这就是你崩溃的原因。你可以通过检查对象是否为nil来防止崩溃

As error suggest you are getting your cell or UITestField nil and Swift doesn't support method call on nil object that's why you are getting crash. you can prevent crash by checking the object is nil or not as bellow

if var label = cell.cellMyCity{
    label.text = cellText
}

编辑

我想我在这里遇到了什么问题...

I think I got what is the problem here...

你有拖放 UISearchBar 在你的故事板中,所以默认设置为你的控制器的所有数据源和委托 ViewController (在其中覆盖UITableView DataSource方法的类)并在 ViewController 中注册MyCell类作为自定义类。
所以你的tableView显示完美但是当你输入UISearchBar的东西时,同样的数据源方法( ViewController )被调用,你也可以注册MyCell searchResultsTableView UISearchDisplayController tableView显示搜索结果),但不知道MyCell,这就是为什么 cell .cellMyCity 在搜索时为零,表示 searchResultsTableView 期望默认 UITableViewCell 并且您没有为默认单元格设置任何内容,例如 -

You have drag-n-drop UISearchBar in your storyboard, So by defaults all data source and delegate set to your Controller say, ViewController(class in which you override UITableView DataSource methods) and in that ViewController you register MyCell class as your customize class. So your tableView displays perfect but when you type something to UISearchBar, same DataSource methods (ViewController) called and there as well, you registers MyCell for searchResultsTableView (UISearchDisplayController tableView thats display search result), but that doesn't know about MyCell and that's why cell.cellMyCity is coming as nil in case of search, that means searchResultsTableView expect the default UITableViewCell and you are not setting anything for default cell like -

cell.textLabel.text = countryList[indexPath.row]

您可以通过将DataSource方法更改为以下方式来观察整个场景

this whole scenario you can observe by changing you DataSource methods as bellow

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

    var cell = tableView.dequeueReusableCellWithIdentifier(kCellIdentifier) as MyCell!
    if cell == nil {
        //tableView.registerNib(UINib(nibName: "UICustomTableViewCell", bundle: nil), forCellReuseIdentifier: "UICustomTableViewCell")
        tableView.registerClass(MyCell.classForCoder(), forCellReuseIdentifier: kCellIdentifier)

        cell = MyCell(style: UITableViewCellStyle.Default, reuseIdentifier: kCellIdentifier)
    }
    if var label = cell.cellMyCity{
        label.text = countryList[indexPath.row]
    }
    else{
        cell.textLabel.text = countryList[indexPath.row]
    }
    return cell
}

至于关注解决方案,可能有两个可能的解决方案..

As far as Solution is concern, there could be two possible solution..

A)。不要使用 searchResultsTableView (由 UISearchDisplayController 提供)并在同一中显示您的搜索结果您在 ViewController 中拥有的tableView 。为此你可以做的是 - 当用户在 UISearchBar 中输入你的结果数据源时,听一下 UISearchBar 委托(可能在不同的数组中)通过谓词和显示结果在相同的 UITableView

A). Don't use searchResultsTableView (provided by UISearchDisplayController) and display your search results in the same tableView that you have in your ViewController. For that what you can do is - listen to the UISearchBar delegates when user type some thing in UISearchBar get your result datasource (May be in different array) by Predicates and Display results in the same UITableView.

B)。自定义UISearchDisplayController并在 searchResultsTableView中使用您的自定义单元格(即MyCell)

B). Customize UISearchDisplayController and use your custom cell (i.e MyCell) in searchResultsTableView

这篇关于Swift中的自定义UITableViewCell寄存器类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆