Cell.xibというxibファイルにCustomCellをつくりました。このCellはcell_objectというidで紐付いています。customTableViewCellというCocoa Touch Classのファイル(下記)でxibファイルを呼び出しています。
swift
1class CustomTableViewCell: UITableViewCell { 2 3 @IBOutlet weak var weather_icon_image: UIImageView! 4 @IBOutlet weak var capitallabel: UILabel! 5 var nibName: String = "Cell" 6 7 override func awakeFromNib() { 8 super.awakeFromNib() 9 let view1:UIView? = UINib(nibName: "Cell",bundle: nil).instantiateWithOwner(self, options: nil)[0] as? UIView 10 self.addSubview(view1!) 11 } 12 13 override func setSelected(selected: Bool, animated: Bool) { 14 super.setSelected(selected, animated: animated) 15 16 } 17 18} 19
これと別のファイルでcell = tableView.dequeueReusableCellWithIdentifier("cell_object") as? CustomTableViewCell
をして、Cellがnilの時のif文にCustomTableViewCell.awakeFromNib()
と書きました。runすると、セルがnilだといって落ちてしまいます。どこを直せばいいでしょうか?ご教示おねがいします。
(追記)
swift
1func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{ 2 3let cell_object = tableView.dequeueReusableCellWithIdentifier("cell_object", forIndexPath: indexPath) as! CellForWeather 4 5if (indexPath.row % 2 == 0) { 6cell_object.backgroundColor = UIColor(hue:0.0,saturation:0.0,brightness:0.67,alpha:1.0) 7} 8// 奇数セル 9else { 10cell_object.backgroundColor = UIColor(hue:0.0,saturation:0.0,brightness:0.73,alpha:1.0) 11} 12 13// セルの向きを横向きに 14cell_object.contentView.transform = CGAffineTransformMakeRotation(CGFloat(M_PI) / 2); 15 16CellForWeather().setdata("Tokyo",iconimg: UIImage(named:"01d.png")!) 17return cell_object 18}
CustomCell
1class CellForWeather: UITableViewCell { 2 3 @IBOutlet weak var weatherIconImage: UIImageView! 4 @IBOutlet weak var capitalLabel : UILabel! 5 6 override func awakeFromNib() { 7 super.awakeFromNib() 8 9 } 10 11 func setdata(capitalstr:String,iconimg:UIImage){ 12 weatherIconImage.image = iconimg 13 capitalLabel.text = capitalstr 14 } 15} 16
このコードの2行目でCould not cast value of type 'UITableViewCell' (0x1a0fe4718) to 'weatherforecast_swift.CellForWeather' (0x100057828).
と出て落ちてしまいます。
cell.xibでのIdentifierはcell_objectで、CustomClassはCellForWeatherと確認しました。
追記2:if cell==nilの時の挙動を書かないといけないのだと思いますが、どのように書けばいいでしょうか?
ご教示お願いします
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/08/12 00:06
2016/08/12 00:30 編集
2016/08/12 03:39
2016/08/12 04:30
2016/08/12 10:14 編集
2016/08/12 15:36
2016/08/12 15:41
2016/08/12 16:05
2016/08/12 16:09
2016/08/12 16:44
2016/08/12 18:16
2016/08/12 18:26