【问题标题】:Custom UITableViewCell does not conform to protocol UITableViewDataSource?自定义 UITableViewCell 不符合协议 UITableViewDataSource?
【发布时间】:2016-04-14 06:44:22
【问题描述】:

我关注了this post 并将UITableViewCell SummaryCell 放入我的自定义UITableView detailTableView

但现在我得到了错误:

Type 'SummaryCell` does not conform to protocol `UITableViewDataSource`

如果有人能说出我做错了什么以及如何解决这个问题,我将不胜感激!


SummmaryCell 的代码:

class SummaryCell: UITableViewCell, UITableViewDelegate, UITableViewDataSource{

@IBOutlet weak var dayOfWeek: UILabel!
@IBOutlet weak var totalSpent: UILabel!
@IBOutlet weak var totalSpentView: UIView!

@IBOutlet weak var heightOfMainView: NSLayoutConstraint!

@IBOutlet weak var detailTableView: UITableView!

var data: [Expense] = [Expense]()

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code

    detailTableView.delegate = self
    detailTableView.dataSource = self

    //create data array
    let calendar = NSCalendar.currentCalendar()
    let dateComponents = NSDateComponents()
    dateComponents.day = 14
    dateComponents.month = 5
    dateComponents.year = 2015
    dateComponents.hour = 19
    dateComponents.minute = 30
    let date = calendar.dateFromComponents(dateComponents)
    data = [Expense(amountSpent: 60), Expense(amountSpent: 20, date: date!), Expense(amountSpent: 40, date: date!, function: Function.Social, category: Category.Fun, subcategory: Subcategory.Events)]

}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
    // Configure the view for the selected state
}

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

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

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

    let cell = tableView.dequeueReusableCellWithIdentifier("detailCell") as! DetailTableViewCell

    return cell
}

}

我的summaryCell 是什么样子的:

【问题讨论】:

标签: ios swift uitableview custom-cell


【解决方案1】:

要跟进return false's answer,单元格应该只对自己的视图负责。它不应该有它的 tableView 的属性。单元格需要了解或控制其父视图层次结构中的视图的任何内容通常是一种糟糕的设计。

此外,如果您考虑到恰好是委托的可重用单元格被取消初始化的可能性不大,tableView 将不再有委托。

【讨论】:

    【解决方案2】:

    使您的UITableViewCell 类符合UITableViewDataSourceUITableViewDelegate 协议通常被认为是不好的做法。

    我强烈建议将它们都设置为包含表格视图的视图控制器,并且可以想象这可能会导致您的错误。

    【讨论】:

    • 让我检查一下我是否理解正确:您是说我应该制作 detailTableView 而不是 UITableView UITableViewController? ...我只是有点困惑你的第二句话是什么意思,如果你能在我的代码的上下文中详细说明你的意思,我将不胜感激
    • 我不确定你是否知道你在这里实际上想要做什么。为了创建一个 tableView,你通常需要一个 ViewController 包含 table View 并包含委托和数据源函数。看来你有点搞砸了。您应该清楚地区分 UITableView、UITableViewCell 和 UIView。我建议阅读本教程以进一步了解 UITableView:ralfebert.de/tutorials/ios-swift-uitableviewcontroller
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多