使用动态原型单元格将 CloudKit 条记录从 TableViewController A 传递到 TableViewControllerB
Passing CloudKit records from TableViewController A to TableViewControllerB on segue with dynamic prototype cells
我可以使用来自 CloudKit (var restaurants: [CKRecord] = []
) 的记录填充 TableViewController A 但不确定如何 将 [CKRecord] 的子集传递到 TableViewController B 因为两者控制器具有动态原型单元 并定义了各自的视图单元 as UITableViewCell
。此外,segue 只应在用户点击 TableViewController A 上的导航项时触发(而不是在选择单元格时)。
下面是我如何实现 cellForRowAt
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "dataCell", for: indexPath) as! RestuarantViewCell
// Configure the cell...
let restaurant = restaurants[indexPath.row]
cell.RestNameLabel?.text = restaurant.object(forKey: "name") as? String
cell.RestDescLabel?.text = restaurant.object(forKey: "desc") as? String
if let image = restaurant.object(forKey: "image"), let imageAsset = image as? CKAsset {
if let imageData = try?Data.init(contentsOf: imageAsset.fileURL) {
cell.RestImageView?.image = UIImage(data: imageData)
}
}
return cell
}
- 当用户执行前导滑动时,将该项目添加到列表中 (
var list: [CKRecord] = []
)
重写 func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let addAction = UIContextualAction(style: .normal, title: "Save") { (action, sourceView, completionHandler) in
//Add to list
self.list.append(self.restaurants[indexPath.row])
print(self.list)
//Delete row and reload tableView i.e. tableView.reloadData()
self.tableView.deleteRows(at: [indexPath], with: .fade)
//Call completion handler to dismiss the action button
completionHandler(true)
}
//Configuring delete button
addAction.backgroundColor = UIColor(red: 76, green: 217, blue: 100)
//Create button for user swipe
let swipeConfiguration = UISwipeActionsConfiguration(actions: [addAction])
return swipeConfiguration
}
- 此列表仅在用户点击 TableViewController A 上的导航项时可见(在选择单元格时不可见)。 Segue
"showList"
已在从导航项到 TableViewController B 的情节提要中实现。
简而言之,TableViewController B 是从 TableViewController A 保存的餐厅列表(名称、图像)
我应该使用 func prepare(for segue: UIStoryboardSegue, sender: Any?)
还是将此滑动保存到云端并在 TableViewController B 中再次获取它?
感谢您的宝贵时间和建议!
是的,我会像您提到的那样使用 prepare for segue
。在 table A
和 table B
.
上使 restaurants
数组成为 class 属性
像这样:
//Table View Controller A
class TableViewA: UIViewController{
var restaurants = [CKRecord]()
override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
if segue.identifier == "NameThisInYourStoryboard"{
let vc = segue.destinationController as! TableViewB
vc.restaurants = self.restaurants //Pass the data
}
}
}
//Table View Controller B
class TableViewB: UIViewController{
var restaurants = [CKRecord]()
}
这一切都假定您设置了控制器的委托和数据源,并在情节提要中定义了从 A
的 table 行点击显示 table B
.
我可以使用来自 CloudKit (var restaurants: [CKRecord] = []
) 的记录填充 TableViewController A 但不确定如何 将 [CKRecord] 的子集传递到 TableViewController B 因为两者控制器具有动态原型单元 并定义了各自的视图单元 as UITableViewCell
。此外,segue 只应在用户点击 TableViewController A 上的导航项时触发(而不是在选择单元格时)。
下面是我如何实现 cellForRowAt
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "dataCell", for: indexPath) as! RestuarantViewCell
// Configure the cell...
let restaurant = restaurants[indexPath.row]
cell.RestNameLabel?.text = restaurant.object(forKey: "name") as? String
cell.RestDescLabel?.text = restaurant.object(forKey: "desc") as? String
if let image = restaurant.object(forKey: "image"), let imageAsset = image as? CKAsset {
if let imageData = try?Data.init(contentsOf: imageAsset.fileURL) {
cell.RestImageView?.image = UIImage(data: imageData)
}
}
return cell
}
- 当用户执行前导滑动时,将该项目添加到列表中 (
var list: [CKRecord] = []
)
重写 func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let addAction = UIContextualAction(style: .normal, title: "Save") { (action, sourceView, completionHandler) in
//Add to list
self.list.append(self.restaurants[indexPath.row])
print(self.list)
//Delete row and reload tableView i.e. tableView.reloadData()
self.tableView.deleteRows(at: [indexPath], with: .fade)
//Call completion handler to dismiss the action button
completionHandler(true)
}
//Configuring delete button
addAction.backgroundColor = UIColor(red: 76, green: 217, blue: 100)
//Create button for user swipe
let swipeConfiguration = UISwipeActionsConfiguration(actions: [addAction])
return swipeConfiguration
}
- 此列表仅在用户点击 TableViewController A 上的导航项时可见(在选择单元格时不可见)。 Segue
"showList"
已在从导航项到 TableViewController B 的情节提要中实现。
简而言之,TableViewController B 是从 TableViewController A 保存的餐厅列表(名称、图像)
我应该使用 func prepare(for segue: UIStoryboardSegue, sender: Any?)
还是将此滑动保存到云端并在 TableViewController B 中再次获取它?
感谢您的宝贵时间和建议!
是的,我会像您提到的那样使用 prepare for segue
。在 table A
和 table B
.
restaurants
数组成为 class 属性
像这样:
//Table View Controller A
class TableViewA: UIViewController{
var restaurants = [CKRecord]()
override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
if segue.identifier == "NameThisInYourStoryboard"{
let vc = segue.destinationController as! TableViewB
vc.restaurants = self.restaurants //Pass the data
}
}
}
//Table View Controller B
class TableViewB: UIViewController{
var restaurants = [CKRecord]()
}
这一切都假定您设置了控制器的委托和数据源,并在情节提要中定义了从 A
的 table 行点击显示 table B
.