2016-08-06 45 views
0

隨着您的最後一個幫助,我的循環運行良好。但只有一次。如果我嘗試重新加載它,我得到以下錯誤:「索引超出範圍」下面一行:以編程方式快速定位多個視圖

let cardOnTop = cards[index-8] 

有人能幫助我嗎?

這裏是我的代碼:

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 



    } 
+0

對於指數的任何值小於8,該行正試圖訪問小於0的第一次調用將尋找卡[索引-8],這顯然超出範圍。您需要更改代碼以防止出現這種情況。 – Westside

+0

if(rowCounter> 0 && index> 7){... – Idan

回答

0

你說這是失敗的,當你運行它第二次。這是因爲您顯然已將rowCountercolumnCounter聲明爲屬性,並且您沒有將rowCountercolumnCounter設置回0,每次調用layoutCards()

rowCountercolumnCounter是局部變量layoutCards()

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

再次感謝您!現在它可以工作。這樣一個愚蠢的錯誤,我真的很盲目...... – Roman