Ios 如果有,返回单元格外部

Ios 如果有,返回单元格外部,ios,swift,uitableview,Ios,Swift,Uitableview,当我在if-else之外返回单元格时,遇到了一个问题,它给出了一个错误“使用未解析的标识符‘cell’”。这是我的密码: func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { if let feedItem = feeds.objectAtIndex(UInt(indexPath.row)) as? FeedsDataMod

当我在if-else之外返回单元格时,遇到了一个问题,它给出了一个错误“使用未解析的标识符‘cell’”。这是我的密码:

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

    if let feedItem = feeds.objectAtIndex(UInt(indexPath.row)) as? FeedsDataModel {
        if feedItem.media_url != "" {
            let cell = tableView.dequeueReusableCellWithIdentifier("FeedsTableWithImgCell", forIndexPath: indexPath) as! FeedsTableViewWithImageCell
            cell.setupCellWithFeedItem()
        }
        else {
            let cell = tableView.dequeueReusableCellWithIdentifier("FeedsTableWithoutImgCell", forIndexPath: indexPath) as! FeedsTableViewWithoutImageCell
            cell.setupCellWithFeedItem()
        }

        return cell
    }
}

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

if let feedItem = feeds.objectAtIndex(UInt(indexPath.row)) as? FeedsDataModel {
    if feedItem.media_url != "" {
        let cell = tableView.dequeueReusableCellWithIdentifier("FeedsTableWithImgCell", forIndexPath: indexPath) as! FeedsTableViewWithImageCell
        cell.setupCellWithFeedItem()
        return cell
    }
    else {
        let cell = tableView.dequeueReusableCellWithIdentifier("FeedsTableWithoutImgCell", forIndexPath: indexPath) as! FeedsTableViewWithoutImageCell
        cell.setupCellWithFeedItem()
        return cell
    }


}
}
更新

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

  let cell: FeedsTableViewWithImageCell

if let feedItem = feeds.objectAtIndex(UInt(indexPath.row)) as? FeedsDataModel {
    if feedItem.media_url != "" {
         cell = tableView.dequeueReusableCellWithIdentifier("FeedsTableWithImgCell", forIndexPath: indexPath) as! FeedsTableViewWithImageCell
        cell.setupCellWithFeedItem()

    }
    else {
         cell = tableView.dequeueReusableCellWithIdentifier("FeedsTableWithoutImgCell", forIndexPath: indexPath) as! FeedsTableViewWithoutImageCell
        cell.setupCellWithFeedItem()

    }


}
     return cell
}
   func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

if let feedItem = feeds.objectAtIndex(UInt(indexPath.row)) as? FeedsDataModel {
    var cell:FeedsTableViewWithImageCell!
    if feedItem.media_url != "" {
        cell = tableView.dequeueReusableCellWithIdentifier("FeedsTableWithImgCell", forIndexPath: indexPath) as! FeedsTableViewWithImageCell
        cell.setupCellWithFeedItem()
    }
    else {
       let cell1 = tableView.dequeueReusableCellWithIdentifier("FeedsTableWithoutImgCell", forIndexPath: indexPath) as! FeedsTableViewWithoutImageCell
        cell1.setupCellWithFeedItem()
         return cell1
    }

    return cell
}
}
编辑和更新

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

  let cell: FeedsTableViewWithImageCell

if let feedItem = feeds.objectAtIndex(UInt(indexPath.row)) as? FeedsDataModel {
    if feedItem.media_url != "" {
         cell = tableView.dequeueReusableCellWithIdentifier("FeedsTableWithImgCell", forIndexPath: indexPath) as! FeedsTableViewWithImageCell
        cell.setupCellWithFeedItem()

    }
    else {
         cell = tableView.dequeueReusableCellWithIdentifier("FeedsTableWithoutImgCell", forIndexPath: indexPath) as! FeedsTableViewWithoutImageCell
        cell.setupCellWithFeedItem()

    }


}
     return cell
}
   func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

if let feedItem = feeds.objectAtIndex(UInt(indexPath.row)) as? FeedsDataModel {
    var cell:FeedsTableViewWithImageCell!
    if feedItem.media_url != "" {
        cell = tableView.dequeueReusableCellWithIdentifier("FeedsTableWithImgCell", forIndexPath: indexPath) as! FeedsTableViewWithImageCell
        cell.setupCellWithFeedItem()
    }
    else {
       let cell1 = tableView.dequeueReusableCellWithIdentifier("FeedsTableWithoutImgCell", forIndexPath: indexPath) as! FeedsTableViewWithoutImageCell
        cell1.setupCellWithFeedItem()
         return cell1
    }

    return cell
}
}

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

if let feedItem = feeds.objectAtIndex(UInt(indexPath.row)) as? FeedsDataModel {
    if feedItem.media_url != "" {
        let cell = tableView.dequeueReusableCellWithIdentifier("FeedsTableWithImgCell", forIndexPath: indexPath) as! FeedsTableViewWithImageCell
        cell.setupCellWithFeedItem()
        return cell
    }
    else {
        let cell = tableView.dequeueReusableCellWithIdentifier("FeedsTableWithoutImgCell", forIndexPath: indexPath) as! FeedsTableViewWithoutImageCell
        cell.setupCellWithFeedItem()
        return cell
    }


}
}
更新

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

  let cell: FeedsTableViewWithImageCell

