Frederic Frederic - 2 months ago 7
Swift Question

how can I get all my data in a single table, rather than split across multiple tables?

Right now after my user has typed into 3 textfields, I get 3 cells that each contain one of the values type by the user.

How can I make it so the 3 values will all be displayed in the same cell?

In my "NavnCell" file I have created multiple labels, but they stil act the same. Its seems like my app takes the first value in the array and displays it and then creates another cell to display the second value.

import UIKit
import Firebase

class TestTableViewController: UITableViewController {
var files: [FIRDataSnapshot]! = []
func mini() {
let ref = FIRDatabase.database().reference().child("BilletSalg")
let usersRef = ref.child("tickets").childByAutoId().observeEventType(.ChildAdded, withBlock: { (snapshot) -> Void in

self.files.append(snapshot)

print(snapshot)


})
}

let cellNavn = "cellNavn"

var holes: [FIRDataSnapshot]! = []


let CoursesRef = FIRDatabase.database().reference().child("BilletSalg")





override func viewDidLoad() {
super.viewDidLoad()

CoursesRef.observeEventType(.Value, withBlock: { snapshot in



self.holes = snapshot.childSnapshotForPath("tickets").children.allObjects as! [FIRDataSnapshot]
var newItems = [FIRDataSnapshot]()

for item in snapshot.childSnapshotForPath("tickets").children {
newItems.append(item as! FIRDataSnapshot)
}

//replace the old array
self.holes = newItems

self.tableView.reloadData()


})

navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel", style: .Plain, target: self, action: #selector(handleCancel))

tableView.registerClass(NavnCell.self, forCellReuseIdentifier: cellNavn)




}


func handleCancel() {
dismissViewControllerAnimated(true, completion: nil)
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return holes.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(cellNavn, forIndexPath: indexPath) as! NavnCell




// I want 3 labels to display the 3 values typed by the user, so in each Row you can view the 3 values
cell.timeLabel.text = self.holes[indexPath.row].value as? String



cell.detailTextLabel?.text = self.holes[indexPath.item].value as? String


// cell.textLabel?.text = self.holes[indexPath.row].value as? String
//
// cell.anotherlabel.text? = self.holes[indexPath.row].value as? String


return cell

}

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 72
}





}

Answer
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return holes.count
}

I think here you want to have something like holes.count / 3 because you want to show three values in one cell, so your number of rows should be like holes div 3, right?

in your cellForRowAtIndexPath it should be something like that:

let newIndex = indexPath.row * 3
cell.timeLabel.text = self.holes[newIndex].value as? String
cell.timeLabel2.text = self.holes[newIndex + 1].value as? String //next value
cell.timeLabel3.text = self.holes[newIndex + 2].value as? String//another next value

but honestly it'd be much simpler for you to prepare your data the right way from the beginning. something like newHoles where newHole consists of self.hole1, self.hole2 and self.hole3.