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

标签 ios objective-c uitableview

<分区>

附加代码返回错误:

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 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
}

或者可能是“其他”情况:

- (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"

不同

关于ios - 控制可能到达结束非空函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27742523/

上一篇:ios - 如何在没有丑陋黑客的情况下构建一个带有图像的视差样式/弹性 TableView 标题?

下一篇:ios - 如何在不知道 UUID、主要值和次要值的情况下获取 objective-c 中信标的 mac 地址?

相关文章:

ios - 启用 ARC 的 AFNetworking 2 后异步内存泄漏

objective-c - 报告哪个方法导致了我的 NSException?

ios - 有没有办法在 iOS 中使用表格 View 设计类似 Tinder/Twitter-self-profile 的标题效果?

ios - Xcode 上传二进制 Apple 的 Web 服务操作不成功

html - 检查是全屏网络应用程序

iphone - 图像正好在 iPad 的中间

ios - 为 UITableCellView 内的 View 添加 NSLayoutConstraint

swift - TableView w。重新加载新数据时动态高度滚动

ios - 无法解码类(SKCameraNode)的对象

objective-c - NSUserDefaults 和 NSMutableArray 的内存泄漏