gpt4 book ai didi

ios - 从 View Controller 转到 TableView 的下一个单元格

转载 作者:行者123 更新时间:2023-11-28 12:46:36 24 4
gpt4 key购买 nike

我有一个关于 Swift 项目的问题。我有一个包含多个单元格的 TableViewController,当我点击它们时,我会转到一个 ViewController,其中数据从 tableViewController 传递。

我想在 ViewController 中实现一个按钮,它允许我显示带有来自 TableView Controller 的“下一个单元格”内容的 ViewController。例如,我单击了 TableView Controller 的第二个单元格,所以我转到显示与该单元格对应的数据的 ViewController,然后当我单击viewController 我想显示表格 View Controller 第三个单元格的内容。我如何以干净的方式做到这一点?

这是我用来将数据从 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 显示了一个网页 View 一篇文章,我有一个 Article 类,其中 articleLink 来自):

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

我在 Main.Storyboard 中链接了一个 nextButton:

@IBOutlet weak var nextButton: UIButton!

我是 Swift 的新手,不知道该怎么做(我的主要问题是因为我在 tableViewController 中声明了我的数据,但我不知道如何留下来在 ViewController 中并从 tableViewController“导入”数据。

最佳答案

像这样更改 TableViewController 的 prepareForSegue 的代码

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 中添加两个全局变量,如下所示,也像这样更改你的下一个按钮点击

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")!)))
}
}

我不知道你正在使用 NSArrayArray 所以创建 articles 数组对象与你在 TableViewController 中创建的相同

希望对您有所帮助。

关于ios - 从 View Controller 转到 TableView 的下一个单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37881002/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com