【问题标题】:How can I get all my data in a single table, rather than split across multiple tables?如何将所有数据放在一个表中,而不是拆分到多个表中?
【发布时间】:2016-10-05 18:02:41
【问题描述】:

现在,在我的用户输入 3 个文本字段后,我得到 3 个单元格,每个单元格都包含用户输入的值之一。

我怎样才能让这 3 个值都显示在同一个单元格中?

在我的NavnCell 文件中,我创建了多个标签,但它们的行为仍然相同。似乎我的应用程序获取数组中的第一个值并显示它,然后创建另一个单元格来显示第二个值。

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
}





}

【问题讨论】:

  • "cell.timeLabel.text = self.holes[indexPath.row].value as?String" — 但这只是一个标签 (cell.timeLabel)。

标签: ios swift uitableview firebase firebase-realtime-database


【解决方案1】:
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return holes.count
}

我认为这里你想要有holes.count / 3 之类的东西,因为你想在一个单元格中显示三个值,所以你的行数应该像holes div 3,对吧?

在你的 cellForRowAtIndexPath 中应该是这样的:

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

但老实说,从一开始就以正确的方式准备数据会简单得多。类似于 newHole 的东西,其中 newHole 由 self.hole1、self.hole2 和 self.hole3 组成。

【讨论】:

  • 感谢您的解决方案,它有效!但是我不确定我是否真的理解这背后的理论。我理解 newIndex + 1,但是为什么 indexPath.row * 3。(为什么将它乘以 3?divholes.count 乘以 3,它会在行中创建 3 列或类似的东西吗?或者它到底是做什么的?非常感谢您的帮助!
  • tableView.datasource 非常简单(或者,如果您愿意,也可以是愚蠢的)。它按原样显示您的数据。因此,我们需要在 tableview 显示之前更新您的数据。例如,您的数据有 9 个值,您希望显示 3 个单元格,每个单元格有 3 个值。 1.首先我们需要告诉tableView你的数据只有3个单元格(行)。为此,我们将 9 除以 3。
  • 2.其次,我们需要告诉 tableView 它应该在每个单元格中显示 3 个连续的值。如果您的 indexPath.row == 0(这是您的第一个单元格),我们需要显示值 #0、#1、#2。因此,我们使用 newIndex、newIndex + 1 和 newIndex + 2。之后我们必须在下一个单元格中显示下一个值序列(indexPath.row = 1),但我们的第一个值应该是 #3(因为 # 0、#1、#2 显示在第一个单元格中)。要获得下一个值的数量,我们必须将 indexPath.row (1) 乘以 3。 indexPath.row = 2 我们将得到值 #6、7、8 等等。
猜你喜欢
  • 2015-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-16
  • 1970-01-01
  • 1970-01-01
  • 2018-03-11
  • 2016-08-24
相关资源
最近更新 更多