I have my tableview hooked up property to my viewcontroller class however I am unable to get all of my cells to return within the sections. I want 10pix margins between each cell and was able to do this successfully in another VC, however now the method that I am using is only returning one cell (there are only 2 cells total) so I would like help in figuring out a way to display all cells within the section, code is included below:
//UITableViewDataSource
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
headerView.layer.cornerRadius = 8.0
headerView.layer.masksToBounds = true
headerView.backgroundColor = UIColor.colorWithHex("A171FF")
let headerLabel = UILabel(frame: CGRect(x: 30, y: 0, width:
tableView.bounds.size.width, height: tableView.bounds.size.height))
headerLabel.font = UIFont(name: "Gill Sans", size: 15)
headerLabel.textColor = UIColor.white
headerLabel.text = self.tableView(self.myWldTbl, titleForHeaderInSection: section)
headerLabel.sizeToFit()
headerView.addSubview(headerLabel)
return headerView
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sections[section]
}
func numberOfSections(in tableView: UITableView) -> Int {
return sections.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch (section) {
case 0 :
return userMoves.count
case 1:
return rsvps.count
default :
print("unable to set up sections")
return 0
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row % 2 != 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: MyWorldTableViewCell.self)) as! MyWorldTableViewCell
//cell appearance
cell.layer.cornerRadius = 8.0
cell.clipsToBounds = true
//cell data
let evt = userMoves[indexPath.row]
cell.rsvpCount.text = "\(rsvps.count)"
//evt img
if let evtImg = evt.event_photo_url {
cell.img.kf.setImage(with: URL(string: Constants.Server.PHOTO_URL + evtImg))
} else {
cell.img.image = UIImage(named: "user_icon")
}
cell.ttl.text = evt.event_name!
return cell } else {
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: InvisibleCell.self)) as! InvisibleCell
cell.backgroundColor = UIColor.clear
return cell
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row % 2 == 0 {
return 10.0
}
return 102.0
}
//UITableViewDelegate
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 30
}
EDITED:
The first thing that is confusing about your code is that you appear to have 2 sections showing the contents of 2 different arrays (per the logic in numberOfRowsInSection
), but you don't check the section number in cellForRowAt
.
Before anything else, you should be checking indexPath.section
in cellForRowAt
to make sure you are using the correct array. As written, your code is using the userMoves
array to populate both sections.
The cause of your missing cells is that you must account for the invisible separator cells in your numberOfRowsInSection
method. You had the right idea to multiply by 2:
switch (section) {
case 0 :
return userMoves.count * 2
//etc
}
When accessing the array, in cellForRowAt
, you need to divide by 2 to avoid the index out of bounds exception:
if indexPath.row % 2 != 0 {
//dequeue, etc.
let evt = userMoves[indexPath.row / 2]
//etc.
}