我有一个ViewController
,里面有TableView
。
我也有一个TableViewCell
控制器。
我表的每个单元格都有Firebase的信息和一个按钮。
这里的目的是在我单击按钮时将我的单元格信息添加到数据库中。
基本上,我有一个歌曲列表,每首都带有add button
,并且我想在单击add
时将歌曲添加到我的用户帐户中。
歌曲列表显示得很好,但是当我单击“添加”按钮时,我不知道如何将这首歌曲的信息放入数据库。
型号代码:
import Foundation
class ServiceModel {
var name: String?
var category: String?
var pricing: String?
init(name: String?, category: String?, pricing: String?){
self.name = name
self.category = category
self.pricing = pricing
}
}
TableViewCell代码:
class PopularTableViewCell: UITableViewCell {
@IBOutlet weak var imageService: UIImageView!
@IBOutlet weak var labelName: UILabel!
@IBOutlet weak var labelCategory: UILabel!
@IBOutlet weak var labelPricing: UILabel!
ViewController代码:
import UIKit
import FirebaseDatabase
import FirebaseAuth
class AddSubViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
var refServices:DatabaseReference!
@IBOutlet weak var ListPop: UITableView!
var serviceList = [ServiceModel]()
var databaseHandle:DatabaseHandle?
let userID = Auth.auth().currentUser?.uid
@IBAction func addSub(_ sender: Any) {
let ref = Database.database().reference()
let usersReference = ref.child("users")
let uid = Auth.auth().currentUser?.uid
let thisUserReference = usersReference.child(uid!).child("subs").childByAutoId()
thisUserReference.setValue("test")
**// I want to put the pricing value of the song of my cell instead of "test" in: setValue("test")**
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return serviceList.count
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "PopCell", for: indexPath) as! PopularTableViewCell
let service: ServiceModel
service = serviceList[indexPath.row]
cell.imageService?.image = UIImage(named: service.name! + ".png")
cell.labelName?.text = service.name
cell.labelCategory?.text = service.category
cell.labelPricing?.text = service.pricing
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
ListPop.delegate = self
ListPop.dataSource = self
refServices = Database.database().reference().child("Categories")
refServices.observe(DataEventType.value, with: { (snapshot) in
if snapshot.childrenCount > 0 {
self.serviceList.removeAll()
for services in snapshot.children.allObjects as! [DataSnapshot] {
let serviceObject = services.value as? [String: AnyObject]
let serviceName = serviceObject?["Name"]
let serviceCategory = serviceObject?["Category"]
let servicePricing = serviceObject?["Pricing"] as! String + " €"
let service = ServiceModel(name: serviceName as! String?, category: serviceCategory as! String?, pricing: servicePricing as String?)
self.serviceList.append(service)
}
self.ListPop.reloadData()
}
})
}
}
我想将单元格中歌曲的定价值而不是“ test”放入:setValue("test")
答案 0 :(得分:0)
如果每个单元格都有按钮,则可以在用户按下按钮后简单地保存模型,因为您具有自定义模型(可以是struct btw),因此可以为其创建变量,也可以为表格视图的{{ 1}}数据源方法,您可以为其分配
cellForRowAt
...
class PopularTableViewCell: UITableViewCell {
var service: ServiceModel!
@IBAction func addButtonPressed(_ sender: UIButton) {
... // save certain service
}
}