私は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
に私のデータを宣言するために、私の主な問題であり、私は私が滞在する可能性がどのように表示されていないスウィフトに比較的新しいですし、それを行う方法を見当がつかないViewController
とtableViewController
からの「インポート」データ)。この
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
に二つのグローバル変数を追加よう
あなたは命の恩人です、ありがとう! それは魅力的に機能し、私はちょうどa.icleLinkをnextButtonClickのあなたの答えのnextArticle.articleLinkに変更しました(将来誰かがこれを必要とする場合)。 助けてくれてありがとう: – EdouM
私はupvotedと答え、再びありがとう! – EdouM