1

ios模拟器

您好,我已经尝试解决此问题 4 天,但仍然没有运气,因此将不胜感激。我正在尝试创建一个表格视图,工作人员可以在其中上传他们的个人资料,用户可以滚动查看他们喜欢哪些(参见模拟器照片)但是当我使用 indexPath.row 时,它会填充整个单元格,而我只想填充它出一个标签,这样我就可以用我想要的数据配置不同的标签。

这是我的表视图控制器代码:

import UIKit
import FirebaseDatabase
import FirebaseStorage

struct Worker {
    var name: String!
    var price: String!
}

class SelectATaskerTableViewController: UITableViewController {

var ref: DatabaseReference!
var myList:[String] = []

@IBOutlet var myTableView: UITableView!

var handle: DatabaseHandle!
var storageHandle: StorageHandle!
var storageRef: StorageReference!

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.rowHeight = 111

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func viewWillAppear(_ animated: Bool) {
    myTableView.delegate = self
    myTableView.dataSource = self
    ref = Database.database().reference()
    storageRef = Storage.storage().reference()
    handle = ref.child("WorkerProfile").child("Name").observe(.childAdded, with: { (snapshot) in
        if let item = snapshot.value as? String {
            self.myList.append(item)
            self.myTableView.reloadData()
        }
    })
}

@IBAction func reset(_ sender: Any) {
    Database.database().reference().child("WorkerProfile").removeValue()
}

// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return myList.count
}

var nameText: String!
var pricePerHourText: String!
var extraDetailsText: String!
var profilePicImage: UIImage!

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell:MyCellTableViewCell = self.tableView.dequeueReusableCell(withIdentifier: "cell") as! MyCellTableViewCell

   cell.firstName.text = myList[indexPath.row]
   cell.pricePerHour.text = myList[indexPath.row]

  // cell.extraDetails.text = extraDetailsText
  // cell.profilePic.image = profilePicImage
  // Configure the cell...

    return cell
}

还有我的自定义表格视图单元格代码

import UIKit

class MyCellTableViewCell: UITableViewCell {

    @IBOutlet weak var firstName: UILabel!
    @IBOutlet weak var pricePerHour: UILabel!
    @IBOutlet weak var extraDetails: UILabel!
    @IBOutlet var profilePic: UIImageView!
  }

如果您对详细信息有任何疑问,请询问:) 谢谢!

4

1 回答 1

0

我建议创建一个填充字典数组的新函数。

我没有对此进行测试,但它应该以某种方式工作。如果有人能够对此进行测试或立即看到错误,请告诉我!

关于用户 ID 可能存在一些问题。我对firebase不太熟悉。

var myList:[[String:String]] = [] // Array of dictionaries now

@IBOutlet var myTableView: UITableView!
var handle: DatabaseHandle!
var storageHandle: StorageHandle!
var storageRef: StorageReference!

override func viewDidLoad() {  
    super.viewDidLoad()
    tableView.rowHeight = 111
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

override func viewWillAppear(_ animated: Bool) {
    myTableView.delegate = self
    myTableView.dataSource = self
    ref = Database.database().reference()
    storageRef = Storage.storage().reference()
    handle = ref.child("WorkerProfile").observe(.childAdded, with: { (snapshot) in
        if let item = snapshot.value as? NSDictionary {
            let itemToAppend = ["name": snapshot["Name"] as? String ?? "",
                                "pricePerHour": snapshot["PricePerHour"] as? String ?? ""
                               ]
            self.myList.append(itemToAppend)
            self.myTableView.reloadData()
        }
    })
}



@IBAction func reset(_ sender: Any) {
    Database.database().reference().child("WorkerProfile").removeValue()
}

// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
} 

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return myList.count
}

var nameText: String!
var pricePerHourText: String!
var extraDetailsText: String!
var profilePicImage: UIImage!

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell:MyCellTableViewCell = self.tableView.dequeueReusableCell(withIdentifier: "cell") as! MyCellTableViewCell
    cell.firstName.text = myList[indexPath.row]["name"]
    cell.pricePerHour.text = myList[indexPath.row]["pricePerHour"]
    // Configure the cell...
    return cell
}
于 2018-08-20T12:28:05.080 回答