我正在创建一个简单的应用程序,在表格视图中显示各种单元格,一个用于捕获。 出于测试目的,我添加了一些从
加载的示例单元格var captures = [Capture]()
它有效。 当我尝试将新捕获添加到捕获数组时,它们不会显示。 这是我的CapturesTableViewController.swift:
import UIKit
class CapturesTableViewController: UITableViewController {
// MARK: Properties
var capture : Capture!
var captures = [Capture]()
override func viewDidLoad() {
super.viewDidLoad()
// Load the sample data.
loadSampleCaptures()
}
func loadSampleCaptures() {
let photo1 = UIImage(named: "photo1")
let capture1 = Capture(photo: photo1, name: "Barracuda", weight: 0.8, bait: "Duo tide flyer", notes: "prima cattura")
let photo2 = UIImage(named: "photo2")
let capture2 = Capture(photo: photo2, name: "Barracuda", weight: 1.2, bait: "Nemesi 18g", notes: "")
let photo3 = UIImage(named: "photo3")
let capture3 = Capture(photo: photo3, name: "Spigola", weight: 1.5, bait: "assassino", notes: "prima spigola")
captures += [capture1!,capture2!,capture3!]
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return captures.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// Table view cells are reused and should be dequeued using a cell identifier.
let cellIdentifier = "CaptureTableViewCell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! CaptureTableViewCell
// Fetches the appropriate meal for the data source layout.
let capture = captures[indexPath.row]
cell.fishName.text = capture.fishName
cell.fishPhoto.image = capture.photo
cell.fishWeight.text = "\(capture.fishWeight) Kg"
return cell
}
@IBAction func unwindToCaptureList(sender: UIStoryboardSegue) {
if let sourceViewController = sender.sourceViewController as? NuovaCatturaViewController , capture = sourceViewController.capture{
// Add a new capture
let newIndexPath = NSIndexPath(forRow: captures.count, inSection: 0)
captures.append(capture)
tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Right)
dispatch_async(dispatch_get_main_queue()) {
self.tableView.reloadData()
}
}
}
}
我从另一个场景调用unwindToCaptureList方法并执行if语句并且捕获不是nil。 为什么不显示? 然而,我正在遵循官方教程苹果(Food Tracker App)作为指导。
答案 0 :(得分:0)
您是否已将方法连接到其他视图,并且知道它是否正在执行?添加打印(“测试”)以检查方法是否执行如下
@IBAction func unwindToCaptureList(sender: UIStoryboardSegue) {
if let sourceViewController = sender.sourceViewController as? NuovaCatturaViewController , capture = sourceViewController.capture{
// Add a new capture
print("Is this executed?: \(capture)")
print("Check dataSource before appending: \(self.captures)")
self.captures.append(capture)
print("Check dataSource after appending: \(self.captures)")
self.tableView.reloadData()
}
}