编程方式添加文本标签和按钮动态的tableview细胞与斯威夫特 [英] Add Text Label and Button to dynamic tableview cell programmatically with Swift

查看:153
本文介绍了编程方式添加文本标签和按钮动态的tableview细胞与斯威夫特的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个动态的tableview并显示一个数组的原型细胞。我的问题是如何将添加其上的每个小区的小区的左侧,然后标签以右侧显示阵列信息显示不同的名称的按钮。谢谢! :D

I have a dynamic tableview and a prototype cell which displays an array. My questions is how would I add a button which displays different names on each cell to the Left side of the cell and then a label to the right side displaying the array info. Thanks! :D

所以,想象这是下面的单元格:

So imagine this is the cell below:

左侧:按钮(数组资讯)< ------------------->右侧:为textLabel(排列方式)

Left side:Button(array info) <-------------------> Right side:TextLabel(array info)

推荐答案

您可以实现这样的

let titleArray = ["a", "b", "c"]

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 50
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    //return no.of cell do you want
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cellHeight = tableView(self.tableView, heightForRowAtIndexPath: NSIndexPath(forRow: indexPath.row, inSection: indexPath.section))
    var cell = CustomCell(frame: CGRectMake(0, 0, self.view.frame.width, cellHeight), title: titleArray[indexPath.row])
    cell.cellLabel.text = //labelText
    cell.cellButton.addTarget(self, action: "buttonPressed:", forControlEvents: UIControlEvents.TouchUpInside)

    return cell
}

class CustomCell: UITableViewCell {
    var cellButton: UIButton!
    var cellLabel: UILabel!

    init(frame: CGRect, title: String) {
        super.init(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")

        cellLabel= UILabel(frame: CGRectMake(self.frame.width - 100, 10, 100.0, 40))
        cellLabel.textColor = UIColor.blackColor()
        cellLabel.font = //set font here

        cellButton = UIButton(frame: CGRectMake(5, 5, 50, 30))
        cellButton.setTitle(title, forState: UIControlState.Normal)

        addSubview(cellLabel)
        addSubview(cellButton)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }
}

这篇关于编程方式添加文本标签和按钮动态的tableview细胞与斯威夫特的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