gpt4 book ai didi

Swift 的 deinit 没有被调用

转载 作者:IT王子 更新时间:2023-10-29 05:28:23 28 4
gpt4 key购买 nike

private let DBItemCellIdentifier = "ItemCellIdentifier"
private let DBItemSegueIdentifier = "ItemSegueIdentifier"

class DBItemsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, DBItemTableViewCellDelegate {

@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var previousButton: UIButton!
@IBOutlet weak var nextButton: UIButton!
@IBOutlet weak var categoryNameLabel: UILabel!

private var elements = [Any]()
private var currentItemIndex = 0
private var isFetching = false

private weak var currentCategory: DBCategory? {

didSet {
updateView()
}
}

var categories = [DBCategory]()
var currentCategoryIndex = 0

//MARK: - Class Methods

//MARK: - Initialization

override func viewDidLoad() {
super.viewDidLoad()

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 100.0
tableView.tableFooterView = UIView(frame: CGRectZero)

setupUserAndCartButtons()
fetchItems()
}

deinit {
print("deinit")
}

//MARK: - Actions

@IBAction func nextButtonTapped(sender: UIButton) {

currentCategoryIndex = min(currentCategoryIndex + 1, categories.count - 1)
fetchItems()
}

@IBAction func previousButtonTapped(sender: UIButton) {

currentCategoryIndex = max(currentCategoryIndex - 1, 0)
fetchItems()
}

//MARK: - Private

private func fetchItems() {

tableView.alpha = 0
currentCategory = nil

if !categories.isEmpty && !isFetching {
let category = categories[currentCategoryIndex]
currentCategory = DBCategory.findCategoryWithIdentifier(category.identifier)

if currentCategory == nil {
SVProgressHUD.show()
}

isFetching = true

DBNetworkClient.sharedClient().itemsForCategory(category, completionBlock: { error in

defer {
self.isFetching = false
SVProgressHUD.dismiss()
UIAlertController.showAlertFromError(error)
}

self.currentCategory = DBCategory.findCategoryWithIdentifier(category.identifier)
})
}
}

private func updateView() {

let category = categories[currentCategoryIndex]
title = category.menu.location.name

categoryNameLabel.text = category.name
previousButton.hidden = currentCategoryIndex == 0 ? true : false
nextButton.hidden = currentCategoryIndex == categories.count - 1 ? true : false

prepareElements()

tableView.reloadData()

UIView.animateWithDuration(0.5, animations: {
self.tableView.alpha = 1
})
}

private func prepareElements() {

elements.removeAll(keepCapacity: false)

if let items = currentCategory?.items {
for item in items {
elements.append(item)
}
}

if let sets = currentCategory?.sets {
for set in sets {
elements.append(set)
}
}

elements.sortInPlace {

let left = ($0 as? DBSet)?.position ?? ($0 as? DBItem)?.position
let right = ($1 as? DBSet)?.position ?? ($1 as? DBItem)?.position

return left < right
}
}

//MARK: - Overridden

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

let element = elements[currentItemIndex]

if segue.identifier == DBItemSegueIdentifier {
let itemViewController = segue.destinationViewController as! DBItemViewController
itemViewController.prepareWithElement(element)

}
}

//MARK: - UITableViewDataSource

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 0 //when I change to elements.count, deinit is not called
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCellWithIdentifier(DBItemCellIdentifier, forIndexPath: indexPath) as! DBItemTableViewCell
let element = elements[indexPath.row]

if let item = element as? DBItem {
cell.configureCellWithItem(item)
} else if let set = element as? DBSet {
cell.configureCellWithSet(set)
}

cell.delegate = self

return cell
}

//MARK: - UITableViewDelegate

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

currentItemIndex = indexPath.row
performSegueWithIdentifier(DBItemSegueIdentifier, sender: tableView.cellForRowAtIndexPath(indexPath))
}

//MARK: - DBItemTableViewCellDelegate

func itemTableViewCell(cell: DBItemTableViewCell, willPresentSetGroupsViewControllerForSet set: DBSet) {
presentSetOrderControllerWithOrder(DBSetOrder(set: set))
}

func itemTableViewCell(cell: DBItemTableViewCell, willPresentItemMealSizesViewControllerForItem item: DBItem) {
presentItemOrderControllerWithOrder(DBItemOrder(item: item))
}
}

为什么我的 deinit 没有被调用。一旦我能够做到这一点,我将提供 100 赏金,并奖励那个帮助我解决这个问题的人......即使在之后我也会提供赏金解决问题。

非常重要的信息:此代码调用 deinit。这是工作。因为行数是 0。但是我需要有 elements.count。当我更改为此时,不会调用 deinit

编辑:

func itemsForCategory(category: DBCategory, completionBlock: DBErrorHandler) {

let query = "locations/" + category.menu.location.identifier + "/categories/" + category.identifier

GET(query, parameters: nil, success: { operation, response in

if let error = NSError(response: response) {
completionBlock(error)
} else {
self.coreDataAssistant.parseAndSaveItemsToPersistentStore(response as? NSDictionary, completionBlock: { error in
completionBlock(error)
})
}

}) { operation, error in

let responseError = NSError(response: operation.responseObject)
completionBlock(responseError ?? error)
}
}

最佳答案

您正在指定 self 作为您的 TableView 单元格的委托(delegate):

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCellWithIdentifier(DBItemCellIdentifier, forIndexPath: indexPath) as! DBItemTableViewCell
let element = elements[indexPath.row]

if let item = element as? DBItem {
cell.configureCellWithItem(item)
} else if let set = element as? DBSet {
cell.configureCellWithSet(set)
}

// HERE
cell.delegate = self

return cell
}

单元格的委托(delegate)属性定义如下:

var delegate: DBItemTableViewCellDelegate?

这会在单元格和委托(delegate)(您的 View Controller )之间创建一个strong 引用。该单元格也由 TableView 保留。这会创建一个保留周期。

您需要将委托(delegate)属性的定义更改为weak:

weak var delegate: DBItemTableViewCellDelegate?

编辑基于comment :

您的 DBItemTableViewCellDelegate 定义需要定义为 class-only protocol

protocol DBItemTableViewCellDelegate: class { 
...
}

关于Swift 的 deinit 没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31717513/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com