控制可能到达非空函数

Lkm*_*Lkm 0 objective-c uitableview ios

附加的代码返回错误:

Control may reach end non-void function
Run Code Online (Sandbox Code Playgroud)

代码:

- (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)
Run Code Online (Sandbox Code Playgroud)

我知道问题特别出在“返回”上,但如何消除错误?

Jam*_*ter 5

您考虑了编译器所说的地点indexPath.row == 0和地点的情况indexPath.row == 1:如果行不是 0 或 1,我应该返回什么?

您可能希望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
}
Run Code Online (Sandbox Code Playgroud)

或者也许是“其他”情况:

- (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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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

"secondCustomCell" 不一样 "SecondCustomCell"