简体   繁体   English

自定义UITableViewCell不符合协议UITableViewDataSource吗?

[英]Custom UITableViewCell does not conform to protocol UITableViewDataSource?

I followed this post and put inside my custom UITableViewCell SummaryCell the UITableView detailTableView 我遵循了这篇文章 ,并在我的自定义UITableViewCell SummaryCell放入了UITableView detailTableView

But now I'm getting the error: 但是现在我得到了错误:

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

If anyone could tell what I'm doing wrong & how to fix this I would greatly appreciate it! 如果有人能告诉我我在做错什么以及如何解决这个问题,我将不胜感激!


Code for SummmaryCell : 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
}

}

What my summaryCell looks like : 我的summaryCell如下所示:

在此处输入图片说明

It is generally considered bad practice to make your UITableViewCell class conform to the UITableViewDataSource and UITableViewDelegate protocols. 通常,使UITableViewCell类符合UITableViewDataSourceUITableViewDelegate协议是一种不好的做法。

I would strongly recommend to set these both to a view controller containing a table view and could imagine that this probably causes your error. 我强烈建议将它们都设置为包含表视图的视图控制器,并且可以想象这可能会导致您的错误。

To follow up to return false's answer , a cell should only be responsible for its own views. 为了跟进返回false的答案 ,单元格应该只负责自己的观点。 It shouldn't have a property for its tableView. 它不应为其tableView具有属性。 It's generally a bad design for the cell to need to know or control anything about a view in its superview hierarchy. 对于单元格来说,需要了解或控制其超级视图层次结构中的视图的任何内容通常是一个糟糕的设计。

Also if you consider the unlikely possibility that the reusable cell that happened to be the delegate were to be deinitialized, the tableView would no longer have a delegate. 另外,如果您考虑到刚好是委托的可重用单元格被取消初始化的可能性很小,则tableView将不再具有委托。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ViewController不符合协议UITableViewDataSource - ViewController does not conform to protocol UITableViewDataSource ImagesTabViewController'不符合协议'UITableViewDataSource' - ImagesTabViewController' does not conform to protocol 'UITableViewDataSource' 我的UIViewController“不符合协议:'UITableViewDataSource'” - My UIViewController “does not conform to protocol: 'UITableViewDataSource'” 类型的viewcontroller不符合协议uitableviewdatasource - type viewcontroller does not conform to protocol uitableviewdatasource 错误:类型“ UserAccView”不符合协议“ UITableViewDataSource” - error : type “UserAccView” does not conform to protocol 'UITableViewDataSource' iOS:'MyViewController'不符合协议'UITableViewDataSource' - iOS : 'MyViewController' does not conform to protocol 'UITableViewDataSource' UITableView不符合协议UITableViewDataSource错误 - UITableView does not conform to protocol UITableViewDataSource error 类型TypeViewOne不符合协议UITableViewDataSource - type TypeViewOne does not conform to protocol UITableViewDataSource 类型“ ViewController”不符合协议“ UITableViewDataSource” - Type “ViewController” does not conform to protocol 'UITableViewDataSource" 类型“ViewController”不符合协议“UITableViewDataSource” - Type 'ViewController' does not conform to protocol 'UITableViewDataSource'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM