ios - 将对象添加到自定义 UITableViewCell

标签 ios swift uitableview

尝试使用包含标签和两个按钮的自定义单元格。 我设法为自定义单元格创建了一个子类,我也需要在单元格中显示它们。这是我的代码:

import UIKit
import Alamofire
import SwiftyJSON

class TableViewController: UITableViewController {

@IBOutlet var table: UITableView!

var fruits = ["Orange", "Apple", "Banana"]

override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.delegate = self
    self.tableView.dataSource = self
    tableView.rowHeight = 80

    self.tableView.register(tableDetailsVCTableViewCell.self, forCellReuseIdentifier: "cell")
}

@objc func downloadPressed() {
    print("Download Button Presses")
}

@objc func previewPressed() {
    print("Preview Button Pressed")
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}


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

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

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! tableDetailsVCTableViewCell
    cell.lable?.text = fruits[indexPath.row]
    cell.dlB?.addTarget(self, action: #selector(downloadPressed), for: .touchUpInside)
    cell.pvB?.addTarget(self, action: #selector(previewPressed), for: .touchUpInside)

    return cell
}
}

我运行了该应用程序,但没有任何显示。标签中没有文字,也没有任何按钮

最佳答案

正如您所说,您正在使用 Storyboard来设计自定义 TableViewCell 然后无需注册单元格,只需从 viewDidLoad 中删除这行代码:

self.tableView.register(tableDetailsVCTableViewCell.self, forCellReuseIdentifier: "cell")

viewDidLoad 看起来像这样:

override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.delegate = self
    self.tableView.dataSource = self
    tableView.rowHeight = 80
}

关于ios - 将对象添加到自定义 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45262332/

相关文章:

ios - 使用 Swift 3 在自定义 View 中使用 Google map 绘制多段线

arrays - Swift 从旧数组到新数组的赋值

ios - UIPasteboard 没有提供选项,如果有多个字符串则粘贴什么

ios - 自定义单元格按钮单击

javascript - iOS中的同步与异步队列

ios - 为什么在要求我的 TableView 滚动到顶部时我会收到 NSRangeException?

iOS 重复调用 CGContext.save/restoreGState(又名 CGContextSave/RestoreGState)导致锯齿

iphone - 我应该使用什么技术来复制 iPhone 应用程序中的撰写电子邮件 View ?

ios - 如何判断 NSLayoutConstraint 是水平的还是垂直的?

ios - 如何在 Swift 3 中将 UIImage 数组导出为电影?