当我将单元格返回到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
}
}
答案 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
变量。在此范围之外不可见。您有两种选择如何解决它:
在同一范围内返回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
}
}
在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