控制可能到达结束非空函数

Control may reach end non-void function

附加代码返回错误:

Control may reach end non-void function

代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.row == 0) {
        FirstCustomCell *fCustomCell = [tableView dequeueReusableCellWithIdentifier:@"firstCustomCell" forIndexPath:indexPath];

        if (fCustomCell == nil) {

            fCustomCell = [[FirstCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"firstCustomCell"];
        }

        return fCustomCell;

    }

    else if (indexPath.row == 1) {
        SecondCustomCell *sCustomCell = [tableView dequeueReusableCellWithIdentifier:@"secondCustomCell" forIndexPath:indexPath];

        if (sCustomCell == nil) {

            sCustomCell = [[SecondCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SecondCustomCell"];
        }

        return sCustomCell;

    }
} //<-- Control may reach non-void function (I precise that's the end of the cellForRowAtIndexPath method)

我知道问题出在 "return" 但我该如何消除错误?

您考虑了 indexPath.row == 0indexPath.row == 1 的情况,但编译器说:如果行不是 0 或 1,我应该 return 什么?

您可能希望在方法的末尾有一个 return nil;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    if (indexPath.row == 0) 
    {
        FirstCustomCell *fCustomCell = [tableView dequeueReusableCellWithIdentifier:@"firstCustomCell" forIndexPath:indexPath];
        if (fCustomCell == nil) 
        {
            fCustomCell = [[FirstCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"firstCustomCell"];
        }
        return fCustomCell;
    }
    else if (indexPath.row == 1) 
    {
        SecondCustomCell *sCustomCell = [tableView dequeueReusableCellWithIdentifier:@"secondCustomCell" forIndexPath:indexPath];
        if (sCustomCell == nil) 
        {
            sCustomCell = [[SecondCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"secondCustomCell"];
        }
        return sCustomCell;
    }
    return nil; //<--Add this line
}

或者 "else" 案例:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    if (indexPath.row == 0) 
    {
        FirstCustomCell *fCustomCell = [tableView dequeueReusableCellWithIdentifier:@"firstCustomCell" forIndexPath:indexPath];
        if (fCustomCell == nil) 
        {
            fCustomCell = [[FirstCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"firstCustomCell"];
        }
        return fCustomCell;
    }
    else if (indexPath.row == 1) 
    {
        SecondCustomCell *sCustomCell = [tableView dequeueReusableCellWithIdentifier:@"secondCustomCell" forIndexPath:indexPath];
        if (sCustomCell == nil) 
        {
            sCustomCell = [[SecondCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"secondCustomCell"];
        }
        return sCustomCell;
    }
    else //<--Add this clause
    {
        OtherCustomCell *oCustomCell = [tableView dequeueReusableCellWithIdentifier:@"otherCustomCell" forIndexPath:indexPath];
        if (oCustomCell == nil) 
        {
            oCustomCell = [[OtherCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"otherCustomCell"];
        }
        return sCustomCell;
    }
}

注意:您的重用标识符也有拼写错误:

"secondCustomCell""SecondCustomCell"

不同

添加 return nil; 行。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.row == 0) {
        FirstCustomCell *fCustomCell = [tableView dequeueReusableCellWithIdentifier:@"firstCustomCell" forIndexPath:indexPath];

        if (fCustomCell == nil) {

            fCustomCell = [[FirstCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"firstCustomCell"];
        }

        return fCustomCell;

    }

    else if (indexPath.row == 1) {
        SecondCustomCell *sCustomCell = [tableView dequeueReusableCellWithIdentifier:@"secondCustomCell" forIndexPath:indexPath];

        if (sCustomCell == nil) {

            sCustomCell = [[SecondCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SecondCustomCell"];
        }

        return sCustomCell;

    }
    return nil;
     }

我可能会补充一点,您需要确保您 fCustomCell 和您的 sCustomCell 不是 (!=) nil.

if (!fCustomCell) {
    fCustomCell = [UITableViewCell alloc] initWithStyle:/*aStyle*/ reuseIdentifier:/*identifier*/];
}

标识符可以是在方法开头定义的静态 NSString,如下所示:static NSString *identifier = @"cell";

看看一些教程。