swift - 如何使用 Action 存储来自特定单元格的数据?
问题描述
我有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!
视图控制器代码:
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()
}
})
}
}
我想将我的手机歌曲的定价值而不是“测试”放在:setValue("test")
解决方案
如果每个单元格都有按钮,您可以在用户按下按钮后简单地保存模型,因为您有自定义模型(可能只是 struct btw),您可以为它和表格视图的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
}
推荐阅读
- php - 使用 PHP/HTML 在表单验证中突出显示空文本框
- javascript - 无法滚动网格直到记录结束
- bunifu - Bunifu Flat Button: Selected Property Problem (取消选择本身)
- python-3.x - Pip install 在窗口上的 docker 容器中不起作用
- swift - 在 Swift 中将 GPS 元数据写入 EXIF
- javascript - 失去地图功能的上下文
- r - R函数ggplot和lines一起不起作用,每次发送不同的警告消息
- python - sphinx 默认模板是什么?
- crystal-reports - Crystal Reports 文本旋转
- javascript - 打字稿错误,如何纠正:元素隐含地具有“任何”类型,因为类型