使用Swift以编程方式向动态表格视图单元格添加文本标签和按钮

6
我有一个动态表格视图和原型单元格,显示一个数组。我的问题是如何在每个单元格的左侧添加一个按钮,以显示不同的名称,然后在右侧添加一个标签,显示数组信息。谢谢! :D
所以想象一下下面是单元格:
左侧:按钮(数组信息) <------------------- > 右侧:文本标签(数组信息)
2个回答

9
你可以像这样实现
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)
    }
}

0
我已经找到了你问题的解决方案,请按照以下步骤操作。
var data = ["iPad", "iPhone", "iWatch", "iPod", "iMac"]
var buttonData = ["US","China","London","Canada","Japan"];


//UITableViewDataSource Methods

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

 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
 {
    return data.count
 }

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

    var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell

    var label = UILabel(frame: CGRectMake(280.0, 14.0, 100.0, 30.0))
    label.text = data[indexPath.row]
    label.tag = indexPath.row
    cell.contentView.addSubview(label)


    let btn = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
    btn.backgroundColor = UIColor.greenColor()
    btn.setTitle(buttonData[indexPath.row], forState: UIControlState.Normal)
    btn.frame = CGRectMake(0, 5, 80, 40)
    btn.addTarget(self, action: "buttonPressed:", forControlEvents: UIControlEvents.TouchUpInside)
    btn.tag = indexPath.row
    cell.contentView.addSubview(btn)

    return cell
 }

 //Button Action is
 func buttonPressed(sender:UIButton!)
 {
    let buttonRow = sender.tag
    println("button is Pressed")
    println("Clicked Button Row is",buttonRow)
 }

它说buttonWithType不可用,使用对象构造UIButton(type:)... 有什么建议吗? - Don Draper
那么,我该如何让标签和按钮适应不同大小的屏幕? - Don Draper
4
当用户滚动时,标签和按钮会一遍又一遍地添加。 - Farid Al Haddad
@FaridAlHaddad,你得到了任何解决这个问题的方法吗? - Anup Gupta

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接