Want to create a cool static UI but : Static table views are only valid...
Add a UITableViewController to your view. It should hold a UITableView. Define this as a static table view, and make it grouped. Add two sections. One with one row, and the other with two rows. Add your Labels buttons and sliders to the rows again.
I do not know why you would want to have two UITableViews here?
illegal configuration - Static table view are only valid when embedded in UITableViewController
By default when Xcode creates a subclass of UITableViewController it adds UITableView datasource delegate methods. Since a static TableView does need a datasource these NEED to be removed.
So the solution was to remove these delegate methods:
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Static table view inside UIViewController [Xcode 5]
UPDATE: With the latest update (Xcode 5.1) it seems that it's no longer possible to put static cells inside regular UIViewController. My answer still applies for UITableViewController though.
Yes, you can have static table view content in UIViewController.
All you need to do is:
-Create the table's static cells in interface builder and design them the way you like.
-Make the UIViewController implement table view's data source and delegate:
@interface MyViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>
-Connect the table view's delegate and dataSource to the view controller in interface builder
-Implement -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
to return the number of your cells. (e.g. return 10
, yes simple as that)
-Connect your cells to your code as IBOutlets in Interface Builder. IMPORTANT: Make sure they are strong
, weak won't work. e.g. @property (strong, nonatomic) IBOutlet UITableViewCell *myFirstCell;
-Implement -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
to return the correct cell at index path. e.g:
int num = indexPath.row;
UITableViewCell *cell;
switch (num) {
case 0:
cell = self.myFirstCell;
break;
case 1:
cell = self.mySecondCell;
break;
}
return cell;
If you apply all these steps, you should have working static cells that works for tables with not many cells. Perfect for tables that you have a few (probably no more than 10-20 would be enough) content. I've ran the same issue a few days ago and I confirm that it works. More info on my answer here: Best approach to add Static-TableView-Cells to a UIViewcontroller?
How to visually create and use static cells in a UITableView embedded in a UIViewController
You are right. In storyboard, you cannot have a tableView with static cells embedded in a viewController. One way around it (I have not tried it myself, though, so I am not sure if it works) can be that you create an instance of UITableViewController in storyboard with static cells. Add an instance of UIView to your viewController, and then programmatically load the tableView of the UITableViewController into the UIView of your viewController.
Update section of static UITableView
When I implement static cells, I delete the cellForRowAtIndexPath
, numberOfSectionsInTableView
, and numberOfRowsInSection
methods from the generic UIViewController
stub that Xcode generates; these seem to be necessary only when you're dealing with dynamic cells.
Then, by control-dragging in the storyboard, I create named outlets for all UI elements in the cells that need to be set programmatically; in my case, these are usually UILabel
s. In the viewDidLoad
method of the controller, I set the appropriate initial values of these outlets, for example:
self.nameLabel.text = self.dataSource.name;
Once you've got this wired up, you can change any of the values at will.
I hope that this works for you. I'd be happy to learn from anyone who has a better (more appropriate?) way of doing this.
Why is there extra padding at the top of my UITableView with style UITableViewStyleGrouped in iOS7
I have found the cause of my original bug and created a sample project showcasing it. I believe there is an iOS7 bug.
As of iOS7, if you create a UITableView with the Grouped style, but do not have a delegate set on first layout, then you set a delegate and call reloadData, there will be a 35px space at the top that will never go away.
See this project I made showcasing the bug: https://github.com/esilverberg/TableViewDelayedDelegateBug
Specifically this file: https://github.com/esilverberg/TableViewDelayedDelegateBug/blob/master/TableViewDelayedDelegateBug/ViewController.m
If line 24 is active,
[self performSelector:@selector(updateDelegate) withObject:nil afterDelay:0.0];
there will be an extra 35 px space at the top. If line 27 is active and 24 is commented out,
self.tableView.delegate = self;
no space at the top. It's like the tableView is caching a result somewhere and not redrawing itself after the delegate is set and reloadData is called.
Related Topics
How to Set Mime Type of Application/Vnd.Apple.Pkpass in Order to Share Pass by Link or Email
Uitextfield in Uialertcontroller (Border, Backgroundcolor)
Google Places Autocomplete on iOS - Can't Load Search Results - Try Again
Swiftui Modal Presentation Works Only Once from Navigationbaritems
Swift: Retrieving Text from a Uitextfield in a Custom Uitableviewcell and Putting It in an Array
iOS Automatically Add Hyphen in Text Field
Swiftui: Set Status Bar Color for a Specific View
Dismissing Both Uinavigation Views and Modal Views at Once Programmatically
How to Get Word Wrap Information with the New iOS 7 APIs
Sbstatusbarcontroller Instance
Undefined Symbols for Architecture I386: "_Objc_Class_$_Zipexception", Referenced From: Error
Confused About Orthographic Projection of Camera in Scenekit
iOS 6.X Open Command Line on Jailbreak
Button Action in Mkannotation View Not Working
Delivery Report of Sending Remote Push Notifications Using Apns - iOS
Can't Cast Value of Type Uiviewcontroller to Patterndetailviewcontroller