在不使用CollectionView的情况下在网格视图中列出UIButtons

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在不使用CollectionView的情况下在网格视图中列出UIButtons相关的知识,希望对你有一定的参考价值。

我将有一个UIButton对象的平方网格。这些UIButtons中的每一个都引用了一个唯一的选项,而id来自数组API。

选项API的示例

options =     (
            {
        name = Color;
        "option_id" = 16;
        "product_option_id" = 1324;
        "product_option_value" =             (
                            {
                image = "example.com/image.jpg";
                name = red;
                "option_value_id" = 104;
                price = 0;
                "price_prefix" = "+";
                "product_option_value_id" = 4650;
            },
                            {
                image = "https://www.ishtari.com/image/cache/data/categories/color/Blue-1125.000000x1125.000000_0.jpg";
                name = blue;
                "option_value_id" = 91;
                price = 0;
                "price_prefix" = "+";
                "product_option_value_id" = 4649;
            }
        );

我正在阅读API并在UIButtons中列出数组

else if dict["type"].stringValue == "image" {
                        let imageOptionView = UIView(frame: CGRect(x: 5, y: Y, width: self.optionView.frame.size.width - 10, height: 30))
                        imageOptionView.isUserInteractionEnabled = true
                        self.optionView.addSubview(imageOptionView)
                        imageOptionView.tag = i
                        self.imageId.append("")
                        Y += 30;
                        let productOptionArray : JSON = JSON(dict["product_option_value"].arrayObject!)
                        let imagesArray:NSMutableArray = []
                        var internalY:CGFloat = 0
                        for j in 0..<productOptionArray.count {


                            let img = RadioButton(frame: CGRect(x: 5, y: internalY, width: 30, height: 30))

                            let imgName = productOptionArray[j]["name"].stringValue
                            img.setTitle(imgName, for: .normal)
                            img.setTitleColor(UIColor.black, for: .normal)
                            img.titleLabel?.font = UIFont.init(name: REGULARFONT, size: 13.0)


                            let images = productOptionArray[j]["image"].stringValue
                            let data = NSData(contentsOf: NSURL(string: images)! as URL)

                            img.setImage(UIImage(data: data! as Data), for: .normal)
                            img.titleEdgeInsets = UIEdgeInsetsMake(0, 6, 0, 0)
                            img.addTarget(self, action: #selector(self.onRadioButtonValueChangedAccount), for:.touchUpInside)

                            img.tag = j
                            imageOptionView.addSubview(img)
                            internalY += 40;

                            imagesArray.add(img)

            }   
                var paymentInformationContainerFrame = imageOptionView.frame
                    paymentInformationContainerFrame.size.height = internalY
                    imageOptionView.frame = paymentInformationContainerFrame
                    let imgButton:RadioButton = RadioButton()
                    //imgButton.groupButtons = imagesArray as! [RadioButton]

                    Y += internalY;

但是我希望它们在不使用CollectionView的情况下以编程方式列在网格视图中,这怎么可能?

你可以看到我定义了基于UIButton类的RadioButton类。我正在使用swift 3

编辑:在论坛中找到Objective-c的解决方案,然后我将其转换为swift 3 HERE

else if dict["type"].stringValue == "image" {
            let imageOptionView = UIView(frame: CGRect(x: 5, y: Y, width: self.optionView.frame.size.width - 10, height: 30))
            imageOptionView.isUserInteractionEnabled = true
            self.optionView.addSubview(imageOptionView)
            imageOptionView.tag = i
            self.imageId.append("")
            Y += 30;
            let productOptionArray : JSON = JSON(dict["product_option_value"].arrayObject!)
            let imagesArray:NSMutableArray = []
            var internalY:CGFloat = 0

                for j in 0..<productOptionArray.count {
                            var rows: Int = 5
                            var columns: Int = 1
                            for n in 0..<rows {
                                for m in 0..<columns {

                            var frame = CGRect(x: 1 + 45 * n, y: Int(internalY) * m, width: 40, height: 40)

                            var img = RadioButton(frame: frame) as? RadioButton

                            let imgName = productOptionArray[j]["name"].stringValue
                            img?.setTitle(imgName, for: .normal)
                            img?.setTitleColor(UIColor.black, for: .normal)
                            img?.titleLabel?.font = UIFont.init(name: REGULARFONT, size: 13.0)


                            let images = productOptionArray[j]["image"].stringValue
                            let data = NSData(contentsOf: NSURL(string: images)! as URL)
                            img?.setImage(UIImage(data: data! as Data), for: .normal)
                            img?.titleEdgeInsets = UIEdgeInsetsMake(0, 6, 0, 0)
                            img?.addTarget(self, action: #selector(self.onRadioButtonValueChangedAccount), for:.touchUpInside)

                            img?.tag = j + n * columns + m
                            imageOptionView.addSubview(img!)
                            internalY += 40;

                            imagesArray.add(img!)

                        print("images", images)
                        print("imagesArray", imagesArray)
                        }

                            }
                        }

                        var paymentInformationContainerFrame = imageOptionView.frame
                        paymentInformationContainerFrame.size.height = internalY
                        imageOptionView.frame = paymentInformationContainerFrame
                        let imgButton:RadioButton = RadioButton()
                        imgButton.groupButtons = imagesArray as! [RadioButton]

                        Y += internalY;

}

现在我在网格视图中获取按钮但是我没有得到正确的值。例如,不是显示3个视图(对象),而是重复1个视图(对象)5次而其他视图没有显示。加上这个视图和下一个视图之间的水平空间(Y),虽然我多次更改了Y的值,但没有工作。

imageArray和图像的打印结果

images https://example.com/option-1.jpg
imagesArray (
    "<RadioButton: 0x7fdfafce1b00; baseClass = UIButton; frame = (1 0; 40 40); opaque = NO; layer = <CALayer: 0x60400062c660>>",
    "<RadioButton: 0x7fdfafce2e30; baseClass = UIButton; frame = (46 0; 40 40); opaque = NO; layer = <CALayer: 0x600000629700>>",
    "<RadioButton: 0x7fdfafdc1d00; baseClass = UIButton; frame = (91 0; 40 40); opaque = NO; layer = <CALayer: 0x604000629e20>>",
    "<RadioButton: 0x7fdfafd99910; baseClass = UIButton; frame = (136 0; 40 40); opaque = NO; layer = <CALayer: 0x60400062e2a0>>",
    "<RadioButton: 0x7fdfafcdc960; baseClass = UIButton; frame = (181 0; 40 40); opaque = NO; layer = <CALayer: 0x60000062ff40>>"
)
images https://example.com/option-2.jpg
imagesArray (
    "<RadioButton: 0x7fdfafce1b00; baseClass = UIButton; frame = (1 0; 40 40); opaque = NO; layer = <CALayer: 0x60400062c660>>",
    "<RadioButton: 0x7fdfafce2e30; baseClass = UIButton; frame = (46 0; 40 40); opaque = NO; layer = <CALayer: 0x600000629700>>",
    "<RadioButton: 0x7fdfafdc1d00; baseClass = UIButton; frame = (91 0; 40 40); opaque = NO; layer = <CALayer: 0x604000629e20>>",
    "<RadioButton: 0x7fdfafd99910; baseClass = UIButton; frame = (136 0; 40 40); opaque = NO; layer = <CALayer: 0x60400062e2a0>>",
    "<RadioButton: 0x7fdfafcdc960; baseClass = UIButton; frame = (181 0; 40 40); opaque = NO; layer = <CALayer: 0x60000062ff40>>",
    "<RadioButton: 0x7fdfafdccc30; baseClass = UIButton; frame = (1 0; 40 40); opaque = NO; tag = 1; layer = <CALayer: 0x60400063e8a0>>"
)
images https://example.com/option-2.jpg
imagesArray (
    "<RadioButton: 0x7fdfafce1b00; baseClass = UIButton; frame = (1 0; 40 40); opaque = NO; layer = <CALayer: 0x60400062c660>>",
    "<RadioButton: 0x7fdfafce2e30; baseClass = UIButton; frame = (46 0; 40 40); opaque = NO; layer = <CALayer: 0x600000629700>>",
    "<RadioButton: 0x7fdfafdc1d00; baseClass = UIButton; frame = (91 0; 40 40); opaque = NO; layer = <CALayer: 0x604000629e20>>",
    "<RadioButton: 0x7fdfafd99910; baseClass = UIButton; frame = (136 0; 40 40); opaque = NO; layer = <CALayer: 0x60400062e2a0>>",
    "<RadioButton: 0x7fdfafcdc960; baseClass = UIButton; frame = (181 0; 40 40); opaque = NO; layer = <CALayer: 0x60000062ff40>>",
    "<RadioButton: 0x7fdfafdccc30; baseClass = UIButton; frame = (1 0; 40 40); opaque = NO; tag = 1; layer = <CALayer: 0x60400063e8a0>>",
    "<RadioButton: 0x7fdfafde4430; baseClass = UIButton; frame = (46 0; 40 40); opaque = NO; tag = 1; layer = <CALayer: 0x60400063bd00>>"
)
images https://example.com/option-2.jpg
imagesArray (
    "<RadioButton: 0x7fdfafce1b00; baseClass = UIButton; frame = (1 0; 40 40); opaque = NO; layer = <CALayer: 0x60400062c660>>",
    "<RadioButton: 0x7fdfafce2e30; baseClass = UIButton; frame = (46 0; 40 40); opaque = NO; layer = <CALayer: 0x600000629700>>",
    "<RadioButton: 0x7fdfafdc1d00; baseClass = UIButton; frame = (91 0; 40 40); opaque = NO; layer = <CALayer: 0x604000629e20>>",
    "<RadioButton: 0x7fdfafd99910; baseClass = UIButton; frame = (136 0; 40 40); opaque = NO; layer = <CALayer: 0x60400062e2a0>>",
    "<RadioButton: 0x7fdfafcdc960; baseClass = UIButton; frame = (181 0; 40 40); opaque = NO; layer = <CALayer: 0x60000062ff40>>",
    "<RadioButton: 0x7fdfafdccc30; baseClass = UIButton; frame = (1 0; 40 40); opaque = NO; tag = 1; layer = <CALayer: 0x60400063e8a0>>",
    "<RadioButton: 0x7fdfafde4430; baseClass = UIButton; frame = (46 0; 40 40); opaque = NO; tag = 1; layer = <CALayer: 0x60400063bd00>>",
    "<RadioButton: 0x7fdfafde6200; baseClass = UIButton; frame = (91 0; 40 40); opaque = NO; tag = 1; layer = <CALayer: 0x60400062b620>>"
)
images https://example.com/option-2.jpg
imagesArray (
    "<RadioButton: 0x7fdfafce1b00; baseClass = UIButton; frame = (1 0; 40 40); opaque = NO; layer = <CALayer: 0x60400062c660>>",
    "<RadioButton: 0x7fdfafce2e30; baseClass = UIButton; frame = (46 0; 40 40); opaque = NO; layer = <CALayer: 0x600000629700>>",
    "<RadioButton: 0x7fdfafdc1d00; baseClass = UIButton; frame = (91 0; 40 40); opaque = NO; layer = <CALayer: 0x604000629e20>>",
    "<RadioButton: 0x7fdfafd99910; baseClass = UIButton; frame = (136 0; 40 40); opaque = NO; layer = <CALayer: 0x60400062e2a0>>",
    "<RadioButton: 0x7fdfafcdc960; baseClass = UIButton; frame = (181 0; 40 40); opaque = NO; layer = <CALayer: 0x60000062ff40>>",
    "<RadioButton: 0x7fdfafdccc30; baseClass = UIButton; frame = (1 0; 40 40); opaque = NO; tag = 1; layer = <CALayer: 0x60400063e8a0>>",
    "<RadioButton: 0x7fdfafde4430; baseClass = UIButton; frame = (46 0; 40 40); opaque = NO; tag = 1; layer = <CALayer: 0x60400063bd00>>",
    "<RadioButton: 0x7fdfafde6200; baseClass = UIButton; frame = (91 0; 40 40); opaque = NO; tag = 1; layer = <CALayer: 0x60400062b620>>",
    "<RadioButton: 0x7fdfafde8320; baseClass = UIButton; frame = (136 0; 40 40); opaque = NO; tag = 1; layer = <CALayer: 0x60400063c400>>"
)
images https://example.com/option-2.jpg
imagesArray (
    "<RadioButton: 0x7fdfafce1b00; baseClass = UIButton; frame = (1 0; 40 40); opaque = NO; layer = <CALayer: 0x60400062c660>>",
    "<RadioButton: 0x7fdfafce2e30; baseClass = UIButton; frame = (46 0; 40 40); opaque = NO; layer = <CALayer: 0x600000629700>>",
    "<RadioButton: 0x7fdfafdc1d00; baseClass = UIButton; frame = (91 0; 40 40); opaque = NO; layer = <CALayer: 0x604000629e20>>",
    "<RadioButton: 0x7fdfafd99910; baseClass = UIButton; frame = (136 0; 40 40); opaque = NO; layer = <CALayer: 0x60400062e2a0>>",
    "<RadioButton: 0x7fdfafcdc960; baseClass = UIButton; frame = (181 0; 40 40); opaque = NO; layer = <CALayer: 0x60000062ff40>>",
    "<RadioButton: 0x7fdfafdccc30; baseClass = UIButton; frame = (1 0; 40 40); opaque = NO; tag = 1; layer = <CALayer: 0x60400063e8a0>>",
    "<RadioButton: 0x7fdfafde4430; baseClass = UIButton; frame = (46 0; 40 40); opaque = NO; tag = 1; layer = <CALayer: 0x60400063bd00>>",
    "<RadioButton: 0x7fdfafde6200; baseClass = UIButton; frame = (91 0; 40 40); opaque = NO; tag = 1; layer = <CALayer: 0x60400062b620>>",
    "<RadioButton: 0x7fdfafde8320; baseClass = UIButton; frame = (136 0; 40 40); opaque = NO; tag = 1; layer = <CALayer: 0x60400063c400>>",
    "<RadioButton: 0x7fdfafdea760; baseClass = UIButton; frame = (181 0; 40 40); opaque = NO; tag = 1; layer = <CALayer: 0x60400062f760>>"
)
答案

如果您不想使用集合视图,则可以使用包含多个水平堆栈视图的垂直堆栈视图,也可以使用滚动视图(如果您希望它们可滚动)或包含UIView和自定义布局代码。

请注意,可以使用约束来均匀地分隔固定数量的视图,这些约束将每个视图的中心位置设置为其容器视图的宽度/高度的一部分

对于x位置,您将水平中心约束设置为:

center = (width / (number_of_views + 1) * (view_index + 1)

因此,如果有3个视图,则第一个视图的水平中心视图0将是:

center = (width/4) * (0 + 1)

要么

center = width/4

因此,您将使最左侧视图的中心位置相对于超视图的宽度,乘数为0.25。将相同的公式应用于网格中的所有视图,x和y维度。

以上是关于在不使用CollectionView的情况下在网格视图中列出UIButtons的主要内容,如果未能解决你的问题,请参考以下文章

如何在不使用 AudioQueueRef 的情况下在 AudioQueue 中设置音量?

有啥方法可以在不使用 getImageData() 的情况下在 JavaScript 中找到像素的颜色?

在不使用 JOIN 的情况下在 HSQLDB 中进行透视

在不使用 boost::promise 的情况下在 boost 线程中返回值

如何在不使用 &nbsp 的情况下在行内元素之间添加空格 [重复]

如何在不使用 Segue 的情况下在单独的 UIViewController 中选择实例变量