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
}
Related
When heightForHeaderInSection and/or heightForFooterInSection is called, the section text raises above it's first row. I attached a screenshot to show the issue. How can I increase the section spacing without the section header being affected?
#IBOutlet weak var tableView: UITableView!
let fruits =
["apple","pear","orange","caramel","peach","fruit","fruits"]
let numbers = ["1", "2"]
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
func tableView(_ tableView: UITableView, numberOfRowsInSection
section: Int) -> Int {
if section == 0 {
return fruits.count
} else {
return numbers.count
}
}
func tableView(_ tableView: UITableView, titleForHeaderInSection
section: Int) -> String? {
if section == 0 {
return "Animals"
} else {
return "Numbers"
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath:
IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier:
"cell1", for: indexPath)
if indexPath.section == 0 {
cell.textLabel?.text = fruits[indexPath.row]
} else {
cell.textLabel?.text = numbers[indexPath.row]
}
return cell
}
These are the 2 methods in question:
func tableView(_ tableView: UITableView, heightForHeaderInSection
section: Int) -> CGFloat {
return 100
}
func tableView(_ tableView: UITableView, heightForFooterInSection
section: Int) -> CGFloat {
return 100
}
You are setting the height of the header to 100. See here:
func tableView(_ tableView: UITableView, heightForHeaderInSection
section: Int) -> CGFloat {
return 100 // Instead try returning 30
}
func tableView(_ tableView: UITableView, heightForFooterInSection
section: Int) -> CGFloat {
return 100 // If you are not using a footer - return .zero
}
I got the answer from another post.
Just add:
tableView.sectionHeaderTopPadding = 100 // or whatever number you want.
There's also another way.
Click on of these ->
You can choose Grouped or the rounded design 'Inset Grouped'
After you can use the method 'heightForHeaderInSection' to select the specific section/s.
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
}
}
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
}
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
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.