加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ios – 在UITableView中动态单元格中的Swift-Access UILabels

发布时间:2020-12-14 17:33:02 所属栏目:百科 来源:网络整理
导读:我创建了一个原型单元格并将其用作动态UITableView的模板: 如何访问单元格中的UIButtons和UILabel以设置每个单元格的内容和自定义操作? 解决方法 首先,您需要使用您的outlet声明UITableViewCell的子类,并将它们与原型连接起来 class MyCustomCell: UITable
我创建了一个原型单元格并将其用作动态UITableView的模板:

如何访问单元格中的UIButtons和UILabel以设置每个单元格的内容和自定义操作?

解决方法

首先,您需要使用您的outlet声明UITableViewCell的子类,并将它们与原型连接起来

class MyCustomCell: UITableViewCell {
    @IBOutlet weak var label1: UILabel!
    @IBOutlet weak var label2: UILabel!
    @IBOutlet weak var label3: UILabel!
}

然后你的tableView(tableView:cellForRowAtIndexPath indexPath :)方法将如下所示:

func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var cell: UITableViewCell? = tableView.dequeueReusableCellWithIdentifier("cell id")

    if (cell == nil) {
        cell = MyCustomCell()
    }

    (cell as MyCustomCell).label1.text = "Some text"
    (cell as MyCustomCell).label2.text = "Some text"
    (cell as MyCustomCell).label3.text = "Some text"

    return cell;
}

通过覆盖UITableViewDelegate的tableView(tableView:,didSelectRowAtIndexPath indexPath :)方法,为每个单元格添加自定义操作:

func tableView(tableView: UITableView,didSelectRowAtIndexPath indexPath: NSIndexPath) {

    switch(indexPath.row) {
    case 1:
        action1()
    case 2:
        action2()
        //and so on
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读