2016-06-17 14 views
0

私はSwiftプロジェクトに関する質問があります。 私は複数のセルを持つTableViewControllerを持っています。私がそれらをクリックすると、私はViewControllerに行き、そこでデータはtableViewControllerから渡されます。viewcontrollerからテーブルビューの次のセルに移動します

ViewControllerにボタンを実装して、ViewControllerをテーブルビューコントローラーの「次のセル」の内容で表示することができます。例えば、私はテーブルビューコントローラの2番目のセルをクリックしたので、そのセルに対応するデータを表示するViewControllerに行き、次にviewControllerの「次へ」ボタンをクリックすると、コンテンツを表示したいテーブルビューコントローラの3番目のセルのそれをどうやって清潔にするのですか?ここで

私は tableViewControllerから ViewControllerにデータを渡すために使用するコードです:

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    if let a = articles { 
     return a.count 
    } 
    return 0 
} 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->UITableViewCell{ 
    let cell = tableView.dequeueReusableCellWithIdentifier("ArticleCell", forIndexPath: indexPath) 

    let article = articles?[indexPath.row] 

    if let a = article { 
     cell.textLabel?.text = a.articleName 
     cell.textLabel?.font = UIFont(name: "Avenir", size: 18) 
     cell.textLabel?.numberOfLines = 0 
     if let i = a.tableViewImage { 
      cell.imageView?.image = UIImage(named: i) 
     } 

    } 

    return cell 
} 

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if segue.identifier == "ShowArticle" { 
     let articleVC = segue.destinationViewController as? ArticleViewController 

     guard let cell = sender as? UITableViewCell, 
      let indexPath = tableView.indexPathForCell(cell) else { 
       return 
     } 

     articleVC?.article = articles?[indexPath.row] 
    } 

    } 

viewControllerでは、右の記事を表示するには、私はviewDidLoad()(私のviewControllerでこれを書いたのウェブ図を示しています

if let a = article { 
     articleWebView.loadRequest(NSURLRequest(URL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(a.articleLink, ofType: "html")!))) 
    } 

私はnextButtonにリンクされている:記事、と私はarticleLinkが来るクラスArticle)を持っています3210:

@IBOutlet weak var nextButton: UIButton! 

私は(私はtableViewControllerに私のデータを宣言するために、私の主な問題であり、私は私が滞在する可能性がどのように表示されていないスウィフトに比較的新しいですし、それを行う方法を見当がつかないViewControllertableViewControllerからの「インポート」データ)。この

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if segue.identifier == "ShowArticle" { 
     let articleVC = segue.destinationViewController as? ArticleViewController 
     guard let cell = sender as? UITableViewCell, let indexPath = tableView.indexPathForCell(cell) else { 
      return 
     } 
     articleVC?.articles = articles 
     articleVC?.selectedIndex = indexPath.row 
     articleVC?.article = articles?[indexPath.row] 
    } 
} 

今すぐあなたの次のボタンを変更するにも、以下のようなあなたのViewControllerに二つのグローバル変数を追加よう

答えて

1
TableViewController's prepareForSegueのあなたのコードを変更

私はあなたを知らないこの

var articles: [article]! 
var selectedIndex: Int! 

//Now your button click 
@IBAction func nextButtonClick(sender: UIButton) { 
    if ((self.selectedIndex + 1) < self.articles.count) { 
     self.selectedIndex++ 
     let nextArticle = self.articles[self.selectedIndex] 
     articleWebView.loadRequest(NSURLRequest(URL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(nextArticle.articleLink, ofType: "html")!))) 
    } 
} 

のようにクリックしてくださいNSArrayまたはArrayを使用しているため、TableViewControllerで作成したのと同じ配列オブジェクトarticlesを作成してください。

希望すると、これが役立ちます。

+0

あなたは命の恩人です、ありがとう! それは魅力的に機能し、私はちょうどa.icleLinkをnextButtonClickのあなたの答えのnextArticle.articleLinkに変更しました(将来誰かがこれを必要とする場合)。 助けてくれてありがとう: – EdouM

+0

私はupvotedと答え、再びありがとう! – EdouM

関連する問題