2016-08-06 4 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

, 그 선은 첫 번째 호출은 카드 [찾고있을 것이다 0보다 작은 인덱스를 액세스를 시도 -8]은 분명히 범위를 벗어났습니다. 이 문제로부터 보호하려면 코드를 변경해야합니다. – Westside

+0

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

답변

0

:

여기 내 코드입니다. 이는 rowCountercolumnCounter이 속성으로 선언되어 있고 layoutCards()이 호출 될 때마다 rowCountercolumnCounter0으로 다시 설정하지 않았기 때문입니다.

rowCounter 확인하고 columnCounterlayoutCards()에 지역 변수가 될 : 이하 8 이상의 인덱스의 값을

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

관련 문제