Reputation: 481
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.
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
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
Reputation: 92589
Here is a step by step. In your project's storyboard, create a UITableViewController
scene. Add UITableViewCell
s 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.
Before setting your code,
UITableViewController
scene has its class set
to "TableViewController".UITableView
in Attributes Inspector.UISegmentedControl
has a view tag set to 1 (see image).UISegmentedControl
has
its identifier set to "SegmentCell".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
Reputation: 19524
Ok, this might take an iteration until I better understand your need.
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