在if-else

时间:2016-04-21 10:38:13

标签: ios swift uitableview

当我将单元格返回到if-else之外时遇到问题,它会给我一个错误“使用未解析的标识符'单元格'”。这是我的代码:

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

5 个答案:

答案 0 :(得分:2)

使用

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
}

编辑&amp;更新

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

答案 1 :(得分:1)

单元格必须是范围内的全局变量。

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

答案 2 :(得分:0)

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
     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!
}

答案 3 :(得分:0)

写下面的代码行。你的问题将得到解决。

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

答案 4 :(得分:0)

cell范围内声明的if变量。在此范围之外不可见。您有两种选择如何解决它:

  1. 在同一范围内返回cell

    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
        }
    }
    
  2. if范围

    之前声明变量
    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