【发布时间】:2014-10-26 13:14:28
【问题描述】:
我有一个项目,其中包含一个名为 TableViewController 的 UITableViewController。因为我希望我的 UITableViewDataSource 协议声明位于我的 TableViewController 声明之外,所以我设置了以下代码(受 Objc.io Lighter View Controllers 启发):
TableViewController:
class TableViewController: UITableViewController {
let array = [["1"], ["2", "3", "4"], ["5", "6"]]
var dataSource: DataSource!
override func viewDidLoad() {
super.viewDidLoad()
dataSource = DataSource(array: array, configureCellBlock: { (cell, item) in
cell.textLabel.text = item
})
tableView.dataSource = dataSource
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
deinit {
println("Quit TVC")
}
}
数据源:
class DataSource: NSObject, UITableViewDataSource {
let array: [[String]]
typealias TableViewCellConfigureBlock = (cell: UITableViewCell, item: String) -> ()
var configureCellBlock: TableViewCellConfigureBlock
init(array: [[String]], configureCellBlock: TableViewCellConfigureBlock) {
self.array = array
self.configureCellBlock = configureCellBlock
super.init()
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return array.count
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array[section].count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
let data = array[indexPath.section][indexPath.row]
configureCellBlock(cell: cell, item: data)
return cell
}
deinit {
println("Quit DataSource")
}
}
这很好用。但是现在,我想用一个方法替换 configureCellBlock 闭包。所以我将我的TableViewController 代码更改为:
class TableViewController: UITableViewController {
let array = [["1"], ["2", "3", "4"], ["5", "6"]]
var dataSource: DataSource!
override func viewDidLoad() {
super.viewDidLoad()
dataSource = DataSource(array: array, configureCellBlock: formatCell)
tableView.dataSource = dataSource
}
func formatCell(cell: UITableViewCell, item: String) -> () {
cell.textLabel.text = item
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
deinit {
println("Quit TVC")
}
}
现在的问题很明显:如果我运行这段代码,TableViewController 和 DataSource 永远不会因为强引用循环而被释放。
我一直在尝试将我的 dataSource 声明更改为 weak var dataSource: DataSource! 或 unowned var dataSource: DataSource,但我最近的尝试都没有奏效。
如何用方法替换我的 configureCellBlock 闭包?我是否必须使用协议委托模式才能这样做?它会是什么样子?
【问题讨论】:
标签: ios methods swift closures