gpt4 book ai didi

swift - 如何使用操作存储特定单元格的数据?

转载 作者:行者123 更新时间:2023-11-30 10:50:40 25 4
gpt4 key购买 nike

我有一个ViewController,里面有一个TableView。我还有一个 TableViewCell Controller 。

表格的每个单元格都包含来自 Firebase 的信息和一个按钮。

这里的目标是当我单击按钮时将单元格的信息添加到我的数据库中。

基本上,我有一个歌曲列表,每首歌曲上都有一个添加按钮,我想在单击添加时将一首歌曲添加到我的用户帐户中。

歌曲列表显示得很好,但当我单击“添加”按钮时,我不知道如何将这首歌曲的信息放入我的数据库中。

型号代码:

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()
}
})
}

}

我想将我的单元格中的歌曲的定值(value)而不是“test”放入:setValue(“test”)

最佳答案

如果每个单元格都有按钮,您可以在用户按下按钮后简单地保存模型,因为您有自定义模型(可能只是结构体),您可以为它以及 TableView 中的每个单元格创建变量 cellForRowAt 数据源方法,您可以分配它

class PopularTableViewCell: UITableViewCell {

var service: ServiceModel!

@IBAction func addButtonPressed(_ sender: UIButton) {
... // save certain service
}

}

...

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

let cell = tableView.dequeueReusableCell(withIdentifier: "PopCell", for: indexPath) as! PopularTableViewCell

let service = serviceList[indexPath.row]

cell.service = service // <---

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
}

关于swift - 如何使用操作存储特定单元格的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54634477/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com