我正在制作一个简单的应用程序,它在表格视图中显示各种单元格,一个用于捕获。
为了进行测试,我添加了一些从中加载的示例单元格

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

 }


ios - TableView未使用新单元格进行更新-LMLPHP

我从另一个场景调用unwindToCaptureList方法,并且执行了if语句,并且捕获结果不是nil。
为什么不显示?
但是我遵循官方教程苹果(Food Tracker App)作为指南。

更新:
我尝试添加更多条目,并且在第三个条目上开始正常工作。
ios - TableView未使用新单元格进行更新-LMLPHP
这怎么可能?

最佳答案

您是否已将方法连接到其他视图,并且知道该方法是否正在执行?添加一个print(“ Test”)来检查该方法是否如下执行

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

10-07 19:13
查看更多