【发布时间】:2014-07-04 07:53:14
【问题描述】:
我正在尝试使用 Swift 创建带有自定义单元格的表格视图。相同的代码适用于单页应用程序,但在基于选项卡的应用程序中,它会在表格视图内容上添加一个灰色层。
这是我的自定义单元类:
class CardCell:UITableViewCell {
@IBOutlet var backImg: UIImageView!
@IBOutlet var testButton: UIButton!
func loadItem(var title:String) {
backImg.image = UIImage(named: title)
}
}
这是视图控制器:
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var items = ["swift 1.jpeg", "swift 2.jpeg"]
@IBOutlet var tableView:UITableView!
override func viewDidLoad() {
super.viewDidLoad()
var nib = UINib(nibName: "CardCell", bundle: nil)
tableView.registerNib(nib, forCellReuseIdentifier: "CardCell")
}
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
return items.count
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
var cell:CardCell = tableView.dequeueReusableCellWithIdentifier("CardCell") as CardCell
var (title) = items[indexPath.row]
cell.loadItem(title)
return cell
}
}
【问题讨论】:
-
试试这个:
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "CardCell") -
我遇到了类似的问题,只需调整情节提要中原型单元格的大小即可解决。这似乎是一个错误。不确定这是否是您遇到的问题,但值得一试。
标签: ios uitableview swift uitabbar