2016-05-29 1 views
0

ここには、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() 
} 
+0

'cellForRowAtIndexPath'は、配列を構築するための場所ではありません;この関数が呼び出される順序は保証されていません。与えられた 'indexPath'を使用して、あなたが操作している行を特定する必要があります – Paulw11

+0

@ Paulw11この状況で何を助言できますか? –

+0

私は、各構造体がテキストまたはイメージを保持し、 'cellForRowAtIndexPath'でこれを使うことができる構造体の配列を一つ持っていることをお勧めします。 – Paulw11

答えて

0

あなたのtypeArray構造体のインスタンスを保持するために、あなたのcontent配列を宣言する必要があります。

var content = [typeArray]() 

そして、配列に構造体のインスタンスを追加します。

let obj = typeArray(image: image) 
content.append(obj) 
self.articleTableView.reloadData() 

次に、あなたがあなたのcellForRowAtIndexPathでこれを使用することができます -

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

    let rowStruct = content[indexPath.row] { 
    if let text = rowStruct.text { 
     let cell = self.articleTableView.dequeueReusableCellWithIdentifier("Text Cell", forIndexPath: indexPath) as! TextTableViewCell 
     cell.textArticle.text = text 
     return cell 
    } else if let image = rowStruct.image { 
     let cell = self.articleTableView.dequeueReusableCellWithIdentifier("Image Cell", forIndexPath: indexPath) as! ImageTableViewCell 
     cell.backgroundColor = UIColor.clearColor() 
     cell.imageArticle.image = image 
     return cell 
    } 
    return UITableViewCell() 
} 
+0

は、rowStruct = content [indexPath.row]、 "非関数型vc.typeArrayの値を呼び出すことはできません" –

+0

私はしましたが、テキスト=コンテンツ[indexPath.row] fine –

+0

配列にイメージではなく構造体のインスタンスを追加する必要があります。私の編集を見る – Paulw11

関連する問題