【问题标题】:getting the value of label in a cell in collection view in swift快速获取集合视图中单元格中标签的值
【发布时间】:2015-03-17 07:05:20
【问题描述】:

我有一个自定义集合视图,其中单元格将包含图像和标签 如何在swift中选择单元格时将标签的值从集合视图传递到表格视图

【问题讨论】:

    标签: swift generics


    【解决方案1】:

    使用委托模式来解决这个问题。 举个例子:

    检查示例代码。以下示例基于打开 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!)
        }
    }
    

    希望这会帮助您获得一个想法。

    【讨论】:

    • 谢谢您的回复,但我的问题是如何在集合视图的单元格中获取标签的值而不是表格视图并将值发送到下一个视图
    • 用你的 CollectionView 试试同样的事情。我得到的是一个示例,您可以从中了解如何解决问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-25
    • 1970-01-01
    • 2017-08-10
    • 1970-01-01
    • 1970-01-01
    • 2014-10-24
    相关资源
    最近更新 更多