-1

I'm trying to return two seperate .counts in my UITableView. I kind of know the logic behind it but not using the syntax right. How can I do this? What I want it to do is fill my tableview up with the both .counts. I would also like to put in a label on top of the first section and second section. Anything would help! Here is what I have so far but I'm only getting the first section of cells. Why isnt it displaying both .counts?

func tableView(tableView: UITableView, numberOfSectionsInTableView: Int) -> Int{
        return 2;
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        if (section == 0){
           return featSubNames.count
        }
        else{
            return subCatNames.count
        }

    }
    func tableView(tableView: UITableView!, viewForHeaderInSection section: Int) -> UIView! {
        if (section == 0){
            ????
        }
        if (section == 1){
            ????
        }
    }

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

        var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell

        // Configure the cell
        switch (indexPath.section) {

        case 0:
            cell.textLabel?.text = featSubName[indexPath.row]
            cell.textLabel?.textColor = UIColor.whiteColor()
        case 1:
            cell.textLabel?.text = subCatNames[indexPath.row]
            cell.textLabel?.textColor = UIColor.whiteColor()
        default:
            cell.textLabel?.text = "Other"

        }



        return cell
    }
Thomas Martinez
  • 1,631
  • 3
  • 16
  • 21
  • 3
    Instead of using `viewForHeaderInSection`, use `titleForHeaderInSection`. Return one of two strings for the titles. – rmaddy Feb 13 '15 at 17:34

1 Answers1

1

Your code seems to be right. To get the UIViews on the header of each section, you can return any object that inherits from a UIView(this doesn't mean that would be nice to). So, you can return a small container with a UIImageView and a UILabel, if you want this for example.

Your code for the viewHeader would be something like this:

func tableView(tableView: UITableView!, viewForHeaderInSection section: Int) -> UIView! {
    // instantiate the view to be returned and placed correctly
    let commonLabel = UILabel(frame: CGRectMake(0, 0, tableWidth, 30))
    commonLabel.textColor = .blackColor()
    commonLabel.textAlignment = .Center
    // ... other settings in the properties

    if section == 0 {
        commonLabel.text = "section1"
        // other settings for the first section
    }
    if section == 1 {
        commonLabel.text = "section2"
        // other settings ...
    }
    return commonLabel
}

Notes:

Be sure to set the container view's frame,for sectionHeaders the top view in the hierarchy don't accept auto layout.

Implementing the viewForHeaderInSection method is more accessible to custom UI's, you can use the TitleForHeaderInSection if you wish, but it's limited for more complex stuffs.

EDIT:

If you're using storyboards, you still can use this code, although it's not so elegant in terms of apps that use IB for the UI. For this, you might take a look at this link: How to Implement Custom Table View Section Headers and Footers with Storyboard . This implements only the view using storyboards, but the delegate part must to be wrote.

Community
  • 1
  • 1
Tiago Maia
  • 616
  • 1
  • 6
  • 20