2016-07-21 2 views
0

내가 튜토리얼에 의해 응용 프로그램을 (개발하고 있어요,하지만 튜토리얼은 스위프트를 위해 작성 내가 목표 - C를 배우고 있습니다. 표시 source아이폰 OS 사용자 정의보기는

튜토리얼하지만 문제가 없습니다 나는 단지 장면이 사용자 정의 컨트롤러 중 하나를 사용할 때 작동하는 사용자 정의 컨트롤러 (RatingController)를 개발하고 있어요 사용자 정의 컨트롤러 소스가 여기에 있습니다 :.

@implementation RatingController 

//MARK: Properties 
const int spacing = 5; 
const int starCount = 5; 
UIButton *ratingButtons[starCount]; 

-(void)setRating:(int)rating{ 
    _rating = rating; 
    [self setNeedsLayout]; 
} 

-(int)getRating{ 
    return _rating; 
} 

//MARK: Initialization 
- (instancetype)initWithCoder:(NSCoder *)coder 
{ 
    self = [super initWithCoder:coder]; 
    if (self) { 
     //load the images 
     UIImage *filledStarImage = [UIImage imageNamed:@"filledStar"]; 
     UIImage *emptyStarImage = [UIImage imageNamed:@"emptyStar"]; 

     for (int i = 0; i < starCount; i++) { 
      //create button 
      UIButton *button = [[UIButton alloc]init]; 

      //set the images 
      [button setImage:emptyStarImage forState:UIControlStateNormal]; 
      [button setImage:filledStarImage forState:UIControlStateSelected]; 
      [button setImage:filledStarImage forState:UIControlStateHighlighted|UIControlStateSelected]; 

      //No additional highlight when switching state 
      button.adjustsImageWhenHighlighted = false; 

      //hook up the method which should be executed when the user touches a button 
      [button addTarget:self action:@selector(ratingButtonTapped:) forControlEvents:UIControlEventTouchDown]; 

      //add the button the the ratingButton array 
      ratingButtons[i] = button; 

      //add the button to the view 
      [self addSubview:button]; 
     } 
    } 
    return self; 
} 

-(void) layoutSubviews{ 
    //Set the button's width and height to the height of the frame. 
    const int buttonSize = self.frame.size.height;  
    CGRect buttonFrame = CGRectMake(0, 0, buttonSize, buttonSize); 

    for(int i = 0; i < starCount; i++){ 
     buttonFrame.origin.x = (i * (buttonSize + spacing)); 
     ratingButtons[i].frame = buttonFrame; 
    } 

    [self updateButtonSelectionState]; 
} 

-(CGSize) intrinsicContentSize{ 
    const int buttonSize = self.frame.size.height; 
    const int width = (buttonSize * starCount) + (spacing * (starCount -1)); 

    return CGSizeMake(width, buttonSize); 
} 

//MARK: Button Action 
-(void) updateButtonSelectionState{ 
    for(int i = 0; i < starCount; i++) 
     ratingButtons[i].selected = i < _rating; 
} 

-(void) ratingButtonTapped:(UIButton *) button{ 
    for(int i = 0; i < starCount; i++){ 
     if(ratingButtons[i] == button){ 
      _rating = i + 1; 
     } 
    } 
    [self updateButtonSelectionState]; 
} 

@end 

내가 1과 사용자 정의 컨트롤러의 2를 넣었을 때 장면 마지막에 추가 된 것을 볼 수 있습니다 (마지막 목록 항목 만 사용자 정의 컨트롤러를 보여주는 목록에 있음) 누군가가 도와 줄 수 있기를 바랍니다. 이자형.

대단히 감사합니다!

+0

한 장면에서 두 컨트롤러가 무슨 뜻입니까? 컨테이너보기에서 사용하고 있습니까? –

+0

여기처럼 : https://snag.gy/n0jK1h.jpg 모든 목록 항목에 별표가 포함되어야합니다. –

+0

tableview 데이터 소스 코드를 게시 할 수 있습니까? cellForRowAtIndexPath. – JingJingTao

답변

0

Edit2가 :

는 속성 및 변경 가능한 배열에 ratingButton 배열을 변경,이 문제가 해결.

편집 :

또한 당신의 버튼이 제대로 작동에 'updateButtonSelectionState'의 코드 당신의 초기화에 ratingButtons 배열에 것을 추가되고 확인. 원래

는 :

그것은 당신의 '평가'세터를 점검 가치, 브레이크 포인트를 설정하고 올바른 값의 처음 두 세포에 전달되고 있는지 확인합니다.

또한 완전한 예제를 다운로드 할 수도 있지만 신속하게 처리 할 수는 있지만 적어도 비교할 수있는 행운을 남깁니다.

+0

ratingButtons [i] .frame = buttonFrame;에 중단 점을 넣으면 ratingButtons 배열이 채워집니다. line이 결과를 얻습니다 : https://snag.gy/pCyxKE.jpg 그리고 'rating'설정자가 작동하고, mealsArray의 마지막 항목 만 layoutSubviews()를 호출합니다. 방법 두 번 연속으로, 그 이상한 아닌가요? –

+0

layoutSubviews가 세 번 호출되기를 기대합니다. 명백한 체크, init의 중단 점은 세 번 트리거되고, layoutSubviews에 설정된 실제 프레임을 확인하십시오. – JingJingTao

+0

initWithCoder가 3 번 실행됩니다. layoutSubViews가 처음 buttonFrame.frame의 모든 값을 호출 할 때 0.0이 예기치 않은 동작입니다. 다른 호출 값은 정확합니다. –