问题
I need to load a custom cell in a UITableView. I created a custom subclass of UITableViewCell named "CustomTableViewCell". I have added a UITabelViewCell to the tableview (using drag and drop) as shown in figure. Then in file inspector I set the class of that UITabelViewCell to be "CustomTableViewCell". Here is my code:
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
@IBOutlet var tableView : UITableView
var items = String[]()
override func viewDidLoad() {
super.viewDidLoad()
items = ["Hi","Hello","How"]
self.tableView.registerClass(CustomTableViewCell.self, forCellReuseIdentifier: "CusTomCell")
// Do any additional setup after loading the view, typically from a nib.
}
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int{
return items.count
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!{
var cell:CustomTableViewCell? = self.tableView.dequeueReusableCellWithIdentifier("CusTomCell") as? CustomTableViewCell
if !cell
{
cell = CustomTableViewCell(style: UITableViewCellStyle.Subtitle,
reuseIdentifier: "CusTomCell")
}
println("cell \(cell)")
// cell.?.labelTitle.text = items[indexPath.row]
cell!.labelTitle.text = "some text"
return cell
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
When I run my code, I get the following error: "fatal error: Can't unwrap Optional.None" as seen in the image.
回答1:
Hi Finally i found solution for my question..please check my code..it works for me..
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
@IBOutlet var tableView : UITableView
var items = String[]()
override func viewDidLoad() {
super.viewDidLoad()
items = ["Hi","Hello","How"]
// Do any additional setup after loading the view, typically from a nib.
}
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int{
return items.count
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!{
var cell:CustomTableViewCell? = self.tableView.dequeueReusableCellWithIdentifier("CusTomCell", forIndexPath: indexPath) as? CustomTableViewCell
cell!.labelTitle.text = "some text"
return cell
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
回答2:
Step 1 : Register nib in viewDidLoad() like below
var xib : UINib = UINib (nibName: "WeatherTableViewCell", bundle: nil)
self.tableView.registerNib(xib, forCellReuseIdentifier: "Cell")
Step 2 : Add the following in cellForRowIndexPath
var cell:WeatherTableViewCell? = self.tableView.dequeueReusableCellWithIdentifier("Cell") as? WeatherTableViewCell
cell!.weatherName.text = "weather"
return cell
回答3:
self.tableView.dequeueReusableCellWithIdentifier will only return a cell if there is one created earlier. You have to do a nil check and create a new cell if it's nil. In you case it will go wrong on the cell.labelTitle.text = because cell could be nil there.
来源:https://stackoverflow.com/questions/24093904/custom-uitableviewcell-shows-fatal-error-cant-unwrap-optional-none-issue-in