Static cell in UITableViewController not showing content in simulator - ios

I'm using UITableViewController static cells and added UILabel and text box to it. I even added constraints. But when I run the program the cells are empty.
Here are my screenshots:
I Just found out
override func viewWillAppear(animated: Bool) {
self.navigationController?.setToolbarHidden(false, animated: true)
}
override func viewWillDisappear(animated: Bool) {
self.navigationController?.setToolbarHidden(true, animated: true)
}
above code make it disapear

In Swift 2, tested the below fix for the question
'Comment-out' the below auto-generated/boiler plate code that comes along when you subclass UITableViewController (as you are using static cells, they may not be used)
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 0
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 0
}

Maybe you forget to set Custom Class for your TableViewController?
Try this:
Go to storyboard.
Select your TableViewController.
Go to Show the identity inspector (3rd icon in right panel).
Set Class to your MyCustomTableViewController class (if this class doesn't exist, create it).
Run your project.

After i updated the following 2 functions return it worked again.
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
return 15
}

If you are using static cell with custom layout as you are here, the class for the tableview should not be your custom tableviewcontroller class. Try and set your tableview and cell as the dumps below.
If you, on the other hand, wants to fill the cell content dynamically, you need to make a class for the custom cell and also handle the filling of the cell in cellForRowAtIndexPath and also use dynamic prototype cells. Apple has some good explanations here and here

It seems that you haven't defined data source and delegate
This might help you.
https://guides.codepath.com/ios/Table-View-Guide

And
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 3
}

Related

UITableview Sections Indexing Click Event

Is there any way to get click event of the table view section indexing?
I have researched lots but not got any appropriate suggestion.
does anyone know how to get click event action of indexing?
I want to get click event of this blue marked indexing in the below image.
You can use the tableView(_:sectionForSectionIndexTitle:at:) method to return the appropriate section index when a index title is clicked.
let sectionArr = ["a","a","b","c","d","d"]
func numberOfSections(in tableView: UITableView) -> Int {
return sectionArr.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sectionArr[section]
}
func sectionIndexTitles(for tableView: UITableView) -> [String]? {
return Array(Set(sectionArr))//["a","b","c","d"]
}
func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int {
if let index = sectionArr.firstIndex(where: { $0 == title }) {
return index//0 for a, 2 for b, 3 for c, 4 for d
}
return 0
}
You can use a custom view as the index view of the UITableView as shown in below reference :
Custom UITableView section index
This is in Objective-c but I think it is a great reference as that has the touchesBegan, touchesMoved, touchesEnded and touchesCancelled events and you can set the user experience with these as you would like. Also it would be very easy to add customised behaviours in this custom indexing view.
Hope this helps.

Prepare number of sections and row before UITableViewController gets called

I have a subclass of UITableViewController where I have 2 properties that will defines the number of sections and number of rows in section
It seems the the delegate methods numberOfSectionsInTableView(tableView: UITableView) -> Int get called before viewDidLoad() which is where I am initialising the 2 properties at currently. Therefore the 2 properties does not have any value when the delegate methods get called.
Where should I initialise these 2 value such that it will be ready for the delegate method.
It shouldn't be calling that delegate func before viewDidLoad.
I just tested the following code:
override func viewDidLoad() {
super.viewDidLoad()
print("viewDidLoad")
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("whatt????")
}
When I run my app, this prints:
viewDidLoad
whatt???
whatt???
whatt???
I would test that your variables are properly initialized. If they are, you should have no problem using them to define how many rows/sections you have.

TableViewController Detect Row Tap with Static Cells

I'm using a TableViewController that has a table with 2 sections of static cells. This is embedded in a view controller. I cannot get didSelectRowAtIndexPath to run when I tap the cells. I've already check all of the usual suspects from this question as well as this one. When I try with a table view inside a viewcontroller with a dynamic table, I am able to get it to work just fine. Is there an issue with using a TableViewController with static cells that would not allow for using didSelectRowAtIndexPath?
Here is what I have in the custom class for the TableViewController:
import UIKit
class OptionTableViewController: UITableViewController {
#IBOutlet var optionsTable: UITableView!
let numberOfRows = [7,2]
let cellIdentifier = "OptionCells"
override func viewDidLoad() {
super.viewDidLoad()
self.optionsTable.delegate = self
self.optionsTable.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 2
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
var rows = 0
if(section < numberOfRows.count){
rows = numberOfRows[section]
}
return rows
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
print("You selected cell #\(indexPath.row)!")
}
}
Update:
I tried replacing the tableviewcontroller and the viewcontroller it was embedded in but I am still not able to get didSelectRowAtIndexPath to run.
Update 2:
Does anyone know if this is possible in Swift 3? I found a working example using Swift 2.2 with a tableviewcontroller and static cells here. Maybe there is a bug doing this with Swift 3?
Wow, so it turns out that didSelectRowAtIndexPath is no longer correct in Swift 3. The correct usage is now didSelectRowAt. I didn't see this mentioned anywhere except this question which I stumbled upon.
This:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("You selected cell #\(indexPath.row)!")
}
Not This:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
print("You selected cell #\(indexPath.row)!")
}
It's possible you have the wrong table view hooked up. Normally, a UITableViewController has it's tableView in the view property and you don't need to set up the data source and delegate programatically.

Static table not showing

I created a table view controller and from drop down in Xcode selected content to be static cells. I placed buttons in those cells but when I run I get an empty table. I didn't enter any code in the TableViewController class as I thought this was not needed.
How can this be fixed?
This is the way it looks in Xcode and then when it runs in simulator:
You may miss some stuff:
1) Check whether your tableView has set the class named as that on in your code. You find the input field to type the name of the class in Attributes inspector of the tableView.
2) Check whether you implemented methods, that comform to UITableViewDelegate and UITableViewDataSource Protocol.
func numberOfSections(in tableView: UITableView) -> Int {
// I see you only have 1 section now
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//you should return appropriate number
return 3
}
3) Check whether your table is properly connected from Storyboard to your UIViewController =>
if your tableView is inside UIViewController, check whether you set delegate and datasource for tableView in your controller (CTRL drag from table to FileOwner - rounded yellow icon in storyboard scene frame.)
using static cells, it's not necessary to implement numberOfSections and numberOfRowsInSection. By default, at runtime you get the static cell(s) as implemented in the designer.
However, if you do implement these methods and, by error, return 0: No cell is shown at runtime.
Following up on the answer above:
1) Make sure the tableview is hooked up as both the datasource and delegate.
2) Make sure these two lines of code are modified to your table. Default is to return 0 (an empty table) so you need to modify them accordingly. You can also choose to comment them out completely (what I did).
func numberOfSections(in tableView: UITableView) -> Int {
// I see you only have 1 section now
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//you should return appropriate number
return 3
}

UITableView does not appear in simulator

I have build UItableView with static cells, when I tried to run it nothing is appear in my simulator,
here is my interface builder looks like:
and here in simulator:
You need to provide return value for number of sections and number of rows. By default it is 0
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// Return the number of sections.
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Return the number of rows in the section.
return 3
}
Make sure the tableview doesn't have a programatic datasource connected to your view controller, with the numberOfCells/numberOfSections/cellForRow methods overriding the static cell population

Resources