0

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 :

enter image description here

Community
  • 1
  • 1
14wml
  • 4,048
  • 11
  • 49
  • 97
  • Have you ever heard of MVC? ;-) https://developer.apple.com/library/ios/documentation/General/Conceptual/DevPedia-CocoaCore/MVC.html – vadian Jan 09 '16 at 19:05

2 Answers2

3

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.

fredpi
  • 8,414
  • 5
  • 41
  • 61
  • Let me check if I understand you correctly: you're saying that I should make detailTableView instead of a `UITableView` a `UITableViewController`? ...I'm just a tad bit confused what you mean by your second sentence and would appreciate it if you could elaborate, in the context of my code, what you mean – 14wml Jan 09 '16 at 17:23
  • 5
    I'm not sure you are aware of what you're actually trying to do here. In order to create a tableView, you usually need a ViewController that contains the table View and that contains the delegate and datasource functions. It seems like you're messing up things a bit. You should clearly differentiate between UITableView, UITableViewCell and UIView. I would recommend reading this tutorial to gain more understanding of UITableView: https://www.ralfebert.de/tutorials/ios-swift-uitableviewcontroller/ – fredpi Jan 09 '16 at 17:28
1

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.

Community
  • 1
  • 1