使用像
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
}
}
是'feeds'真是一個可選的變量聲明或未知類型?如果在Interface Builder中設計表視圖,則數據源數組應該是一個特定類型的非可選對象,並且根本不需要可選綁定。 – vadian