Ios 使用动态原型单元将CloudKit记录从TableViewController A传递到segue上的TableViewController B
我能够用CloudKit(Ios 使用动态原型单元将CloudKit记录从TableViewController A传递到segue上的TableViewController B,ios,swift,segue,cloudkit,Ios,Swift,Segue,Cloudkit,我能够用CloudKit(var:[CKRecord]=[])中的记录填充TableViewController A,但不确定如何将[CKRecord]的子集传递到TableViewController B,因为两个控制器都有动态原型单元,并将各自的视图单元定义为UITableViewCell。此外,只有当用户点击TableViewController A上的导航项时才应触发序列(而不是在选择单元格时) 下面是我如何实现cellForRowAt的 override func tableV
var:[CKRecord]=[]
)中的记录填充TableViewController A,但不确定如何将[CKRecord]的子集传递到TableViewController B,因为两个控制器都有动态原型单元,并将各自的视图单元定义为UITableViewCell。此外,只有当用户点击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]=[]
)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
}
“showList”
已在从导航项到TableViewController B的故事板中实现func prepare(对于segue:UIStoryboardSegue,sender:Any?
还是将此刷卡保存到云中,然后在TableViewController B中再次获取它
感谢您的时间和建议 是的,我会像你提到的那样使用
prepare for segue
。在表a
和表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
的表行点击定义故事板中的序列以显示表B