Ok so ive been at this problem from a day or so now, seems to be a fairly common problem amongst new iOS devs. However, ive search google, appledev, stack overflow etc.
However, ive had trouble finding the correct solution. No where has clear answers.
I need the segmented control to either change the tabledata source or show a new view entirely, either would be fine.
At the moment, i have a work around solution where i put the segmented control inside a ta开发者_C百科bleheader, this would work fine, but i dont want the segmented control to scroll with the table cells.
Im trying to have it static and unscrollable, just like the Navigation bar.
Ive tried a few option in interface builder but neither of them work, either they dont show up or i get a crash because it couldnt find the table view.
Although, i do know it has something to do with putting a view inside a view?
If anyone has a spare moment to write a quick but detailed guide, i would be very thankful.
edit
Attempt 1: in IB...
View
-> Tableview (tableview inside the parent view)
creates this error
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UITableViewController loadView] loaded the "EventsViewController" nib but didn't get a UITableView.'
First of all you will need a single controller which will contain the segmented control, table view and other elements which you want to add. Since there is a single controller for your application, any change in the segmented control can be captured in it. Once you capture the event you can decide whether to reload the tableview by updating the tableview or load a new view.
This new view can also be loaded as a modal view or as a new view to a navigation control or as a subview to the current view. (the call is left to you).
The advantage will be the segmented control and table view will be part of a UIView and scrolling one will not scroll the segmented view out of sight. Other advantage will be the new view will be loaded on top of the current view. You can have delegate methods to handle the back press event on the new view. You can also get data from the new view to a single controller.
UIView
--- Segmented control(you can wrap this into a seperate UIView
--- UITableView (it can also be wrapped into a seperate uiview )
--- Other widgets
Now you can have a single view controller controlling the above Main UIView. Hope this helps you. I have used a similar approach in one of my apps.
What you can do is have a segmented control basically change the table data source. Basically, set the action hitting either segment as [myTableView reloadData];
. Then, in your data source methods, put some if statements up. For example, under numberOfRowsInSection, say: if (mySegmentedControl.selectedSegmentIndex = 1)
//2nd button, use this array. Get what I mean?
This way you can use one view controller to with basically two (really one) table.
精彩评论