Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

thefredelement

macrumors 65816
Original poster
Apr 10, 2012
1,241
748
New York
This problem is making me a little batty and at this point I think I'm missing some other step or more likely a better way to do this, so any help would be greatly appreciated.

I have a UITableView with a viewForHeaderInSection, in each header section I am adding a UIButton, I am also defining a title (from an array) for each section.

On the button I added a target, which calls the specified method without incident, however when I try to get info back from the sent sender on the method (a UIButton), they all show as nil. I either need a button tag or it's title to filter a dictionary's keys to set a variable in a new view controller that will add values to the dictionary.

Alternatively if there's a way to set a variable in the viewForHeaderInSection method that would be consistent, that would be great, in experimenting with this I found it's very volatile based on where the header section appears on screen and I can't count on it.

Code:
import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    
    var sectionsDictionary:NSDictionary = ["Fruits" : ["Apple", "Orange", "Kiwi","Lime"], "Veggies" : ["Cucumber", "Squash", "Potato","Lettuce"]]
    
    var sectionTitlesArray = []

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        sectionTitlesArray = Array(sectionsDictionary.allKeys)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        
        return sectionTitlesArray.count

    }
    
    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        var headerFrame:CGRect = tableView.frame
        
        var title = UILabel(frame: CGRectMake(10, 10, 100, 30))
        title.text = sectionTitlesArray.objectAtIndex(section) as? String
        
        var headBttn:UIButton = UIButton.buttonWithType(UIButtonType.ContactAdd) as UIButton
        headBttn.frame = CGRectMake(80, 20, 120, 50)
        headBttn.enabled = true
        headBttn.addTarget(self, action: "addItemVC:", forControlEvents: UIControlEvents.TouchUpInside)
        
        var headerView:UIView = UIView(frame: CGRectMake(0, 0, headerFrame.size.width, headerFrame.size.height))
        headerView.addSubview(title)
        headerView.addSubview(headBttn)
        
        return headerView
        
    }
    
    func addItemVC(sender: UIButton) {
        println(sender.titleLabel?.text)
        
    }
    
    func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        
        var headerFloat = CGFloat.abs(50)
        
        return headerFloat
        
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        var sectionTitleString:String = sectionTitlesArray.objectAtIndex(section) as String
        
        var arrayForRowsInSection = sectionsDictionary.objectForKey(sectionTitleString) as [String]
        
        return arrayForRowsInSection.count
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        var cell = UITableViewCell(style: .Default, reuseIdentifier: "cell")
        
        var sectionTitleString:String = sectionTitlesArray.objectAtIndex(indexPath.section) as String
        
        var arrayForRowsInSection:NSArray = sectionsDictionary.objectForKey(sectionTitleString) as [String]
        
        cell?.textLabel?.text = arrayForRowsInSection.objectAtIndex(indexPath.row) as? String
        
        return cell!
    }

}
 
Last edited by a moderator:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.