heightForRowAtIndexPath adding extra cell separator - iOS Swift - ios

I have set separator to none on these two below table views which works fine until I implement the heightForRowAtIndexPath method. Once I do that, a new separator appears, that doesn't extend all the way to the right like the default separator, and each one has a varying thickness. What's going on and how can I get rid of the new separators?
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if tableView == editsTableView {
return 44.0
} else if tableView == selectionTableViewLeft || tableView == selectionTableViewRight {
let h = selectionTableViewLeft.frame.size.height / CGFloat(leftOptions.count)
if h > 44.0 {
return h
} else {
return 44.0
}
} else {
return 44.0
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == selectionTableViewLeft {
return leftOptions.count
} else if tableView == selectionTableViewRight {
return rightOptions.count
} else {
return edits.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView == editsTableView {
let cell = tableView.dequeueReusableCell(withIdentifier: "editCell")!
if indexPath.row == 0 {
cell.textLabel?.text = "Issued by \(String(describing: edits[indexPath.row].name))"
} else {
cell.textLabel?.text = "Edited by \(String(describing: edits[indexPath.row].name))"
}
cell.detailTextLabel?.text = GlobalFunctions.shared.formattedTimestamp(ts: edits[indexPath.row].timeStamp, includeDate: true, includeTime: true)
return cell
} else {
var currentOptions: [String] = []
if tableView == selectionTableViewLeft {
currentOptions = leftOptions
} else {
currentOptions = rightOptions
}
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")! as UITableViewCell
colorCell(text: currentOptions[indexPath.row], cell: cell)
cell.textLabel?.font = UIFont.systemFont(ofSize: 20.0)
cell.textLabel?.textAlignment = .center
if currentOptions[indexPath.row] == "(Blank)" {
cell.textLabel?.attributedText = GlobalFunctions.shared.italic(string: "(Blank)", size: 20.0, color: .lightGray)
} else {
cell.textLabel?.text = currentOptions[indexPath.row]
}
return cell
}
}

Related

UITableViewRows Not Visible

I have a simple UITableViewController and none of the rows are displaying. The table view header and section headers are all there. I have never encountered this. Thoughts?
import UIKit
class DashboardTableViewController: UITableViewController {
var countyName: String?
var sections = ["Quota Over/Short", "Qualified Renewals", "Membership Dues"]
override func viewDidLoad() {
super.viewDidLoad()
if countyName != nil {
self.title = "\(countyName!) County"
} else {
self.title = "County Info"
}
let headerView = DashboardCollectionView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 200))
tableView.tableHeaderView = headerView
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return sections.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return 1
} else if section == 1 {
return 2
} else {
return 9
}
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sections[section]
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell")
if cell == nil {
cell = UITableViewCell(style: .default, reuseIdentifier: "UITableViewCell")
}
if #available(iOS 13.0, *) {
cell?.textLabel?.textColor = .label
} else {
cell?.textLabel?.textColor = .black
}
cell?.textLabel?.font = UIFont(name: "HelveticaNeue-Regular", size: 25)
cell?.textLabel?.numberOfLines = 2
if indexPath.section == 0 {
cell?.textLabel?.text = "-500"
} else if indexPath.section == 1 {
if indexPath.row == 0 {
cell?.textLabel?.text = "Previous Year: 1500"
} else {
cell?.textLabel?.text = "Current Year: 1000"
}
} else {
cell?.textLabel?.text = "Test Dues"
}
return cell!
}
}
You didn't answer the question about Prototype or Code-based cell class, and the code you've shown (in cellForRowAt) is not the right way to use table view cells.
Without seeing your custom cell setup, try it like this with a "default" cell:
class DashboardTableViewController: UITableViewController {
var countyName: String?
var sections = ["Quota Over/Short", "Qualified Renewals", "Membership Dues"]
override func viewDidLoad() {
super.viewDidLoad()
if countyName != nil {
self.title = "\(countyName!) County"
} else {
self.title = "County Info"
}
// I don't have your DashboardCollectionView class, so I'm creating
// as plain UIView with a blue background
//let headerView = DashboardCollectionView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 200))
let headerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 200))
headerView.backgroundColor = .blue
tableView.tableHeaderView = headerView
// register a default UITableViewCell
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "defaultCell")
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return sections.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return 1
} else if section == 1 {
return 2
} else {
return 9
}
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sections[section]
}
//override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
// return UITableView.automaticDimension
//}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// don't do it like this
//var cell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell")
//if cell == nil {
// cell = UITableViewCell(style: .default, reuseIdentifier: "UITableViewCell")
//}
// dequeue a registered cell
let cell = tableView.dequeueReusableCell(withIdentifier: "defaultCell", for: indexPath)
if #available(iOS 13.0, *) {
cell.textLabel?.textColor = .label
} else {
cell.textLabel?.textColor = .black
}
cell.textLabel?.font = UIFont(name: "HelveticaNeue-Regular", size: 25)
cell.textLabel?.numberOfLines = 2
if indexPath.section == 0 {
cell.textLabel?.text = "-500"
} else if indexPath.section == 1 {
if indexPath.row == 0 {
cell.textLabel?.text = "Previous Year: 1500"
} else {
cell.textLabel?.text = "Current Year: 1000"
}
} else {
cell.textLabel?.text = "Test Dues"
}
return cell
}
}
Result:
If that works (which it will), then try to use this code to figure out why your custom cell is not working.
From the image it looks like UITableView allocated space for your cells. I would recommend testing the cell by
NOT reuse cell just to see if you can get something to show up
let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
Set background color to something
cell.backgroundColor = .red
If you see the cell show up then you know at least the tableview is giving you the correct # of cells

