【发布时间】:2016-06-17 11:57:43
【问题描述】:
我有一个关于 Swift 项目的问题。
我有一个带有多个单元格的TableViewController,当我点击它们时,我会转到ViewController,其中数据是从tableViewController 传递的。
我想在ViewController 中实现一个按钮,它允许我显示ViewController 以及表格视图控制器中“下一个单元格”的内容。例如,我点击了表格视图控制器的第二个单元格,所以我转到显示与该单元格对应的数据的ViewController,然后当我点击viewController 中的“下一步”按钮时,我想要显示表格视图控制器的第三个单元格的内容。我该如何以干净的方式做到这一点?
这是我用来将数据从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 显示了一篇文章的网络视图,我有一个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“导入”数据。
【问题讨论】:
标签: ios swift uitableview uiviewcontroller swift2