4

这是一个简单的问题:为什么以下代码会导致“控件可能到达非无效函数结束”警告?在什么情况下两个 return 语句之一不会被命中?return将第二条语句放在else块之外会是更标准的编码实践吗?这确实使警告静音,但我很好奇它为什么存在。

- (CGFloat) tableView: (UITableView *) tableView heightForRowAtIndexPath: (NSIndexPath *) indexPath
{
    NSString *lineToFit = [[self.fetchedResultsController objectAtIndexPath: indexPath] line];
    NSString *actorToFit = [[self.fetchedResultsController objectAtIndexPath: indexPath] actor];
    CGSize lineSize = [lineToFit sizeWithFont: [UIFont boldSystemFontOfSize: 12.0f] constrainedToSize: CGSizeMake(320, 800)];
    CGSize actorSize = [actorToFit sizeWithFont: [UIFont boldSystemFontOfSize: 12.0f] constrainedToSize: CGSizeMake(320, 800)];

    if (shouldDisplayExtra) {
        kExtraCellType cellType = [self cellIsGoingToContainExtraView];
        if (cellType != kExtraCellTypeNone) {
            [cellsToContainExtra setValue: [NSNumber numberWithInt: cellType] forKey: lineIDString];
            return lineSize.height + actorSize.height + 200;
        }
    } else {
        // Return the line height, actor height, plus a buffer.
        return lineSize.height + actorSize.height;
    }
}
4

4 回答 4

13

您确实有一个无法返回的条件:

如果shouldDisplayExtra存在,cellType == kExtraCellTypeNone则没有定义返回...

您应该在条件中添加一个 else 块:

    if (cellType != kExtraCellTypeNone) {
        [cellsToContainExtra setValue: [NSNumber numberWithInt: cellType] forKey: lineIDString];
        return lineSize.height + actorSize.height + 200;
    } else {
       // return something here
    }
于 2013-03-06T13:25:42.013 回答
2

朋友很简单,如果我们声明 int 类型的函数,并且在 if,for..etc 语句中我们返回一个值,那么它会显示这个警告 bcz 如果不满足上述任何一个条件,那么它将返回什么,所以我们应该在结尾或 else 块中返回任何值。

于 2014-01-09T18:50:55.103 回答
1

返回零;为我工作。

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section

{

if(tableViewObj == tableView) //here u can make decision
{

UIView *footerView=[[UIView alloc]initWithFrame:CGRectMake(self.view.frame.origin.x,self.view.frame.origin.y, 320, 40)];

    UIButton *addcharity=[UIButton buttonWithType:UIButtonTypeCustom];

    [addcharity setTitle:@"Help" forState:UIControlStateNormal];
    [addcharity addTarget:self action:@selector(addCharity:) forControlEvents:UIControlEventTouchUpInside];

    [addcharity setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];


    addcharity.frame=CGRectMake(0, 0, 130, 30); //set some large width to ur title

    [footerView addSubview:addcharity];

    return footerView;
}

return nil;

}

我希望它可以帮助某人...

于 2015-05-01T16:46:50.167 回答
0

编译器会这样处理,因为“main”块中没有 return 语句。

尝试这样做,警告将消失:

- (CGFloat) tableView: (UITableView *) tableView heightForRowAtIndexPath: (NSIndexPath *) indexPath
{
    NSString *lineToFit = [[self.fetchedResultsController objectAtIndexPath: indexPath] line];
    NSString *actorToFit = [[self.fetchedResultsController objectAtIndexPath: indexPath] actor];
    CGSize lineSize = [lineToFit sizeWithFont: [UIFont boldSystemFontOfSize: 12.0f] constrainedToSize: CGSizeMake(320, 800)];
    CGSize actorSize = [actorToFit sizeWithFont: [UIFont boldSystemFontOfSize: 12.0f] constrainedToSize: CGSizeMake(320, 800)];

    if (shouldDisplayExtra) {
        kExtraCellType cellType = [self cellIsGoingToContainExtraView];
        if (cellType != kExtraCellTypeNone) {
            [cellsToContainExtra setValue: [NSNumber numberWithInt: cellType] forKey: lineIDString];
            return lineSize.height + actorSize.height + 200;
        }
    }
        // Return the line height, actor height, plus a buffer.
        return lineSize.height + actorSize.height;
}

它将具有您想要的行为,并且没有警告。

于 2013-03-06T13:22:38.660 回答