How to expand cell when button inside cell is pressed?

I'm newer in xcode and swift and i found a guide for expand cell when the entire cell is pressed.
Now i wanna expand the cell when the button inside the cell is pressed.
I have this in my view controller :
//datasource
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView.tag == 100 {
return nameArr.count
}else{
return prova.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView.tag == 100 {
let cell = tableView.dequeueReusableCell(withIdentifier: "MainTableViewCell") as! MainTableViewCell
cell.lblName.text = nameArr[indexPath.row]
cell.expand.tag = indexPath.row
return cell
}else{
tableView.estimatedRowHeight = 60
tableView.rowHeight = UITableView.automaticDimension
let cell = tableView.dequeueReusableCell(withIdentifier: "InsideTableViewCell") as! InsideTableViewCell
cell.lblInsideName.text = prova[indexPath.row]
return cell
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
//se la cella è selezionata e deve essere tirata ancora giù ritorna 243 che sarebbe il valore della cella + l'immagine dentro da mostrare
if selectedIndex == indexPath.row && isCollapsed == true && tableView.tag == 100 {
return 375
}else {
//altrimenti se è gia collassata e ripremiamo sulla cella ritorna 50 e quindi richiude la cella
return 96
}
}
//delegate
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
if selectedIndex == indexPath.row {
if self.isCollapsed == false {
self.isCollapsed = true
}else{
self.isCollapsed = false
}
}else{
self.isCollapsed = true
}
self.selectedIndex = indexPath.row
//tableView.reloadRows(at: [indexPath], with: .automatic)
tableView.beginUpdates()
tableView.endUpdates()
}
But i don't know ho to do that.
In my viewController now i have this variable :
var selectedIndex = -1 //tell me which cell i pressed
var isCollapsed = false // tell if the cell is already collapsed
You can define a clouser in cell and call it when you press the button in your cell
in your cell define:
var buttonClicked: (() -> Void)?
in your cell button call this clouser
func buttonPressAction() {
buttonClicked?()
}
in your cellForRow method change it like this:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView.tag == 100 {
let cell = tableView.dequeueReusableCell(withIdentifier: "MainTableViewCell") as! MainTableViewCell
cell.buttonClicked = { [weak self] in
if self.isCollapsed == false {
self.isCollapsed = true
} else{
self.isCollapsed = false
}
}else{
self.isCollapsed = true
}
}
}
Modify the data source to change it
And of course the data source can be more complex it can be an object that contains whether or not to open the property
let nameArr:[Bool] = [false, false, false]
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.nameArr[indexPath.row] = !self.nameArr[indexPath.row]
tableView.reloadRows(at: [indexPath], with: .none)
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
let isshow = self.nameArr[indexPath.row]
if isshow {
return 375
} else {
return 69
}
}
You should update the table view after changing the height:
tableView.reloadData().
You can do it by sending info about that action to the view controller using delegates.
1) You should save a state of your cells. You can store it in the cell model:
class YourCellModel {
var isExpanded = false
}
2) You should create a delegate protocol:
protocol YourCellDelegate: AnyObject {
func buttonPressed()
}
3) Add properties for the cell delegate and the cell model. Also you should add a function buttonPressed:
class YourCell: UITableViewCell {
weak var delegate: YourCellDelegate?
var model: YourCellModel?
//...
#IBAction func buttonPressed() {
model?.isExpanded = true
delegate?.buttonPressed()
}
}
4) You should store cell models in the view controller: {
class YourViewController: UIViewController {
var cellModels: [YourCellModel] = []
//...
override func viewDidLoad() {
super.viewDidLoad()
cellModels = Array(repeating: YourCellModel(), count: <count of cells, maybe it is a prova.count>)
}
}
5) Setup cell models and delegates to the cells in cellForItem:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let model = cellModels[indexPath.row]
if tableView.tag == 100 {
let cell = tableView.dequeueReusableCell(withIdentifier: "MainTableViewCell") as! MainTableViewCell
cell.lblName.text = nameArr[indexPath.row]
cell.expand.tag = indexPath.row
cell.model = model
cell.delegate = self
return cell
} else {
tableView.estimatedRowHeight = 60
tableView.rowHeight = UITableView.automaticDimension
let cell = tableView.dequeueReusableCell(withIdentifier: "InsideTableViewCell") as! InsideTableViewCell
cell.lblInsideName.text = prova[indexPath.row]
cell.model = model
cell.delegate = self
return cell
}
}
6) Update heightForItem:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
let model = cellModels[indexPath.row]
if model.isExpanded {
return 375
} else {
return 96
}
}
7) Your view controller should implement YourCellDelegate:
extension YourViewController: YourCellDelegate {
func buttonPressed() {
tableView.reloadData()
}
}

