具有包含2種類型的結構 - 圖像和文本。有一個數組,它將被添加。如何在cellForRowAtIndexPath中進行類型檢查?結構檢查類型| Swift
struct typeArray {
var text: String?
var image: UIImage?
init(text: String){
self.text = text
}
init(image: UIImage){
self.image = image
}
}
var content = [AnyObject]()
圖像添加按鈕:
let obj = typeArray(image: image)
content.append(obj.image!)
self.articleTableView.reloadData()
文本添加按鈕:
let obj = typeArray(text: self.articleTextView.text as String!)
self.content.append(obj.text!)
self.articleTableView.reloadData()
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
if content[indexPath.row] == {
let cell = self.articleTableView.dequeueReusableCellWithIdentifier("Text Cell", forIndexPath: indexPath) as! TextTableViewCell
cell.textArticle.text = content[indexPath.row] as? String
return cell
}
else if content[indexPath.row] == {
let cell = self.articleTableView.dequeueReusableCellWithIdentifier("Image Cell", forIndexPath: indexPath) as! ImageTableViewCell
cell.backgroundColor = UIColor.clearColor()
cell.imageArticle.image = content[indexPath.row] as? UIImage
return cell
}
return UITableViewCell()
}
'cellForRowAtIndexPath'是不是要建立陣列的地方;這個函數將被調用的順序不能保證;你需要使用提供的'indexPath'來確定你正在操作哪一行 – Paulw11
@ Paulw11在這種情況下你有什麼建議? –
我建議你有一個單一的結構數組,其中每個結構體可以保存文本或圖像,然後在'cellForRowAtIndexPath'中使用它。 – Paulw11