简体   繁体   中英

Custom UITableViewCell does not conform to protocol UITableViewDataSource?

I followed this post and put inside my custom UITableViewCell SummaryCell the 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 :

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 :

在此处输入图片说明

It is generally considered bad practice to make your UITableViewCell class conform to the UITableViewDataSource and UITableViewDelegate protocols.

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. It shouldn't have a property for its 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM