【问题标题】:Add Text Label and Button to dynamic tableview cell programmatically with Swift使用 Swift 以编程方式将文本标签和按钮添加到动态表格视图单元格
【发布时间】:2016-01-08 07:05:38
【问题描述】:

我有一个动态表格视图和一个显示数组的原型单元格。我的问题是如何在单元格的左侧添加一个在每个单元格上显示不同名称的按钮,然后在右侧添加一个显示数组信息的标签。谢谢! :D

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

左侧:按钮(数组信息) 右侧:TextLabel(数组信息)

【问题讨论】:

    标签: swift button dynamic tableview cell


    【解决方案1】:

    我已经为您的问题找到了解决方案,请按照以下步骤操作

    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:)... 有什么建议吗?
    • 那么,如何让标签和按钮符合不同尺寸的屏幕?
    • 当用户滚动时,它会一次又一次地添加标签和按钮..
    • @FaridAlHaddad 你有解决办法吗?
    【解决方案2】:

    你可以这样实现

    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)
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-10
      • 2020-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-16
      • 1970-01-01
      相关资源
      最近更新 更多