if let feedItem = feeds.objectAtIndex(UInt(indexPath.row)) as? FeedsDataModel {
    if feedItem.media_url != "" {
         cell = tableView.dequeueReusableCellWithIdentifier("FeedsTableWithImgCell", forIndexPath: indexPath) as! FeedsTableViewWithImageCell
        cell.setupCellWithFeedItem()

    }
    else {
         cell = tableView.dequeueReusableCellWithIdentifier("FeedsTableWithoutImgCell", forIndexPath: indexPath) as! FeedsTableViewWithoutImageCell
        cell.setupCellWithFeedItem()

    }


}
     return cell
}
   func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

if let feedItem = feeds.objectAtIndex(UInt(indexPath.row)) as? FeedsDataModel {
    var cell:FeedsTableViewWithImageCell!
    if feedItem.media_url != "" {
        cell = tableView.dequeueReusableCellWithIdentifier("FeedsTableWithImgCell", forIndexPath: indexPath) as! FeedsTableViewWithImageCell
        cell.setupCellWithFeedItem()
    }
    else {
       let cell1 = tableView.dequeueReusableCellWithIdentifier("FeedsTableWithoutImgCell", forIndexPath: indexPath) as! FeedsTableViewWithoutImageCell
        cell1.setupCellWithFeedItem()
         return cell1
    }

    return cell
}
}
编辑和更新

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

  let cell: FeedsTableViewWithImageCell

if let feedItem = feeds.objectAtIndex(UInt(indexPath.row)) as? FeedsDataModel {
    if feedItem.media_url != "" {
         cell = tableView.dequeueReusableCellWithIdentifier("FeedsTableWithImgCell", forIndexPath: indexPath) as! FeedsTableViewWithImageCell
        cell.setupCellWithFeedItem()

    }
    else {
         cell = tableView.dequeueReusableCellWithIdentifier("FeedsTableWithoutImgCell", forIndexPath: indexPath) as! FeedsTableViewWithoutImageCell
        cell.setupCellWithFeedItem()

    }


}
     return cell
}
   func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

if let feedItem = feeds.objectAtIndex(UInt(indexPath.row)) as? FeedsDataModel {
    var cell:FeedsTableViewWithImageCell!
    if feedItem.media_url != "" {
        cell = tableView.dequeueReusableCellWithIdentifier("FeedsTableWithImgCell", forIndexPath: indexPath) as! FeedsTableViewWithImageCell
        cell.setupCellWithFeedItem()
    }
    else {
       let cell1 = tableView.dequeueReusableCellWithIdentifier("FeedsTableWithoutImgCell", forIndexPath: indexPath) as! FeedsTableViewWithoutImageCell
        cell1.setupCellWithFeedItem()
         return cell1
    }

    return cell
}
}

单元格必须是作用域中的全局变量

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

    if let feedItem = feeds.objectAtIndex(UInt(indexPath.row)) as? FeedsDataModel {
        var cell:FeedsTableViewWithImageCell!
        if feedItem.media_url != "" {
            cell = tableView.dequeueReusableCellWithIdentifier("FeedsTableWithImgCell", forIndexPath: indexPath) as! FeedsTableViewWithImageCell
            cell.setupCellWithFeedItem()
        }
        else {
            cell = tableView.dequeueReusableCellWithIdentifier("FeedsTableWithoutImgCell", forIndexPath: indexPath) as! FeedsTableViewWithoutImageCell
            cell.setupCellWithFeedItem()
        }

        return cell
    }
}

单元格必须是作用域中的全局变量

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

    if let feedItem = feeds.objectAtIndex(UInt(indexPath.row)) as? FeedsDataModel {
        var cell:FeedsTableViewWithImageCell!
        if feedItem.media_url != "" {
            cell = tableView.dequeueReusableCellWithIdentifier("FeedsTableWithImgCell", forIndexPath: indexPath) as! FeedsTableViewWithImageCell
            cell.setupCellWithFeedItem()
        }
        else {
            cell = tableView.dequeueReusableCellWithIdentifier("FeedsTableWithoutImgCell", forIndexPath: indexPath) as! FeedsTableViewWithoutImageCell
            cell.setupCellWithFeedItem()
        }

        return cell
    }
}

写在下面的代码行。你的问题会解决的

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell: UITableViewCell?
    if let feedItem = feeds.objectAtIndex(UInt(indexPath.row)) as? FeedsDataModel {
        if feedItem.media_url != "" {
            cell = tableView.dequeueReusableCellWithIdentifier("FeedsTableWithImgCell", forIndexPath: indexPath) as! FeedsTableViewWithImageCell
            cell.setupCellWithFeedItem()
        }
        else {
            cell = tableView.dequeueReusableCellWithIdentifier("FeedsTableWithoutImgCell", forIndexPath: indexPath) as! FeedsTableViewWithoutImageCell
            cell.setupCellWithFeedItem()
        }
    }

    return cell!
}

