gamerChristian
gamerChristian

Reputation: 481

switch between cells when segmentedControl has changed

i have a custom UITableViewCell which contains an segmentedControl. This segmentedControl is suppose to control the second cell. When the index in segmentedControl has changed it should switch to another custom cell. How can i do something like this? i've tried implementing a IBAction in the viewController, but then i cant connect it to the segmentedControl in the xib file. If i put that method in the segmentedViewCell then i cant change the cells subclass. How can i obtain this?

Here is a little illustration. segmentedControl and the bottom view is in different cells.

enter image description here

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

    if indexPath.row == 0 {
        let cell = tableView.dequeueReusableCellWithIdentifier("ImageViewCell", forIndexPath: indexPath) as ImageViewCell


        cell.itemImage.image = itemFile


        cell.separatorInset = UIEdgeInsetsMake(0, cell.bounds.size.width, 0, 0)
        cell.selectionStyle = UITableViewCellSelectionStyle.None
        return cell

    } else if indexPath.row == 1 {
        let cell = tableView.dequeueReusableCellWithIdentifier("UtilityViewCell", forIndexPath: indexPath) as UtilityViewCell


        cell.titleLabel.text = itemTitle
        cell.selectionStyle = UITableViewCellSelectionStyle.None
        cell.separatorInset = UIEdgeInsetsMake(0, cell.bounds.size.width, 0, 0)

        return cell

    } else if indexPath.row == 2 {
        let cell = tableView.dequeueReusableCellWithIdentifier("DescViewCell", forIndexPath: indexPath) as DescViewCell



        cell.selectionStyle = UITableViewCellSelectionStyle.None

        cell.separatorInset = UIEdgeInsetsMake(0, cell.bounds.size.width, 0, 0)

        return cell

    }




    return nil
}

Upvotes: 1

Views: 2628

Answers (3)

Benny Pounder
Benny Pounder

Reputation: 1

override func numberOfSections(in intableView: UITableView) -> Int { return 1 }

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 2
}



func segmentAction(_ sender: UISegmentedControl) {
    switch sender.selectedSegmentIndex {
    case 0:
        segment = 0
    case 1:
        segment = 1
    case 2:
        segment = 2
    default:
        break
    }
    tableView.reloadData()
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell: UITableViewCell!

    if indexPath.row == 0 {
        cell = tableView.dequeueReusableCell(withIdentifier: "SegmentCell", for: indexPath as IndexPath) as UITableViewCell
       // cell.selectionStyle = .none //if necessary

        let segmentControl = cell.viewWithTag(1) as! UISegmentedControl
        segmentControl.selectedSegmentIndex = segment
        segmentControl.addTarget(self, action: #selector(JobReportTableViewController.segmentAction(_:)), for: .valueChanged)
    } else {
        switch segment {
        case 0:
            cell = tableView.dequeueReusableCell(withIdentifier: "CellZero", for: indexPath as IndexPath) as UITableViewCell!
        case 1:
            cell = tableView.dequeueReusableCell(withIdentifier: "CellOne", for: indexPath as IndexPath) as UITableViewCell!
        case 2:
            cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath as IndexPath)as UITableViewCell!
        default:
            break
        }
    }

    return cell
}

}

Upvotes: 0

Imanou Petit
Imanou Petit

Reputation: 92589

Here is a step by step. In your project's storyboard, create a UITableViewController scene. Add UITableViewCells in it as indicated in the image below. Change your first cell's style to "custom" and add a UISegmentedControl with non-ambiguous auto layout constraints in it.

enter image description here

Before setting your code,

  • Make sure that your UITableViewController scene has its class set to "TableViewController".
  • Make sure to select Prototype Cells and Grouped TableView Style for your UITableView in Attributes Inspector.
  • Make sure that your UISegmentedControl has a view tag set to 1 (see image).
  • Make sure that the cell that contains the UISegmentedControl has its identifier set to "SegmentCell".
  • Make sure that the second cell has its identifier set to "CellZero".
  • Make sure that the third cell has its identifier set to "CellOne".
  • Make sure that the fourth cell has its identifier set to "CellTwo".

Finally, your UITableViewController class file will contain the following code:

import UIKit

class TableViewController: UITableViewController {

    var segment = 0

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

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

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

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

    func segmentAction(sender: UISegmentedControl) {
        switch sender.selectedSegmentIndex {
        case 0:
            segment = 0
        case 1:
            segment = 1
        case 2:
            segment = 2
        default:
            break
        }
        tableView.reloadData()
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell: UITableViewCell!

        if indexPath.row == 0 {
            cell = tableView.dequeueReusableCellWithIdentifier("SegmentCell", forIndexPath: indexPath) as UITableViewCell
            cell.selectionStyle = .None //if necessary

            let segmentControl = cell.viewWithTag(1) as UISegmentedControl
            segmentControl.selectedSegmentIndex = segment
            segmentControl.addTarget(self, action: "segmentAction:", forControlEvents: .ValueChanged)
        } else {
            switch segment {
            case 0:
                cell = tableView.dequeueReusableCellWithIdentifier("CellZero", forIndexPath: indexPath) as UITableViewCell
            case 1:
                cell = tableView.dequeueReusableCellWithIdentifier("CellOne", forIndexPath: indexPath) as UITableViewCell
            case 2:
                cell = tableView.dequeueReusableCellWithIdentifier("CellTwo", forIndexPath: indexPath) as UITableViewCell
            default:
                break
            }
        }

        return cell
    }

}

Upvotes: 2

Steve Rosenberg
Steve Rosenberg

Reputation: 19524

Ok, this might take an iteration until I better understand your need.

Math Flash Card History View

This from a demo project of some flash cards and I use a segmented control to decide to display all the historical games or just those of a certain type (addition, subtraction, etc.) I had to deal with the issue of where to put the segmented control. Inside the table or above the table. The problem with inside the table is in my case the view reloads after each change.

But I do use he segmented control to change both the header and the cell contents.

So I can post the code for all this, or I can create a short demo code for the segmented control within a table cell (not sure how that will play out).

Upvotes: 0

Related Questions