首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >以快捷方式以编程方式定位微视图

以快捷方式以编程方式定位微视图
EN

Stack Overflow用户
提问于 2016-08-06 11:14:09
回答 1查看 48关注 0票数 0

在你最后的帮助下,我的循环运转良好。但只有一次。如果我试图重新加载它,我会得到以下错误:“超出范围的索引”在下面的行中:

代码语言:javascript
复制
let cardOnTop = cards[index-8]

有人能帮我吗?

这是我的代码:

代码语言:javascript
复制
func layoutCards() {

        //  create cards array with several elements
        var cards = (1...64).map { _ in UIView() }// array with several cards


        //Loop through each card in the array
        for index in 0...cards.count-1 {



            // place the card in the view and turn off translateAutoresizingMask
            let thisCard = cards[index]

            thisCard.layer.borderWidth = 1
            thisCard.layer.borderColor = UIColor.blackColor().CGColor
            thisCard.backgroundColor = UIColor.greenColor()

            thisCard.translatesAutoresizingMaskIntoConstraints = false
            midView.addSubview(thisCard)

            //set the height and width constraints
            let widthConstraint = NSLayoutConstraint(item: thisCard, attribute: .Width, relatedBy: .Equal, toItem: midView, attribute: .Width, multiplier: 0.125, constant: 0)
            let heightConstraint = NSLayoutConstraint(item: thisCard, attribute: .Height, relatedBy: .Equal, toItem: midView, attribute: .Height, multiplier: 0.125, constant: 0)
            midView.addConstraints([heightConstraint, widthConstraint])

            //set the horizontal  position

            if (columnCounter > 0) {

                // card is not in the first column
                let cardOnTheLeft = cards[index-1]

                let leftSideConstraint = NSLayoutConstraint(item: thisCard, attribute: .Left, relatedBy: .Equal, toItem: cardOnTheLeft, attribute: .Right, multiplier: 1, constant: 0)

                //add constraint to the contentView
                midView.addConstraint(leftSideConstraint)

            } else {

                //card is in the first column

                let leftSideConstraint = NSLayoutConstraint(item: thisCard, attribute: .Left, relatedBy: .Equal, toItem: midView, attribute: .Left, multiplier: 1, constant: 0)

                //add constraint to the contentView
                midView.addConstraint(leftSideConstraint)

            }


            //set the vertical position

            if (rowCounter > 0) {

                // card is not in the first row
                let cardOnTop = cards[index-8]

                let topConstraint = NSLayoutConstraint(item: thisCard, attribute: .Top, relatedBy: .Equal, toItem: cardOnTop, attribute: .Bottom, multiplier: 1, constant: 0)

                // add constraint to the contentView
                midView.addConstraint(topConstraint)

            } else {

                //card is in the first row

                let topConstraint = NSLayoutConstraint(item: thisCard, attribute: .Top, relatedBy: .Equal, toItem: midView, attribute: .Top, multiplier: 1, constant: 0)

                //add constraint to the contentView
                midView.addConstraint(topConstraint)

            }



            //increment the column counter
            columnCounter = columnCounter+1

            //if the column counter reaches the fifth column reset it and increase the row counter
            if (columnCounter >= 8) {
                columnCounter = 0
                rowCounter = rowCounter+1
            }




        } // end of the loop



    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-06 11:42:33

你说这是失败的,当你运行它第二次。这是因为您显然已经将rowCountercolumnCounter声明为属性,而且每次调用layoutCards()时都不会将rowCountercolumnCounter设置为0

使rowCountercolumnCounter成为layoutCards()的局部变量

代码语言:javascript
复制
func layoutCards() {
    var rowCounter = 0
    var columnCounter = 0

    //  create cards array with several elements
    var cards = (1...64).map { _ in UIView() }// array with several cards
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38803642

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档