Swift 3 UITableView sections showing after the list - swift

I'm trying to create a list of products using swift 3 UITableViewController, but the sections is showing after the list of the products. There must be something I'm missing, couldn't find anywhere
Here is a simplified code
let data = [["0,0", "0,1", "0,2"], ["1,0", "1,1", "1,2"], ["2,0", "2,1", "2,2"], ["3,0", "3,1", "3,2"]]
override func numberOfSections(in tableView: UITableView) -> Int {
return data.count//productCategoriesSections.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data[section].count//productCategories[section].count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "defaultCategoryCell", for: indexPath) as! CategoryDefaultTableViewCell
cell.titleTextView.text = data[indexPath.section][indexPath.row]
return cell
}
override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
return "Section \(section)"
}
screenshot

It is because you are using wrong callback.
Use override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? instead of override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String?.
The former will place the text as a header above the section. The latter places it at the bottom.

Related

To reduce gap between tableview section in swift

I want to reduce gap between tableview section in swift. I tried “.leastNormalMagnitude”. But still I am getting some space between the sections. I want there should be no space. Can anyone help me?
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return CGFloat.leastNormalMagnitude
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return UIView()
}
return 0
instead of
return CGFloat.leastNormalMagnitude
To hide a section header:
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
return nil
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return .zero
}

How to insert one Cell into the first section of UITableView Swift

I have created a UITableView with two sections. Now I am trying to populate one section. When I do action, my program populates both sections at the same time with the same text. What I am trying to do is to populate the first section, and then eventually move one of the cell from Section 1 to Section 2. So far I got here:
extension ListVC: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
randomCount = notes.count
return notes.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: CustomNoteCell.reuseId) as! CustomNoteCell
cell.set(with: notes[indexPath.row])
return cell
}
func numberOfSections(in tableView: UITableView) -> Int {
sections.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if section < sections.count {
return sections[section]
}
return nil
}
}
I believe it needs to happen in cellForRowAt but I have no clue how to specify section. Any help and clues appreciated. Many thanks ;)
Change the code of numberOfRowInSection to this:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 { //first section
randomCount = notes.count
return notes.count
}else{
return 0
}
}

Header view not showing at Section 0

I add custom tableView header. My section count is 2 and the header is not displaying at section 0. But showing properly at section 1.
func numberOfSections(in tableView: UITableView) -> Int {
return arrRouteDetials.count
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if section == 0 {
headerRadioBtn.tag = section
headerRadioBtn.addTarget(self, action:#selector(RdAction(_sender: )), for: .touchUpInside)
return viewTblHeader
}else{
headerRadioBtn.tag = section
headerRadioBtn.addTarget(self, action:#selector(RdAction(_sender: )), for: .touchUpInside)
return viewTblHeader
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrOfSectionRowValue[section].count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "BranchUnloadingcell", for: indexPath) as! BranchUnloadingcell
cell.lblSerialNo.text = "\(indexPath.row + 1)"
cell.val = arrOfSectionRowValue[indexPath.section][indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
self.viewWillLayoutSubviews()
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 74
}

UITableView does not conform to protocol

Type ‘TVViewController’ does not conform to protocol ‘UITableViewDataSource’
I'm fairly new to programming and the error is very vague so I don't know where to begin troubleshooting.
class WeeksViewController: UIViewController, UITableViewDataSource {
//Categories
var categories = ["One", "Two", "Three", "Four", "Five", "Six", "Seven"]
//Number of sections
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return categories.count
}
//Defining the headers
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return categories[section]
}
//Number of rows
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
//Cell as identifier & category as the class
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! CategoryRow
return cell
}
}
You copied and pasted Swift 2 code. These are the proper Swift 3+ methods
class WeeksViewController: UIViewController, UITableViewDataSource {
//Categories
var categories = ["One", "Two", "Three", "Four", "Five", "Six", "Seven"]
//Number of sections
func numberOfSections(in tableView: UITableView) -> Int
return categories.count
}
//Defining the headers
func func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return categories[section]
}
//Number of rows
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
//Cell as identifier & category as the class
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", for: indexPath) as! CategoryRow
return cell
}
}
It's always worth it to have a look at the documentation
I think the issue is that you have a misspelling in the name of the numberOfRows data source method and the cellForRow signature is also a bit off.
Instead of these:
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return categories.count
}
...
func func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//
}
You probably want these:
func numberOfSections(in tableView: UITableView) -> Int {
return categories.count
}
...
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// code
}

How to display data correctly in spacing table view cells

Only the first element from the array is displayed in my spacing cells.
I have no problems displaying data in simple cells without spacing, but when I need spacing cells and change my code, only the first item is displayed
let test = ["1","2","3"]
func numberOfSections(in tableView: UITableView) -> Int {
return test.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection
section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
let cellSpacingHeight: CGFloat = 10
return cellSpacingHeight
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
headerView.backgroundColor = UIColor.clear
return headerView
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "SellerPlacesCell", for: indexPath) as! SellerPlacesCell
cell.backgroundColor = UIColor.white.withAlphaComponent(0.5)
cell.namePlace.text = test[indexPath.row]
return cell
}
How to fix it?
if in
func tableView(_ tableView: UITableView, numberOfRowsInSection
section: Int) -> Int {
return 1
}
return 1
i replace return 1 to test.count have this:
Replace
cell.namePlace.text = test[indexPath.row]
with
cell.namePlace.text = test[indexPath.section]
the spacing is for the fact that you display sections , so you need to access the array with section not row