简体   繁体   English

预期返回“ UITableViewCell”的函数中缺少返回

[英]Missing return in a function expected to return 'UITableViewCell'

EDIT 1: Revised code - still does not work. 编辑1:修改的代码-仍然无法正常工作。

I've got two custom cells. 我有两个自定义单元。

The first cell (and only the first cell) will be of type CurrentIssueFrontCoverTableViewCell , and the rest of the cells will be of type CurrentIssueArticlesTableViewCell . 第一个单元格(只有第一个单元格)的类型为CurrentIssueFrontCoverTableViewCell ,其余单元格的类型为CurrentIssueArticlesTableViewCell I am getting the error, described in the title, when declaring my cellForRowAtIndexPath function. 声明cellForRowAtIndexPath函数时,出现标题中所述的错误。 Any idea of to fix this ? 有解决此问题的想法吗? Why is it not detecting that I'm returning the cell in my "if" loop ? 为什么没有检测到我要在“ if”循环中返回单元格?

override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell {

    let row = indexPath.row

    if indexPath.row == 0 {

        let cellWithCoverImage = tableView.dequeueReusableCellWithIdentifier(CurrentIssueFrontCoverTableCellIdentifier, forIndexPath: indexPath) as! CurrentIssueFrontCoverTableViewCell

        if let currentIssueFrontCoverObject = currentIssueObjects.objectAtIndex(indexPath.row) as? IssueElement {

            let title = currentIssueFrontCoverObject.title ?? ""

            let timeStampDateObject = NSDate(timeIntervalSince1970: NSTimeInterval(currentIssueFrontCoverObject.timeStamp))
            let timeStampDateString = dateFormatter.stringFromDate(timeStampDateObject)

            let issueNumber = currentIssueFrontCoverObject.issueNumber ?? ""
            let volumeNumber = currentIssueFrontCoverObject.volumeNumber ?? ""

            let nodeID = currentIssueFrontCoverObject.nodeID ?? 0

            let imageURL = currentIssueFrontCoverObject.imageURL ?? ""


            cellWithCoverImage.request?.cancel()

            if let coverImage = self.imageCache.objectForKey(imageURL) as? UIImage {
                cellWithCoverImage.currentIssueFrontCoverImageView.image = coverImage
            } else {
                cellWithCoverImage.currentIssueFrontCoverImageView.image = nil
                cellWithCoverImage.request = Alamofire.request(.GET, imageURL).responseImage() { response in
                    if let coverImage = response.result.value {
                        self.imageCache.setObject(response.result.value!, forKey: imageURL)
                        cellWithCoverImage.currentIssueFrontCoverImageView.image = coverImage

                    } else {

                    }
                }
            }
        } else {

        }

        return cellWithCoverImage

        // Populating data in the "Articles" type cells

} else if indexPath.row >= 1 {

        let cellWithoutCoverImage = tableView.dequeueReusableCellWithIdentifier(CurrentIssueArticlesTableCellIdentifier, forIndexPath: indexPath) as! CurrentIssueArticlesTableViewCell

        if let currentIssueArticleObject = currentIssueObjects.objectAtIndex(indexPath.row) as? IssueElement {

            let title = currentIssueArticleObject.title ?? ""

            let timeStampDateObject = NSDate(timeIntervalSince1970: NSTimeInterval(currentIssueArticleObject.timeStamp))
            let timeStampDateString = dateFormatter.stringFromDate(timeStampDateObject)

            let author = currentIssueArticleObject.author ?? ""

            let issueNumber = currentIssueArticleObject.issueNumber ?? ""
            let volumeNumber = currentIssueArticleObject.volumeNumber ?? ""

            let articleContent = currentIssueArticleObject.articleContent ?? ""

            let nodeID = currentIssueArticleObject.nodeID ?? 0


            cellWithoutCoverImage.currentIssueArticlesHeadlineLabel.font = UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline)
            cellWithoutCoverImage.currentIssueArticlesHeadlineLabel.text = title

            cellWithoutCoverImage.currentIssueArticlesAuthorLabel.font = UIFont.preferredFontForTextStyle(UIFontTextStyleSubheadline)
            cellWithoutCoverImage.currentIssueArticlesAuthorLabel.text = author

            cellWithoutCoverImage.currentIssueArticlesPublishDateLabel.font = UIFont.preferredFontForTextStyle(UIFontTextStyleSubheadline)
            cellWithoutCoverImage.currentIssueArticlesPublishDateLabel.text = timeStampDateString

            return cellWithoutCoverImage

        } else {

        }
    }

    else {

    }
}

EDIT 1: Revised code - still does not work 编辑1:修改的代码-仍然无法正常工作

