代码之家  ›  专栏  ›  技术社区  ›  Andres Paladines

Swift-tableviewcell中按钮的动态数量

  •  0
  • Andres Paladines  · 技术社区  · 7 年前

    实际上我有一个项目 github . 问题是,我不明白为什么滚动时,单元格中的按钮约束会变得疯狂。。 我没有看到任何这样的项目,所以我有理由分享它,但我想给其他人一个很好的例子。 我将非常感谢任何能促使我解决这个问题的帮助。 顺致敬意, 这是单元格的代码:

    import UIKit
    
    class BookTableViewCell: UITableViewCell {
    
    let nameLabel = UILabel()
    let detailLabel = UILabel()
    
    var cellButton = UIButton()
    var cellLabel = UILabel()
    var book : Book!
    
    // MARK: Initalizers
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }
    
    func configureCellCon(botones:Int, titulo:String, book:Book) {
        self.book = book
        let marginGuide = contentView.layoutMarginsGuide
        // configure titleLabel
        contentView.addSubview(nameLabel)
        nameLabel.translatesAutoresizingMaskIntoConstraints = false
        nameLabel.leadingAnchor.constraint(equalTo: marginGuide.leadingAnchor).isActive = true
        nameLabel.topAnchor.constraint(equalTo: marginGuide.topAnchor).isActive = true
        nameLabel.trailingAnchor.constraint(equalTo: marginGuide.trailingAnchor).isActive = true
        nameLabel.numberOfLines = 0
        nameLabel.font = UIFont(name: "AvenirNext-DemiBold", size: 16)
        nameLabel.text = book.name
    
        // configure authorLabel
        contentView.addSubview(detailLabel)
        detailLabel.translatesAutoresizingMaskIntoConstraints = false
        detailLabel.leadingAnchor.constraint(equalTo: marginGuide.leadingAnchor).isActive = true
        //        detailLabel.bottomAnchor.constraint(equalTo: marginGuide.bottomAnchor).isActive = true
        detailLabel.trailingAnchor.constraint(equalTo: marginGuide.trailingAnchor).isActive = true
        detailLabel.topAnchor.constraint(equalTo: nameLabel.bottomAnchor).isActive = true
        detailLabel.numberOfLines = 0
        detailLabel.font = UIFont(name: "Avenir-Book", size: 12)
        detailLabel.textColor = UIColor.lightGray
        detailLabel.text = book.details
    
        var lastButton = UIButton()
        if(book.buttonsAttibutes.count == 1) {
            let button = UIButton()
            button.tag = 0
            button.backgroundColor = UIColor.init(white: 0.9, alpha: 0.0)
            //            button.setBackgroundColor(color: UIColor.white, forState: .normal)
            //            button.setBackgroundColor(color: UIColor.blue, forState: .normal)
            button.setTitle(book.buttonsAttibutes[0].title, for: .normal)
            button.setTitleColor(UIColor.blue.withAlphaComponent(0.7), for: .normal)
            button.setTitleColor(UIColor.init(white: 0.8, alpha: 1), for: .highlighted)
            button.layer.borderWidth = 0
            button.layer.borderColor = UIColor.blue.cgColor
            contentView.addSubview(button)
            button.translatesAutoresizingMaskIntoConstraints = false
            contentView.addConstraints([
                NSLayoutConstraint(item: button, attribute: .leftMargin, relatedBy: .equal, toItem: contentView, attribute: .leftMargin, multiplier: 1.0, constant: 20),
                NSLayoutConstraint(item: button, attribute: .rightMargin, relatedBy: .equal, toItem: contentView, attribute: .rightMargin, multiplier: 1.0, constant: -20),
                ])
            button.topAnchor.constraint(equalTo: detailLabel.bottomAnchor).isActive = true
            button.bottomAnchor.constraint(equalTo: marginGuide.bottomAnchor).isActive = true
            button.addTarget(self, action:#selector(mostrarMensaje), for:.touchUpInside)
            return
        }else if(book.buttonsAttibutes.count == 0) {
            detailLabel.bottomAnchor.constraint(equalTo: marginGuide.bottomAnchor).isActive = true
            return
        }
    
        if(book.buttonsAttibutes.count > 0) {
            for x in 0...(book.buttonsAttibutes.count - 1) {
                let button = UIButton()
                button.tag = x
                button.backgroundColor = UIColor.init(white: 0.9, alpha: 0.0)
                //            button.setBackgroundColor(color: UIColor.white, forState: .normal)
                //            button.setBackgroundColor(color: UIColor.blue, forState: .normal)
                button.setTitle(book.buttonsAttibutes[x].title, for: .normal)
                button.setTitleColor(UIColor.blue.withAlphaComponent(0.7), for: .normal)
                button.setTitleColor(UIColor.init(white: 0.8, alpha: 1), for: .highlighted)
                button.layer.borderWidth = 0
                button.layer.borderColor = UIColor.blue.cgColor
                contentView.addSubview(button)
                button.translatesAutoresizingMaskIntoConstraints = false
                //            button.leadingAnchor.constraint(equalTo: marginGuide.leadingAnchor).isActive = true
                //            button.trailingAnchor.constraint(equalTo: marginGuide.trailingAnchor).isActive = true
    
                contentView.addConstraints([
                    NSLayoutConstraint(item: button, attribute: .leftMargin, relatedBy: .equal, toItem: contentView, attribute: .leftMargin, multiplier: 1.0, constant: 20),
                    NSLayoutConstraint(item: button, attribute: .rightMargin, relatedBy: .equal, toItem: contentView, attribute: .rightMargin, multiplier: 1.0, constant: -20),
                    ])
    
                if(x == 0){
                    if #available(iOS 11.0, *) {
                        button.layer.cornerRadius = 8
                        button.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
                    } else {
                        button.layer.cornerRadius = 5
                    }
                    button.topAnchor.constraint(equalTo: detailLabel.bottomAnchor).isActive = true
                }else if(x == (book.buttonsAttibutes.count - 1)){
                    if #available(iOS 11.0, *) {
                        button.layer.cornerRadius = 8
                        button.layer.maskedCorners = [.layerMinXMaxYCorner, .layerMaxXMaxYCorner]
                    } else {
                        button.layer.cornerRadius = 5
                    }
                    button.topAnchor.constraint(equalTo: lastButton.bottomAnchor).isActive = true
                    button.bottomAnchor.constraint(equalTo: marginGuide.bottomAnchor).isActive = true
                }else{
                    button.topAnchor.constraint(equalTo: lastButton.bottomAnchor).isActive = true
                }
    
                button.addTarget(self, action:#selector(mostrarMensaje), for:.touchUpInside)
                lastButton = button
            }
        }
    
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func mostrarMensaje(sender: UIButton){
    
        let message = self.book.buttonsAttibutes[sender.tag].message
        let alertController = UIAlertController(title: "\(self.book.name)", message: message, preferredStyle: UIAlertControllerStyle.alert)
        let okAction = UIAlertAction(title: "Cerrar", style: UIAlertActionStyle.default) {
            (result : UIAlertAction) -> Void in
        }
    
        alertController.addAction(okAction)
        if let myViewController = parentViewController {
            print(myViewController.title ?? "ViewController without title.")
            myViewController.present(alertController, animated: true, completion: nil)
        }
    } 
    }
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Andres Paladines    6 年前

    好的,我选择了“InterfaceBuilder”,并且做得很好。 然而,我将阅读更多关于编程方法的内容。 谢谢大家的指导。

    以编程方式创建按钮和约束时,它们会在同一单元格上生成更多约束,从而导致崩溃。