【问题标题】:Connecting a UITableViewCell Label in Swift在 Swift 中连接 UITableViewCell 标签
【发布时间】:2014-12-21 03:20:27
【问题描述】:

我有一个 TableView 单元,里面有一个 ImageViewLabel。但是当我使用以下方式连接到他们时:

 @IBOutlet weak var menuListLabel: UILabel!
 @IBOutlet weak var menuListImage: UIImageView!

非法配置:

从 ViewController 到 UIImageView 的 menuListImage 出口是 无效的。插座不能连接到重复的内容。

【问题讨论】:

    标签: ios uitableview


    【解决方案1】:

    您需要创建一个继承自 UITableViewCell 的自定义类,并在那里配置 outlet。

    class MyCustomTableViewCell: UITableViewCell {
       @IBOutlet weak var menuListLabel: UILabel!
       @IBOutlet weak var menuListImage: UIImageView!
    }
    

    接下来,您需要在情节提要中配置单元格。选择您的单元格。打开身份检查器并将自定义类设置为“MyCustomTableViewCell”。

    然后,在单元格仍处于选中状态的情况下,转到属性检查器,并将重用标识符设置为“MyCustomTableViewCell”。 (这个标识符可以是任何你想要的,你只需要在调用'dequeueReusableCellWithIdentifier'时使用这个确切的值。我喜欢使用我的单元格的类名作为标识符,这样很容易记住。)

    在您的表格视图控制器中,实施必要的方法来使用您的自定义单元格构建表格。

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    
        return 1   // however many sections you need
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
        return 1   // however many rows you need
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    
        // get an instance of your cell
        let cell = tableView.dequeueReusableCellWithIdentifier("MyCustomTableViewCell", forIndexPath: indexPath) as MyCustomTableViewCell
    
        // populate the data in your cell as desired
        cell.menuListLabel.text = "some text"
        cell.menuListImage.image = UIImage(named: "some image")
    
        return cell
    }
    

    【讨论】:

      猜你喜欢
      • 2021-09-18
      • 2015-04-03
      • 1970-01-01
      • 2017-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多