在预期返回的函数中缺少返回' UITableViewCell'

时间:2015-10-16 16:20:23

标签: ios swift uitableview

编辑1:修改后的代码 - 仍无效。

我有两个自定义单元格。

第一个单元格(仅第一个单元格)的类型为 CurrentIssueFrontCoverTableViewCell ,其余单元格的类型为 CurrentIssueArticlesTableViewCell 。在声明我的cellForRowAtIndexPath函数时,我收到标题中描述的错误。有什么想解决这个问题吗?为什么我没有发现我在我的"中返回了这个单元格?如果"循环?

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 {

    }
}

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

}

3 个答案:

答案 0 :(得分:0)

您需要删除代码中的最后一个else,因为它是空的。您也可以使用switch之类的:

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

        ....
        return cellWithCoverImage;
    }

    default:{

        // code with the CurrentIssueArticlesTableViewCell

        ...
        return cellWithoutCoverImage;
    }
}

答案 1 :(得分:0)

Victor的评论是正确的。因为如果你要逐步完成函数的每个可能的结果,你可能会在没有返回任何内容的情况下达到它的结尾。您需要为每个可能的结果提供一个return语句,包括在当前为空的所有“else”块中。

答案 2 :(得分:0)

你的问题是:

} else {
    return
}

在期望返回类型的方法中不能有空返回,你需要在这里返回一些东西。

方法中的每个代码路径必须以返回UITableViewCell的返回结束。任何不会使编译器抱怨的东西。