我在表格视图中有自定义单元格。当我运行我的代码时,我正在
低于错误
fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb)
我的代码如下:
class viewCompanyResultController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet var uiTblCompanyResultView: UITableView!
var companyName=""
var countryCode=""
var townCode=""
var element=""
var resultDict: NSDictionary=[:]
var comapniesArr:[Company] = [Company]()
override func viewDidLoad() {
super.viewDidLoad()
self.uiTblCompanyResultView.delegate=self
self.uiTblCompanyResultView.dataSource=self
self.comapniesArr.append(Company(orgName: "cbre", orgCode: "orgCode2", imageName: "3.png"))
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView:UITableView, numberOfRowsInSection section:Int) -> Int {
return self.comapniesArr.count
}
func tableView(tableView:UITableView, cellForRowAtIndexPath indexPath:NSIndexPath) -> UITableViewCell {
let cell: CompantResultCell = tableView .dequeueReusableCellWithIdentifier("cell") as CompantResultCell
let companyResult=comapniesArr[indexPath.row]
println("dgfdf \(companyResult.orgName)")
cell.setCompanyCell(uiImgConComp: companyResult.imageName,lblCompanyName: companyResult.orgName)
return cell
}
}
客户细胞类
import UIKit
class CompantResultCell: UITableViewCell {
@IBOutlet var uiImgConComp: UIImageView!
@IBOutlet var lblCompanyName: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
required init(coder aDecoder: NSCoder) {
//fatalError("init(coder:) has not been implemented")
super.init(coder: aDecoder)
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
func setCompanyCell(imageName: String,lblCompanyName: String)
{
self.uiImgConComp.image=UIImage(named: imageName)
self.lblCompanyName.text=lblCompanyName;
}
}
答案 0 :(得分:2)
自定义单元类看起来很完美!
您需要在tableView:(_cellForRowAtIndexPath:)
方法中解包自定义单元格对象,如下所示:
func tableView(tableView:UITableView, cellForRowAtIndexPath indexPath:NSIndexPath) -> UITableViewCell {
var cell: CompantResultCell! = tableView .dequeueReusableCellWithIdentifier("cell") as? CompantResultCell
if (cell == nil) {
cell = CompantResultCell(style: UITableViewCellStyle.Default, reuseIdentifier:"cell")
}
let companyResult=comapniesArr[indexPath.row]
println("dgfdf \(companyResult.orgName)")
cell.setCompanyCell(uiImgConComp: companyResult.imageName,lblCompanyName: companyResult.orgName)
return cell
}
编辑:
还在UIImage
方法中展开setCompanyCell
个对象:
func setCompanyCell(imageName: String,lblCompanyName: String) {
var cellImage = UIImage(named: imageName as String!) as UIImage!
self.uiImgConComp.image = cellImage
self.lblCompanyName.text=lblCompanyName;
}
在Swift中调用函数行为是不同的,不像在Objective C中那样。将此函数调用如下:
cell.setCompanyCell(companyResult.imageName, lblCompanyName: companyResult.orgName)