Atm*_*tma 2 iphone uitableview ios swift
我正在使用情节提要创建一个自定义单元格。我已将此单元格连接到情节提要中的表视图:
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
}
}
Run Code Online (Sandbox Code Playgroud)
我的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
}
Run Code Online (Sandbox Code Playgroud)
执行此操作时,单元格在表视图中显示为空白。我究竟做错了什么?
在处理您的阵列之前,让我们开始基本的单元工作:
class NewsCell: UITableViewCell {
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var summaryLabel: UILabel!
}
Run Code Online (Sandbox Code Playgroud)
在您的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
}
Run Code Online (Sandbox Code Playgroud)
然后,如果有字符串,我们可以帮助您处理数组。
如果在情节提要中,您可以忘记注册笔尖代码。但是值得学习如何做。
| 归档时间: |
|
| 查看次数: |
4598 次 |
| 最近记录: |