2012-07-17 2 views
0

UIButton을 만드는 일부 코드에 대해 다음과 같은 오류 메시지가 나타납니다. 이 오류를 해결하기 위해이를 초기화하는 가겠어요 방법에 대해 분석기는 나에게 경고하는 것이 : 논리 오류가메시지 식의 Receiver를 수정하는 방법은 초기화되지 않은 값 경고입니다.

: 메시지 발현 수신기 오류가 굵은

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UILabel *textLabel; 
    UILabel *detailTextLabel; 
    UIButton *button; 

    UIFont *cellFont = [UIFont boldSystemFontOfSize:10.0]; 
    UIFont *detailCellFont = [UIFont fontWithName:@"Helvetica" size:8.0]; 

    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 

     // Initialize API title UILabel 
     textLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease]; 
     textLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin; 
     textLabel.tag = TITLE_TAG; 
     textLabel.font = cellFont; 
     [cell.contentView addSubview:textLabel]; 

     // Initialize API description UILabel 
     detailTextLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease]; 
     detailTextLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin; 
     detailTextLabel.tag = DESCRIPTION_TAG; 
     detailTextLabel.font = detailCellFont; 
     detailTextLabel.textColor = [UIColor blackColor]; 
     detailTextLabel.lineBreakMode = UILineBreakModeWordWrap; 
     detailTextLabel.numberOfLines = 0; 
     [cell.contentView addSubview:detailTextLabel]; 

     // Initialize API button UIButton 
     button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     button.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin; 
     [button setBackgroundImage:[[UIImage imageNamed:@"blueButton.png"] 
            stretchableImageWithLeftCapWidth:9 topCapHeight:9] 
          forState:UIControlStateNormal]; 
     [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
     [button addTarget:self action:@selector(apiButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; 
     [cell.contentView addSubview:button]; 
    } else { 
     textLabel = (UILabel *)[cell.contentView viewWithTag:TITLE_TAG]; 
     detailTextLabel = (UILabel *)[cell.contentView viewWithTag:DESCRIPTION_TAG]; 
     // For the button cannot search by tag since it is not constant 
     // and is dynamically used figure out which button is clicked. 
     // So instead we loop through subviews of the cell to find the button. 
     for (UIView *subview in cell.contentView.subviews) { 
      if([subview isKindOfClass:[UIButton class]]) { 
       button = (UIButton *)subview; 
      } 
     } 
    } 

    CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT); 

    // The API title 
    NSString *cellText = [[apiMenuItems objectAtIndex:indexPath.row] objectForKey:@"title"]; 
    CGSize labelSize = [cellText sizeWithFont:cellFont 
          constrainedToSize:constraintSize 
           lineBreakMode:UILineBreakModeWordWrap]; 
    textLabel.frame = CGRectMake(20, 2, 
           (cell.contentView.frame.size.width-40), 
           labelSize.height); 
    textLabel.text = cellText; 

    // The API description 
    NSString *detailCellText = [[apiMenuItems objectAtIndex:indexPath.row] objectForKey:@"description"]; 
    CGSize detailLabelSize = [detailCellText sizeWithFont:detailCellFont 
             constrainedToSize:constraintSize 
              lineBreakMode:UILineBreakModeWordWrap]; 
    detailTextLabel.frame = CGRectMake(20, (labelSize.height + 4), 
             (cell.contentView.frame.size.width-40), 
             detailLabelSize.height); 
    detailTextLabel.text = detailCellText; 


    // The API button 
    CGFloat yButtonOffset = labelSize.height + detailLabelSize.height + 15; 
    **button.frame = CGRectMake(20, yButtonOffset, (cell.contentView.frame.size.width-40), 44);** 
    [button setTitle:[[apiMenuItems objectAtIndex:indexPath.row] objectForKey:@"button"] 
      forState:UIControlStateNormal]; 
    // Set the tag that will later identify the button that is clicked. 
    button.tag = indexPath.row; 


    return cell; 
} 
의 라인에 발생하는 초기화되지 않은 값

입니다

도움을 주셔서 감사합니다.

+1

에이 라인

UIButton *button; 

을 변경하려고? – jrturton

+0

'button'과'cell'을 초기화하는 코드를 보여줄 수 있습니까? ('indexPath'는 메소드에 전달 된 매개 변수라고 가정합니다. 가장 가능성이 높은 것은'-tableView : cellForRowAtIndexPath :') –

+0

굵게 표시된 줄에서 오류가 발생합니다. 덕분에 – hanumanDev

답변

1

코드에서 아무 곳이나 같이 했습니까?

UIButton* button = [[UIButton alloc] initWithType:UIButtonTypeCustom]; 
+0

(셀과 동일) –

+0

예를 들어 더 많은 코드를 추가했습니다. 어떤 도움을 주셔서 감사합니다 – hanumanDev

1

오류 버튼이 초기화되지 않았 음을 알려 :

UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom]; 

... 또는이 (유형은 UIButtonRoundRect 수 있습니다).
오류가 발생되는 라인

UIButton *button = [[UIButton alloc] init]Autorelease]; //remove Autorelease if using ARC 
+0

감사합니다! 그리고 나서 [버튼 해제]; "return cell; 선? 너무 빨리 풀어주지 않을거야? – hanumanDev

+1

내 생각 엔 "isKindOfClass"가 하위보기 (단추)를 찾지 못해 변수가 초기화되지 않은 상태로 남아 있습니다. –

+0

@hanumanDev : 업데이트 된 코드 – user523234

관련 문제