Add UITextView and UIImageView under UITableViewCell programmatically Swift 4

I would like to add one picture and some texts under UITableViewCell. Here is the result that I want to achieve:
But I can only implement UITableViewCell with pure codes. Anyone know how to add a picture or some texts under or above the UITableViewCell? Any suggestions would be appreciated. Here is my current result:
Here are my codes:
import UIKit
class AboutViewController : UITableViewController {
private let about = ["About us"]
private let termsOfService = ["Terms of service"]
private let privacyPolicies = ["Privacy policies"]
private let openSourceLicense = ["Open Source Libraries"]
private let sections = ["about", "termsOfService", "privacyPolicies", "openSourceLicense"]
let myTableView: UITableView = {
let mt = UITableView()
return mt
}()
override func viewDidLoad() {
super.viewDidLoad()
// register cell name
myTableView.register(UITableViewCell.self, forCellReuseIdentifier: "MyCell")
// set DataSource
myTableView.dataSource = self
// set Delegate
myTableView.delegate = self
}
override func numberOfSections(in tableView: UITableView) -> Int {
return sections.count
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return tableView.rowHeight
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.section == 0 {
print("Value: \(about[indexPath.row])")
} else if indexPath.section == 1 {
print("Value: \(termsOfService[indexPath.row])")
} else if indexPath.section == 2 {
print("Value: \(privacyPolicies[indexPath.row])")
} else if indexPath.section == 3 {
print("Value: \(openSourceLicense[indexPath.row])")
}
}
// return the number of cells each section.
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return about.count
} else if section == 1 {
return termsOfService.count
} else if section == 2 {
return privacyPolicies.count
} else if section == 3 {
return openSourceLicense.count
} else {
return 0
}
}
// return cells
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "MyCell")
if indexPath.section == 0 {
cell.accessoryType = .disclosureIndicator
cell.textLabel?.textAlignment = .left
cell.textLabel?.text = "\(about[indexPath.row])"
} else if indexPath.section == 1 {
cell.accessoryType = .disclosureIndicator
cell.textLabel?.textAlignment = .left
cell.textLabel?.text = "\(termsOfService[indexPath.row])"
} else if indexPath.section == 2 {
cell.accessoryType = .disclosureIndicator
cell.textLabel?.textAlignment = .left
cell.textLabel?.text = "\(privacyPolicies[indexPath.row])"
} else if indexPath.section == 3 {
cell.accessoryType = .disclosureIndicator
cell.textLabel?.textAlignment = .left
cell.textLabel?.text = "\(openSourceLicense[indexPath.row])"
}
return cell
}
}
Thanks #Larme for all the help. Add the following codes will be able to add a UIImage to the header (Which means to implement a TableHeaderView):
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let logoView = UIImageView()
if section == 0 {
logoView.contentMode = .scaleAspectFit
let logo: UIImage = #imageLiteral(resourceName: "IMG_0517")
logoView.image = logo
view.addSubview(logoView)
return logoView
} else {
return nil
}
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if section == 0 {
return 200
} else {
return tableView.rowHeight
}
}
Here is the final result shows:
Final result picture
If you would like to add some texts under the UITableViewCell, you can add a footer to the final section.