写在下面的代码行。你的问题会解决的

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell: UITableViewCell?
    if let feedItem = feeds.objectAtIndex(UInt(indexPath.row)) as? FeedsDataModel {
        if feedItem.media_url != "" {
            cell = tableView.dequeueReusableCellWithIdentifier("FeedsTableWithImgCell", forIndexPath: indexPath) as! FeedsTableViewWithImageCell
            cell.setupCellWithFeedItem()
        }
        else {
            cell = tableView.dequeueReusableCellWithIdentifier("FeedsTableWithoutImgCell", forIndexPath: indexPath) as! FeedsTableViewWithoutImageCell
            cell.setupCellWithFeedItem()
        }
    }

    return cell!
}

您的
单元格
变量在
if
范围中声明。在这个范围之外它是不可见的。您有两种方法来修复它:

  • 返回同一范围内的
    单元格

    if let feedItem = feeds.objectAtIndex(UInt(indexPath.row)) as? FeedsDataModel {
        if feedItem.media_url != "" {
            let cell = tableView.dequeueReusableCellWithIdentifier("FeedsTableWithImgCell", forIndexPath: indexPath) as! FeedsTableViewWithImageCell
            cell.setupCellWithFeedItem()
            return cell
        }
        else {
            let cell = tableView.dequeueReusableCellWithIdentifier("FeedsTableWithoutImgCell", forIndexPath: indexPath) as! FeedsTableViewWithoutImageCell
            cell.setupCellWithFeedItem()
            return cell
        }
    }
    
  • 范围之前声明变量

    var cell: FeedsTableViewWithImageCell?
    if let feedItem = feeds.objectAtIndex(UInt(indexPath.row)) as? FeedsDataModel {
        if feedItem.media_url != "" {
            cell = tableView.dequeueReusableCellWithIdentifier("FeedsTableWithImgCell", forIndexPath: indexPath) as! FeedsTableViewWithImageCell
            cell.setupCellWithFeedItem()
        }
        else {
            cell = tableView.dequeueReusableCellWithIdentifier("FeedsTableWithoutImgCell", forIndexPath: indexPath) as! FeedsTableViewWithoutImageCell
            cell.setupCellWithFeedItem()
        }
    }
    return cell
    

  • 您的
    单元格
    变量在
    if
    范围中声明。在这个范围之外它是不可见的。您有两种方法来修复它:

  • 返回同一范围内的
    单元格

    if let feedItem = feeds.objectAtIndex(UInt(indexPath.row)) as? FeedsDataModel {
        if feedItem.media_url != "" {
            let cell = tableView.dequeueReusableCellWithIdentifier("FeedsTableWithImgCell", forIndexPath: indexPath) as! FeedsTableViewWithImageCell
            cell.setupCellWithFeedItem()
            return cell
        }
        else {
            let cell = tableView.dequeueReusableCellWithIdentifier("FeedsTableWithoutImgCell", forIndexPath: indexPath) as! FeedsTableViewWithoutImageCell
            cell.setupCellWithFeedItem()
            return cell
        }
    }
    
  • 范围之前声明变量

    var cell: FeedsTableViewWithImageCell?
    if let feedItem = feeds.objectAtIndex(UInt(indexPath.row)) as? FeedsDataModel {
        if feedItem.media_url != "" {
            cell = tableView.dequeueReusableCellWithIdentifier("FeedsTableWithImgCell", forIndexPath: indexPath) as! FeedsTableViewWithImageCell
            cell.setupCellWithFeedItem()
        }
        else {
            cell = tableView.dequeueReusableCellWithIdentifier("FeedsTableWithoutImgCell", forIndexPath: indexPath) as! FeedsTableViewWithoutImageCell
            cell.setupCellWithFeedItem()
        }
    }
    return cell
    

  • 那不行。还有一个不带ImageCell的
    FeedStableView
    类型,并且该单元格显然是非可选的。将我的答案更改为“展开”变量,它应该可以工作,但不起作用。还有一个
    FeedsTableViewWithoutImageCell
    类型,该单元格显然是非可选的。将我的答案更改为unwrap变量,它应该可以工作。这会给我一个“缺少返回函数”的错误,否则会给我一个“无法将类型为”FeedsTableWithoutImageCell“的值分配给类型”的错误FeedsTableWithImageCell“是的!这就是我要找的!这给了我一个“缺少返回函数”的错误,在其他条件下,它给了我一个“无法为FeedsTableWithImageCell”类型赋值”的错误是的!这就是我要找的是
    提要
    真的是可选的还是未知的类型?如果表视图是在Interface Builder中设计的,那么数据源数组应该是一种特定类型的非可选对象,并且根本不需要可选绑定。
    feeds
    真的是可选类型还是未知类型?如果表视图是在Interface Builder中设计的,那么数据源数组应该是一个特定类型的非可选对象,并且根本不需要可选绑定。