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
Related
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.
I have a tableview and tabview (3 different tab).I want to show 4 section for first tab , 3 section for second tab and . 2 section for third tab.
Just first section's header must be sticky top of the view.Because of this I have implemented headerview just first section but header scrolls and be hidden like a tableview cell .it is not stick on top of the screen.What is the problem here?.Must I implement or override a specific function of tableview?
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if section == 0{
return UITableView.automaticDimension
}else {
return 0 //sadece 1. sectionda tablar header olarak olacak diğerlerinde header olmayacak
}
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return CGFloat.leastNormalMagnitude
}
func numberOfSections(in tableView: UITableView) -> Int {
if dataReady {
totalSectionCount = getSectionCount()
return totalSectionCount
}else {
return 1 //permanent because of tabview
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
if !dataReady || ApplicationContext.instance.userAuthenticationStatus.value == .semiSecure{
return 1//for shimmer cell and semisecure view
}else {
return getNumberOfRows(sectionNumber: section)
}
}
There is not any function which restricts Specific Header To Stick and Specific To Scroll. If you defined a header for a section, it will scroll up and will be hidden when next section header comes up.
In your case, you must define first header view/cell as Section Header and manage other headers in cellForRowAt() method. Because you want them to scroll up and not stick at top.
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
}
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
}
I'm new to Swift and iOS programming and I'm having a play with making some simple apps. I am trying to build a master-detail app.
In the master view I've given the tableview two sections and I've set the content of the table view to "static cells". Initially I gave each section 3 rows and was able to successfully run the app with the following code in the mainviewcontroller file:
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 2
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
I am now wanting to have 11 rows in the first section, and 5 rows in the second section but the changes I have tried to the code prevent the app from running. I've tried:
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 16
}
and I've tried:
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 11
}
but it falls over. What am I doing wrong here?
You should use the section to decide how many rows there should be in that section. For example, you could have a variable in your view controller:
let numberOfRowsAtSection: [Int] = [11, 5]
Now in numberOfRowsForSection:
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var rows: Int = 0
if section < numberOfRowsAtSection.count {
rows = numberOfRowsAtSection[section]
}
return rows
}