How to Expand UITableview cell without collapse other cell(multiple cell expand) in swift

When we click on table view cell it will expand but if other cells are already expanded they will not collapse means we can see all cells expanded at a time..
i used below code for single cell expand but not getting how to do for multiple cell
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.section == 2 && indexPath.row == selectedRowIndex && thereIsCellTapped {
switch indexPath.row{
case 0:
return 119
case 1:
return 93
case 2:
return 117
case 3:
return 135
case 4:
return 93
case 5:
return 230
default:
return 140
}
}
return 55
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let currentCell = tableView.cellForRowAtIndexPath(indexPath) as! PatientDetailsTableViewCell
if indexPath.section == 2 {
self.tableView.cellForRowAtIndexPath(indexPath)?.backgroundColor = UIColor.whiteColor()
if self.selectedRowIndex != -1 {
self.tableView.cellForRowAtIndexPath(NSIndexPath(forRow: self.selectedRowIndex, inSection: 2))?.backgroundColor = UIColor.whiteColor()
}
if indexPath.section == 2 && selectedRowIndex != indexPath.row {
self.thereIsCellTapped = true
self.selectedRowIndex = indexPath.row
currentCell.lblRightArrow.text = "P"
print(selectedRowIndex)
}
else {
// there is no cell selected anymore
self.thereIsCellTapped = false
self.selectedRowIndex = -1
currentCell.lblRightArrow.text = "p"
}
self.tableView.beginUpdates()
self.tableView.endUpdates()
}
}
I used that code for expanding single cell but how to do for multiple cell I am not getting
please help....thanks in advance
After too much work i got the solution for expanding multiple cell so i am sharing here to help others..
for expanding cell you have to store the indexPath in array when click on cell then use that array at tableview delegate method for height.My code is below..
var selectedIndexPath : NSIndexPath?
var indexPaths : Array<NSIndexPath> = []
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
selectedIndexPath = indexPath
if !indexPaths.contains(selectedIndexPath!){
indexPaths += [selectedIndexPath!]
}
else {
let index = indexPaths.indexOf(selectedIndexPath!)
indexPaths.removeAtIndex(index!)
}
tableView.beginUpdates()
tableView.endUpdates()
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPaths.count>0 {
if indexPaths.contains(indexPath){
return 200
}
else {
return 50
}
}
return 50
}
Create a new file named as TableViewController subclass of UITableViewController and also take a UITableViewController from storyboard and assign the class TableViewController from Identity Inspector of Xcode and also name the cell identifier as Cell. Then implement the class bellow:
class TableViewController: UITableViewController {
var groupArray = [String]()
var boolArray : [String]!
var dataDic = [String : [String]]()
override func viewDidLoad() {
super.viewDidLoad()
groupArray = ["A","B","C"]
boolArray = [String](count: groupArray.count, repeatedValue: "0")
let groupA = ["CELL ONE","CELL TWO","CELL THREE","CELL FOUR","CELL FIVE"]
let groupB = ["CELL ONE","CELL TWO","CELL THREE","CELL FOUR","CELL FIVE"]
let groupC = ["CELL ONE","CELL TWO","CELL THREE","CELL FOUR","CELL FIVE"]
dataDic["A"] = groupA
dataDic["B"] = groupB
dataDic["C"] = groupC
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int{
return groupArray.count
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
let boolForSec = boolArray[section] as String
if (Int(boolForSec) != 0) {
var arr = dataDic[groupArray[section]]
return arr!.count
}else {
return 0
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
let boolForSec = boolArray[indexPath.section] as String
if (Int(boolForSec) != 0) {
var arr : [String] = dataDic[groupArray[indexPath.section]]!
cell.textLabel?.text = arr[indexPath.row] as String
}else {
}
// cell.textLabel?.text = "row \(indexPath.row)"
return cell
}
override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50
}
override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 1
}
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView(frame: CGRectMake(0, 0, tableView.frame.size.width, 40))
headerView.backgroundColor = UIColor.blueColor()
headerView.tag = section
let headerString = UILabel(frame: CGRect(x: 10, y: 10, width: tableView.frame.size.width-10, height: 30)) as UILabel
headerString.text = groupArray[section] as String
headerView .addSubview(headerString)
let headerTapped = UITapGestureRecognizer (target: self, action:"sectionHeaderTapped:")
headerView .addGestureRecognizer(headerTapped)
return headerView
}
func sectionHeaderTapped(tapped: UITapGestureRecognizer){
let section = tapped.view?.tag
let boolForSec = boolArray[section!] as String
if (Int(boolForSec) != 0) {
boolArray[section!] = "0"
}else {
boolArray[section!] = "1"
}
tableView.reloadSections(NSIndexSet(index: section!), withRowAnimation: UITableViewRowAnimation.Fade)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

Multiple Custom Cells in TableViewController

Fairly new at this. I'm trying to populate 3 custom cells into a TableViewController.
I've managed (with help) to get 2 to load with no trouble. This is the code i use for 2:
override func viewDidLoad() {
super.viewDidLoad()
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 44
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath.row % 2 == 0 {
let cell = tableView.dequeueReusableCellWithIdentifier("FirstCell") as! FirstTableViewCell
cell.artImageView.image = UIImage(named: "art")
return cell
} else {
let cell = tableView.dequeueReusableCellWithIdentifier("SecondCell") as! SecondTableViewCell
cell.kidsImageView.image = UIImage(named: "kids")
return cell
}
When I try to add a third row, this is where I run into trouble. This is the code i'm using:
override func viewDidLoad() {
super.viewDidLoad()
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 44
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath.row % 3 == 0 {
let cell = tableView.dequeueReusableCellWithIdentifier("FirstCell") as! FirstTableViewCell
cell.artImageView.image = UIImage(named: "art")
return cell
} else if {
let cell = tableView.dequeueReusableCellWithIdentifier("SecondCell") as! SecondTableViewCell
cell.kidsImageView.image = UIImage(named: "kids")
return cell
} else {
let cell = tableView.dequeueReusableCellWithIdentifier("ThirdCell") as! ThirdTableViewCell
cell.pastaImageView.image = UIImage(named: "pasta")
return cell
}
}
Any help would be great. Gracias.
Your issue starts with this line
if indexPath.row % 3 == 0 {
If you really want the cells to be laid out 1,2,3 you can just do this:
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCellWithIdentifier("FirstCell") as! FirstTableViewCell
cell.artImageView.image = UIImage(named: "art")
return cell
} else if indexPath.row == 1 {
let cell = tableView.dequeueReusableCellWithIdentifier("SecondCell") as! SecondTableViewCell
cell.kidsImageView.image = UIImage(named: "kids")
return cell
} else if indexPath.row == 2 {
let cell = tableView.dequeueReusableCellWithIdentifier("ThirdCell") as! ThirdTableViewCell
cell.pastaImageView.image = UIImage(named: "pasta")
return cell
}
using indexPath.row % 3 == 0 is doing modulus math so it is giving you the "FirstCell" only when indexPath.row/3 is equal to an integer. Then you have an else if that has no test so it is getting called for all other rows. Finally your else never gets called.

Resources