2016-04-19 21 views
0

UIButtonのタイトルテキストを変更しようとしていますが、機能しません。私はUITableViewにそれを持っています。ボタンのタイトルのテキストを変更することができません

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

     let imageCell = tableView.dequeueReusableCellWithIdentifier("imageCell", forIndexPath: indexPath) as! StoryCell 
     let videoCell = tableView.dequeueReusableCellWithIdentifier("videoCell", forIndexPath: indexPath) as! StoryCell 
     let blankCell = tableView.dequeueReusableCellWithIdentifier("blankCell", forIndexPath: indexPath) as! StoryCell 

     if (userFile[indexPath.item].url!.rangeOfString(".jpeg") != nil) { 
      print("imageCell: \(userFile[indexPath.row].url)") 

      imageCell.viewstoryButton.setTitle("Vis bildeeee - \(self.createdAt[indexPath.row].timeAgo)", forState: UIControlState.Normal) 
      imageCell.viewstoryButton.titleLabel?.textColor = UIColor.redColor() 

      return imageCell 
     } 
     if (userFile[indexPath.row].url!.rangeOfString(".mp4") != nil) { 
      print("videoCell: \(userFile[indexPath.row].url)") 

      videoCell.viewstoryButton.setTitle("Vis video - \(self.createdAt[indexPath.row].timeAgo)", forState: UIControlState.Normal) 

      return videoCell 
     } 
     else{ 
      print("blankCell") 
      return blankCell 
     } 

    } 

そして、私は3個のプロトタイプ細胞があります:imageCellvideoCellblankCellをこれは、それがどのように見えるかです。

これらはすべてStoryCellに接続されていますが、ボタンは表示されますが、テキストまたは色は変更されません。何か案は?

+0

これらのボタンは、セルに表示されているというカスタムセルライブラリに接続されている場合も、アプリケーションを実行するときに、確認することができます? ifブロック呼び出しかどうかを確認してください。 – Lion

+1

ブレークポイントを設定して、それらの行が呼び出されているかどうかを確認します。また、あなたのコードの流れに合わせてreloadDataが必要かもしれません。 –

+0

@Lionはい、表示されます。そして、ifブロックが呼び出すのは、imageCellとvideoCellの両方から出力されるためです。しかし、何か変です。 Parse here:http://i63.tinypic.com/1zd7lzc.jpgには、2つの画像と1つのビデオが含まれていますが、アプリケーションを実行すると、2つのテーブルビューのビデオセルと1つのビデオが表示されます画像のセル..任意の手がかり? –

答えて

0

正しい構文は次のとおりです。公式apple docに説明し、あなたのボタンはまた、アウトレットとしてごスウィフトファイル

+0

ボタンは、セルの別のCocoaクラスに追加されました。Story Cell.Swiftのボタンのための「StoryCell.swift」 –

+0

ボタン用のアウトレットを追加してください。 とcellForRowAtIndexPathには変更タイトルが実装されています –

0

ように接続されていることを確認します

のsetTitle(_タイトル:文字列?, forState状態:UIControlState)

そしてtitleLabel docのため言う:

タイトルラベル:UILabel? {get}

あなたの構文コードは正しいです。

reloadData()が呼び出された場合、あなたIBOutletボタンが正しく「StoryCell」

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

     var imageCell = tableView.dequeueReusableCellWithIdentifier("imageCell", forIndexPath: indexPath) as! StoryCell 
     if imageCell == nil { 
      imageCell = StoryCell(style: UITableViewCellStyle.Default, reuseIdentifier: ("imageCell")) 
     } 

     var videoCell = tableView.dequeueReusableCellWithIdentifier("videoCell", forIndexPath: indexPath) as! StoryCell 
     if videoCell == nil { 
      videoCell = StoryCell(style: UITableViewCellStyle.Default, reuseIdentifier: ("videoCell")) 
     } 

     var blankCell = tableView.dequeueReusableCellWithIdentifier("blankCell", forIndexPath: indexPath) as! StoryCell 
     if blankCell == nil { 
      blankCell = StoryCell(style: UITableViewCellStyle.Default, reuseIdentifier: ("blankCell")) 
     }   

     if (userFile[indexPath.item].url!.rangeOfString(".jpeg") != nil) { 
      print("imageCell: \(userFile[indexPath.row].url)") 

      imageCell.viewstoryButton.setTitle("Vis bildeeee - \(self.createdAt[indexPath.row].timeAgo)", forState: UIControlState.Normal) 
      imageCell.viewstoryButton.titleLabel?.textColor = UIColor.redColor() 

      return imageCell 
     } 
     if (userFile[indexPath.row].url!.rangeOfString(".mp4") != nil) { 
      print("videoCell: \(userFile[indexPath.row].url)") 

      videoCell.viewstoryButton.setTitle("Vis video - \(self.createdAt[indexPath.row].timeAgo)", forState: UIControlState.Normal) 

      return videoCell 
     } 
     else{ 
      print("blankCell") 
      return blankCell 
     } 

    } 
関連する問題