一个闭包如何在UITableViewCell定义中工作?

时间:2015-11-11 00:46:51

标签: uitableview closures swift2.1

从在线教程中检索了以下代码段:

typealias TableCellConfigurationBlock = (cell: ScheduleTableViewCell, indexPath: NSIndexPath, session: Session) -> ()

' typealias'似乎是一个封闭;或者一个产生空隙的元组()?

但我不知道它在以下功能中是如何工作的:

   func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("ScheduleTableViewCell") as! ScheduleTableViewCell
        let session = sessionForIndexPath(indexPath)
        if let configureBlock = tableCellConfigurationBlock {
            configureBlock(cell: cell, indexPath: indexPath, session: session)
        }
        return cell
    }

说明?
引用?

1 个答案:

答案 0 :(得分:1)

typealias ClosureType = (i: Int, s: String, d: Double)->String

let c1: ClosureType = {
    // has three input parameters 
    // i:Int, s: String, d: Double
    // and returns String
    i, s, d in
    return s + " integer: \(i) and double: \(d)"
}
let c2: ClosureType = {
    $1 + " integer: \($0) and double: \($2)"
}
print(c1(i: 1,s: "You pass in",d: 3.14))
print(c2(i: 1,s: "You pass in",d: 3.14))
/*
You pass in integer: 1 and double: 3.14
You pass in integer: 1 and double: 3.14
*/