【问题标题】:CustomCell in UITableView Swift 2UITableView Swift 2 中的 CustomCell
【发布时间】:2016-06-24 03:02:44
【问题描述】:

我创建了一个 UITableView,并希望动态创建单元格来保存名称、持续时间和价格等信息。

我有以下代码:

class MyCustomTableViewCell: UITableViewCell {
    @IBOutlet weak var price: UILabel!
}

class TableViewController: UITableViewController {
    var data = [["name1","amount1","duration1"],["Name2","amount2","duration2"],["name3","amount3","duration3"]]


override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return data.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
   let cell = tableView.dequeueReusableCellWithIdentifier("MyCustomTableViewCell", forIndexPath: indexPath) as! MyCustomTableViewCell
    
    cell.textLabel?.text = "\(data[indexPath.row][0])"
    cell.detailTextLabel?.text = "\(data[indexPath.row][1])"
    cell.price.text = "\(data[indexPath.row][2])"
    return cell
}

}

我在情节提要上收到以下错误:

从 TableViewController 到 UILabel 的价格出口无效。插座不能连接到重复的内容。

我理解错误的含义,但是我认为 MyCustomTableViewCell 类意味着价格变量是动态的,因此不会尝试重复静态内容。

任何帮助将不胜感激:)

【问题讨论】:

  • 您是否注册了自定义单元 XIB 以供重复使用?
  • @DookieMan 不,我没有,我该怎么做?
  • self.tableView.registerNib(UINib(nibName: "CellName", bundle: nil), forCellReuseIdentifier: "CellIdentifier")

标签: ios swift uitableview custom-cell


【解决方案1】:

我从 Askash 那里得到了这个答案,但是我的代码似乎有点混乱,所以我想我应该发布我的答案,以防其他人遇到这个问题。

第 1 步。 确保使用以下代码创建一个 TableViewController 并确保它在情节提要上设置:

class TableViewController: UITableViewController {
var data = [["name1","amount1","duration1"],["Name2","amount2","duration2"],["name3","amount3","duration3"]]


override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return data.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
   let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! TableViewCell

    cell.textLabel?.text = "\(data[indexPath.row][0])"
    cell.detailTextLabel?.text = "\(data[indexPath.row][2])"
    cell.price.text = "£\(data[indexPath.row][2])"
    return cell
}

}

在情节提要上单击 TableViewController,单击“显示身份检查器”并将“TableViewController”粘贴到类部分:

第 2 步 创建一个新的 TableViewCell 类。 - 转到文件 > 新建 > 文件... > IOS 源 > Cocoa Touch 类 > 下一步 并创建一个带有 UITableViewCell 子类的 TableViewCell 类:

步骤 3 将 TableViewCell 分配给原型单元并设置恢复 ID。

单击情节提要中的原型单元格,单击显示“属性检查器”并将“TableViewCell”粘贴到类部分和标识符类型“单元格”中:

第 4 步在“TableViewCell”类中创建标签“Price”的出口。 (使用 Ctrl + 拖动)并将其命名为“价格”。

TableViewCell 中的代码应如下所示:

类 TableViewCell: UITableViewCell {

@IBOutlet weak var price: UILabel!


override func awakeFromNib() {
    super.awakeFromNib()
}

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

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多