Reputation: 33
I have a question regarding a Swift project.
I have a TableViewController
with multiple cells, and when I click on them I go to a ViewController
where data is passed from the tableViewController
.
I want to implement a button in the ViewController
that allows me to display the ViewController
with the content of the "next cell" from the table view controller. For instance, I clicked on the 2nd cell of the table view controller, so I go to the ViewController
that displays the data corresponding to that cell, and then when I click on that "next" button in the viewController
I want to display the content of the 3rd cell of the table view controller. How do I do that in a clean way?
Here is the code I use to pass the data from tableViewController
to 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]
}
}
In the viewController
, to show the right article, I wrote this in the viewDidLoad()
(my viewController
shows a web view of an article, and I have a class Article
where articleLink
comes from):
if let a = article {
articleWebView.loadRequest(NSURLRequest(URL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(a.articleLink, ofType: "html")!)))
}
I have linked a nextButton
in Main.Storyboard
:
@IBOutlet weak var nextButton: UIButton!
I'm relatively new to Swift, and have no idea how to do it (my main problem is because I declare my data in the tableViewController
, and I don't see how I could stay in the ViewController
and "import" data from the tableViewController
).
Upvotes: 1
Views: 960
Reputation: 72410
Change your code of TableViewController's
prepareForSegue
like this
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]
}
}
Now add two global variable in your ViewController
like below also change your next button click like this
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")!)))
}
}
I don't know you are using NSArray
or Array
so create the articles
array object same as you created in TableViewController
.
Hope this will help you.
Upvotes: 3