2012-05-16 18 views
0

這是APICallsViewController文件中的代碼和一些截圖。我的應用崩潰了,我不確定這是否是原因。針對iOS的Facebook API中的分析器警告

感謝您的幫助

enter image description here

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

    UIFont *cellFont = [UIFont boldSystemFontOfSize:14.0]; 
    UIFont *detailCellFont = [UIFont fontWithName:@"Helvetica" size:12.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 darkGrayColor]; 
     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:@"MenuButton.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

else聲明UIButton *button未初始化。看看如何在if語句是:

button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 

但在else初始化丟失。你可以把後:

} else { 
     textLabel = (UILabel *)[cell.contentView viewWithTag:TITLE_TAG]; 
     detailTextLabel = (UILabel *)[cell.contentView viewWithTag:DESCRIPTION_TAG]; 
     button = (UIButton *)[cell.contentView viewWithTag:indexPath.row]; // <---- add this 

並移動這樣的:在if語句結束

button.tag = indexPath.row; 

[cell.contentView addSubview:button]; 

...

+0

真棒感謝您的幫助! 。它完全沒有更多的崩潰 – hanumanDev

3

button變量是一個局部變量。除非您使用ARC,否則不保證將其初始化。這可能是無稽之談。分析器警告你,當你試圖向其發送消息時,它並沒有清楚它已經分配了有意義的地址(frame屬性的設置者是你正在發送的消息的設置者)。

考慮一下:cell不是nil,並且在其子視圖中找不到按鈕。如果是這種情況,那麼應用程序將到達相關線路,而沒有分配到button變量。它可以包含一個絕對的指針。如果嘗試取消引用指針向對象發送消息,則可能會在應用程序中的任何位置塗寫內存。

現在,可能會出現這種情況,您知道它可以保證找到一個按鈕的子類。但分析儀無法解決這個問題。所以無論如何它警告你。

您應該將button變量初始化爲nil。如果有可能找不到按鈕,則應該處理該情況。