Jon
Jon

Reputation: 4732

Can't access UITableView Datasource methods using delegate of UISegmentedControl

I am using a UISegmentedControl in my viewController. With one segment selected, I want the Table's data to be loaded from the NSFetchedResultsController (already setup), and from the other segment, I want data to be loaded from an array.

I am using the segment's delegate method here

- (void)segmentedControl:(SVSegmentedControl*)segmentedControl didSelectIndex:(NSUInteger)index

The problem here is I can't seem to acces the tableView data source methods using the segment's delegate.

For example, it would be good if in the didSelectRowAtIndexPath method, I could do something like if (selectedSegment == 1), then xxxxx.

Does anyone know how I can do this?

I want to push to a different view when a row is selected depending on the segment.

Upvotes: 1

Views: 454

Answers (1)

Murdock
Murdock

Reputation: 866

Assuming you've synthesized your segmented control as a property, you can certainly access it from your tableView data source methods.

For example, if your segment is called userSegment you would just do

self.userSegment.selectedSegmentIndex

And then proceed accordingly. e.g. if(self.userSegment.selectedSegmentIndex == 0) {...} else {...}. There are a variety of methods you can do with this index, too. Check out the documentation. One easy example:

NSString *selectedTitle = [self.userSegment titleForSegmentAtIndex:self.userSegment.selectedSegmentIndex];

That's just a simple example, but the point is you can easily use the state of your segment to conditionally populate your tableView data source.

Edit: See the UISegmentedControl Class Reference for more info on selectedSegmentIndex and other properties and methods you can use.

Upvotes: 1

Related Questions