mercredi 6 mai 2015

How do I instantiate a UIView / UITableView from a xib (nib) in a ViewController

I want to display a user interface that has a segmented control, and a different table view for each section of the segmented control; so, 2 table views (buddies and bunches) that can be switched between. To implement this, I have done the following

  1. Create a ViewController in Storyboard
  2. Delete the View from the ViewController
  3. Create a new UIViewController swift class with an associated xib file
  4. Put the segmented control in the the main UIView in the xib
  5. Put a inner UIView element inside of the main UIView to take the space where the table views where replace it
  6. Created two subclasses of UITableView and corresponding xib files

Some options I have thought of:

  • I can set the class of the inner UIView in Interface Builder to be that of one of the table views, but I wouldn't know how to instantiate the other one in place of the initial one. If I created overlapping inner UIViews that each was associated with a table view and hiding one of them when I switch the segmented control, that actually kind of works, but the overlapping nature of views makes layout difficult and unintuitive.
  • What I want to know how to do: Instantiate the table views in place of the single main UIView element
  • Alternative: Have one UITableView subclass that has a condition based on the state of the segmented control for what data it displays. I don't like this as much because it will mix the code together for the table views. In this case, I wouldn't even need to use xibs anymore, I could do this in the storyboard with just one table view.

** ViewController Code **

@objc(BuddiesBunchesViewController) class BuddiesBunchesViewController: UIViewController {

    @IBOutlet weak var segmentedControl: UISegmentedControl!

    @IBOutlet weak var tableView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        // Instantiate tableView here to BuddiesTableView
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    @IBAction func segmentedControlIndexChanged(sender: AnyObject) {
        switch segmentedControl.selectedSegmentIndex {
        case 0: // Buddies
            // Set tableview to the buddies table view
        case 1: // Bunches
            // Set tableview to the buddies table view
        default:
            break;
        }
    }
}

** Table View **

@IBDesignable class BuddiesTableView: UITableView, UITableViewDataSource, UITableViewDelegate {

    var view: UIView!

    var nibName: String = "BuddiesTableView"

    //init

    override init(frame: CGRect) {
        // set properties

        super.init(frame: frame)

        // Set anything that uses the view or visible bounds
        setup()
    }

    required init(coder aDecoder: NSCoder) {
        //set properties

        super.init(coder: aDecoder)

        // Setup
        setup()
    }

    func setup() {
        view = loadViewFromNib()

        view.frame = self.bounds
        view.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight

         addSubview(view)
    }

    func loadViewFromNib() -> UIView {
        let bundle = NSBundle(forClass: self.dynamicType)
        let nib = UINib(nibName: nibName, bundle: bundle)
        let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView

        return view
    }

// MARK: - Table View

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "MyTestCell")
        cell.textLabel?.text = "\(indexPath.row)"
        return cell
    }
}

Aucun commentaire:

Enregistrer un commentaire