【问题标题】:Custom UITableViewCell in swift快速自定义 UITableViewCell
【发布时间】:2014-12-09 15:45:18
【问题描述】:

我在 Swift 中有一个自定义 tableview 单元格作为 xib 文件,这很好,我有一个链接到它的 swift 文件。

像这样:

import UIKit

class CustomTableViewCell: UITableViewCell {

    @IBOutlet weak var CellTitle: UILabel!
    @IBOutlet weak var CellLocation: UILabel!
    @IBOutlet weak var CellDate: UILabel!
    @IBOutlet weak var Type: UIImageView!
    @IBOutlet weak var TypeText: 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
    }

}

IBOutlets 只是指向 UILabels 和 UIImageViews 的链接,但它们目前不需要任何值,因为它们是在设计中添加的。

我有一个 ViewController 文件,它链接到情节提要上的视图控制器,并且上面有一个 tableview。 这是我为此使用的代码,但是我如何使用我在这个 tableview(视图控制器)上创建的 customCell。

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }

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

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


        return cell
    }

}

任何帮助都会很棒,谢谢

我已添加:

 var nib = UINib(nibName: "CustomTableViewCell", bundle: nil)
        tableView.registerNib(nib, forCellReuseIdentifier: "customCell")

注册笔尖名称并在tableview中注册单元格,我还将cellforindexpath更改为:

var cell:CustomTableViewCell = self.tableView.dequeueReusableCellWithIdentifier("CustomCell") as CustomTableViewCell

【问题讨论】:

  • 这个设置会产生什么错误?

标签: iphone uitableview swift


【解决方案1】:

cellIdentifier 不同。错别字?

tableView.registerNib(nib, forCellReuseIdentifier: "customCell")

var cell:CustomTableViewCell = self.tableView.dequeueReusableCellWithIdentifier("CustomCell") as CustomTableViewCell

【讨论】:

    【解决方案2】:

    试试这个!!

        override func viewDidLoad() {
                super.viewDidLoad()
        self.tableView.delegate = self
        self.tableView.datasourse = self 
        self.tableView.registerNib(nib, forCellReuseIdentifier: "customCell")
           }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return 0
        }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    
            let cell = tableView.dequeueReusableCellWithIdentifier("tableCell", forIndexPath: indexPath) as UITableViewCell
    
    
            return cell
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-26
      • 2015-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多