Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在swift中为故事板使用自定义uitableviewcell_Ios_Iphone_Uitableview_Swift - Fatal编程技术网

Ios 如何在swift中为故事板使用自定义uitableviewcell

Ios 如何在swift中为故事板使用自定义uitableviewcell,ios,iphone,uitableview,swift,Ios,Iphone,Uitableview,Swift,我正在使用故事板创建自定义单元。我已将此单元格连接到故事板中的tableview: import UIKit class NewsCell: UITableViewCell { @IBOutlet weak var titleLabel: UILabel! @IBOutlet weak var summaryLabel: UILabel! override func awakeFromNib() { super.awakeFromNib()

我正在使用故事板创建自定义单元。我已将此单元格连接到故事板中的tableview:

import UIKit

class NewsCell: UITableViewCell {

    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var summaryLabel: UILabel!



    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

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

        // Configure the view for the selected state
    }

}
我的cellforrowatindexpath和init函数如下所示:

var newsItems:[NewsItem]

required init(coder aDecoder:NSCoder){
    newsItems = [NewsItem]()

    let item1 = NewsItem()
    item1.title = "I am so awesome"
    item1.summary = "My awesome summary."

    newsItems.append(item1)

    super.init(coder: aDecoder)
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cellIdentifier = "NewsCell"
        var cell: NewsCell! = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? NewsCell

        if cell == nil {
            cell = NewsCell()
        }

        let item = newsItems[indexPath.row]

        if let titleLabel = cell.titleLabel{
            titleLabel.text = item.title
        }

        return cell
    }

当我这样做时,我的单元格在tableview中显示为空白。我做错了什么?

在处理您的阵列之前,让我们先让基本单元正常工作:

class NewsCell: UITableViewCell {

    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var summaryLabel: UILabel!

}
在Table类中:

override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.registerNib(UINib(nibName: "NewsCell", bundle: nil)!,
            forCellReuseIdentifier: "NewsCell")

    let title = "I am so awesome"
    let summary = "My awesome summary."

    }


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

        let cell = tableView.dequeueReusableCellWithIdentifier("NewsCell", forIndexPath: indexPath) as NewsCell


cell.titleLabel = title
cell.SummaryLabel = summary

return cell
}
然后我们可以帮助您处理数组if字符串


如果在情节提要中,您可能会忘记注册nib代码。但这是值得学习的。

您是否已将
标题标签
与Interface Builder中的等效标签相连接?我转到了显示序列图像板和单元格视图的拆分助手视图。我从标签拖到创建连接的单元格。空白是指它没有行?您是否在表视图中重新加载了数据?您是否将原型单元的类(在故事板中)设置为
NewsCell
?您是否实现了numberOfRowsInSection:,并从中返回了一个非零的数字?您不应该为故事板中生成的单元注册nib。对不起,我确实建议在这里使用Xib文件。