Reputation: 151
I'm trying to load data in table view from an API response, from API response I get the array and pass it to the table view delegates and data sources. I want to show each element of array under a single section. I'm passing an array of section but when i load data it shows multiple elements under single section. This is how it looks,
This my code to show data,
func numberOfSections(in tableView: UITableView) -> Int {
return headerArray.count
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 35
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let bgView = UIView()
bgView.backgroundColor = #colorLiteral(red: 0.3221348226, green: 0.6953448653, blue: 0.9635410905, alpha: 1)
bgView.layer.cornerRadius = 2.0
let titleLbl = UILabel()
titleLbl.frame = CGRect(x: 10, y: 10, width: 150, height: 20)
titleLbl.text = headerArray[section]
titleLbl.font = UIFont.systemFont(ofSize: 17, weight: .semibold)
titleLbl.textColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
bgView.addSubview(titleLbl)
return bgView
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if vcType == "SellerInProcess"
{
return propertyObject?.owners?.count ?? 0
}
else
{
return ownersList?.count ?? 0
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let owners = ownerArray[indexPath.row]
if owners.userType != nil && owners.userType == Identity.OWNER {
let cell : OwnerListTableViewCell! = tableView.dequeueReusableCell(withIdentifier: "OwnerListTableViewCell", for: indexPath) as? OwnerListTableViewCell
let ownerData = ownersList![indexPath.row]
cell.ownerData = ownerData as? OwnerModel
return cell
}
}
For section array i have made like this,
var i = 1
self?.headerArray.removeAll()
for _ in self?.ownersList ?? []
{
let index = i + 0
i += 1
self?.print(i,index)
let sectionString = "Owner \(index)"
self?.headerArray.append(sectionString)
}
How i can show each element under a single section?
Upvotes: 0
Views: 146
Reputation: 285250
Organize your data appropriately.
For example create a model like
struct Section {
let headerName : String
let owner : WhateverItsTypeIs
}
and map your data to an array of this model
var sections = [Section]()
Then return section.count
in numberOfSections
and 1
(or 0
then make owner
optional) in numberOfRowsInSection
.
Don't use multiple arrays as data source anyway.
Upvotes: 0
Reputation: 1864
If you want only one cell inside each section, you need to return either 1 or 0 (for empty section) inside the numberOfRowsInSection
method. You need to also modify the cellForRow
section and use indexPath.section
as array index to get the element for this section.
Also I would not prepare headerArray
, since you can generate the string inside the viewForHeaderInSection
because you will get the section
variable and can do something like this: let headerTitle = "Owner \(section + 1)"
+1 because indices are counted from zero.
Upvotes: 0