override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell {

    let row = indexPath.row

    switch(row) {


    case 0:

        let cellWithCoverImage = tableView.dequeueReusableCellWithIdentifier(CurrentIssueFrontCoverTableCellIdentifier, forIndexPath: indexPath) as! CurrentIssueFrontCoverTableViewCell

        if let currentIssueFrontCoverObject = currentIssueObjects.objectAtIndex(indexPath.row) as? IssueElement {

            let title = currentIssueFrontCoverObject.title ?? ""

            let timeStampDateObject = NSDate(timeIntervalSince1970: NSTimeInterval(currentIssueFrontCoverObject.timeStamp))
            let timeStampDateString = dateFormatter.stringFromDate(timeStampDateObject)

            let issueNumber = currentIssueFrontCoverObject.issueNumber ?? ""
            let volumeNumber = currentIssueFrontCoverObject.volumeNumber ?? ""

            let nodeID = currentIssueFrontCoverObject.nodeID ?? 0

            let imageURL = currentIssueFrontCoverObject.imageURL ?? ""


            cellWithCoverImage.request?.cancel()

            if let coverImage = self.imageCache.objectForKey(imageURL) as? UIImage {
                cellWithCoverImage.currentIssueFrontCoverImageView.image = coverImage
            } else {
                cellWithCoverImage.currentIssueFrontCoverImageView.image = nil
                cellWithCoverImage.request = Alamofire.request(.GET, imageURL).responseImage() { response in
                    if let coverImage = response.result.value {
                        self.imageCache.setObject(response.result.value!, forKey: imageURL)
                        cellWithCoverImage.currentIssueFrontCoverImageView.image = coverImage

                    } else {

                        return

                    }
                }
            }
        } else {

            break
        }

        return cellWithCoverImage;

        // Populating data in the "Articles" type cells



    default:

        let cellWithoutCoverImage = tableView.dequeueReusableCellWithIdentifier(CurrentIssueArticlesTableCellIdentifier, forIndexPath: indexPath) as! CurrentIssueArticlesTableViewCell

        if let currentIssueArticleObject = currentIssueObjects.objectAtIndex(indexPath.row) as? IssueElement {

            let title = currentIssueArticleObject.title ?? ""

            let timeStampDateObject = NSDate(timeIntervalSince1970: NSTimeInterval(currentIssueArticleObject.timeStamp))
            let timeStampDateString = dateFormatter.stringFromDate(timeStampDateObject)

            let author = currentIssueArticleObject.author ?? ""

            let issueNumber = currentIssueArticleObject.issueNumber ?? ""
            let volumeNumber = currentIssueArticleObject.volumeNumber ?? ""

            let articleContent = currentIssueArticleObject.articleContent ?? ""

            let nodeID = currentIssueArticleObject.nodeID ?? 0


            cellWithoutCoverImage.currentIssueArticlesHeadlineLabel.font = UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline)
            cellWithoutCoverImage.currentIssueArticlesHeadlineLabel.text = title

            cellWithoutCoverImage.currentIssueArticlesAuthorLabel.font = UIFont.preferredFontForTextStyle(UIFontTextStyleSubheadline)
            cellWithoutCoverImage.currentIssueArticlesAuthorLabel.text = author

            cellWithoutCoverImage.currentIssueArticlesPublishDateLabel.font = UIFont.preferredFontForTextStyle(UIFontTextStyleSubheadline)
            cellWithoutCoverImage.currentIssueArticlesPublishDateLabel.text = timeStampDateString

            return cellWithoutCoverImage;

        } else {
            break
        }
    }

}

You need to remove the last else in your code cose it's empty. 您需要删除过去的else在你的代码COSE它是空的。 Also you could use a switch like: 您也可以使用类似以下的switch

switch(row){
    case 0:{
        // code with the CurrentIssueFrontCoverTableViewCell

        ....
        return cellWithCoverImage;
    }

    default:{

        // code with the CurrentIssueArticlesTableViewCell

        ...
        return cellWithoutCoverImage;
    }
}

The comment from Victor is correct. 维克多的评论是正确的。 Because if you were to step through every possible outcome of the function, you could possibly reach the end of it without returning anything. 因为如果您要逐步执行该函数的所有可能结果,则可能无需返回任何内容就可以到达函数的结尾。 You will need to provide a return statement for every possible outcome, including inside all of the 'else' blocks at the end that are currently empty. 您将需要为每种可能的结果提供一个return语句,包括在最后所有当前为空的'else'块内部。

Your problem is this: 您的问题是这样的:

} else {
    return
}

You can't have empty returns in a method expecting a return type, you need to return something here. 在期望返回类型的方法中,不能有空返回,您需要在此处返回一些内容。

Every code path in your method must end in a return that returns a UITableViewCell. 方法中的每个代码路径都必须以返回UITableViewCell的返回结尾。 Anything that does not will make the compiler complain. 任何不使编译器抱怨的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM