【发布时间】:2015-03-17 07:05:20
【问题描述】:
我有一个自定义集合视图,其中单元格将包含图像和标签 如何在swift中选择单元格时将标签的值从集合视图传递到表格视图
【问题讨论】:
我有一个自定义集合视图,其中单元格将包含图像和标签 如何在swift中选择单元格时将标签的值从集合视图传递到表格视图
【问题讨论】:
使用委托模式来解决这个问题。 举个例子:
检查示例代码。以下示例基于打开 UITableViewController 实例的 UIViewController 实例。用户可以在 TableViewController 上进行选择,并将其传递给 ViewController。
协议代码:
import UIKit
protocol SelectionTableViewControllerDelegate
{
func selectionDone(controller: SelectionTableViewController, selection: Sting)
}
UIViewController 代码:
import UIKit
class SampleViewController : UIViewController, SelectionTableViewControllerDelegate
{
func selectionDone(controller: SelectionTableViewController, selection: String)
{
//use the selection as you wish
}
}
UITableViewController 代码:
class SelectionTableViewController : UITableViewContorller
{
var delegate : SelectionTableViewControllerDelegate?
override func tableView(tableView: UITableView, didSelectRowAtIndexPathindexPath: NSIndexPath) {
let cell = self.tableView.cellForRowAtIndexPath(indexPath)
let selectedValue = cell?.textLabel?.text
self.delegate?.selectionDone(self, selection: selectedValue!)
}
}
希望这会帮助您获得一个想法。
【讨论】: