gpt4 book ai didi

ios - Swift - 在动态 TableCell 上继续

转载 作者:搜寻专家 更新时间:2023-10-31 19:35:28 26 4
gpt4 key购买 nike

我正在快速学习,并且在尝试在触摸 TableViewCell 时触发 segue 时遇到问题,这应该将 url 传递给第二个 View ,目前只是在标签中显示它。我动态创建(我见过人们以编程方式使用,这可能是正确的词)每个单元格,所以,在 Storyboard 中,除了 View 本身之外,我没有任何对象可以链接到另一个 View ......这就是我做到了。所以我将第一个 View Controller 连接到第二个 View Controller ,并添加了执行转场的代码。

我不确定这是否正确,我的知识来自教程,但并未完全解释我想做什么。

下面是两个 View 的代码。

第一眼

    import UIKit

protocol sendInfoDelegate{

func userDidEnterInfo( WhichInfo info : String)

}

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

var tableData = []
@IBOutlet weak var redditListTableView: UITableView!
var selectedCellURL : String?
var delegate : sendInfoDelegate? = nil



override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

// change the link to change the json source

getRedditJSON("http://www.reddit.com/.json")
}

//
//Creates a connection via a task (networktask) then parses the json
//
func getRedditJSON(whichReddit : String){
let mySession = NSURLSession.sharedSession()
let url: NSURL = NSURL(string: whichReddit)
let networkTask = mySession.dataTaskWithURL(url, completionHandler : {data, response, error -> Void in
var err: NSError?
var theJSON = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as NSMutableDictionary
let results : NSArray = theJSON["data"]!["children"] as NSArray
dispatch_async(dispatch_get_main_queue(), {
self.tableData = results
self.redditListTableView.reloadData()
})
})
networkTask.resume()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

//needs to be implemented

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableData.count
}

//creates the whole table

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "MyTestCell")
let redditEntry : NSMutableDictionary = self.tableData[indexPath.row] as NSMutableDictionary
cell.textLabel?.text = redditEntry["data"]!["title"] as? String
cell.detailTextLabel?.text = redditEntry["data"]!["author"] as? String
return cell
}

// action to be taken when a cell is selected
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let redditEntry : NSMutableDictionary = self.tableData[indexPath.row] as NSMutableDictionary
self.selectedCellURL = redditEntry["data"]!["url"] as? String
self.performSegueWithIdentifier("passInfo" , sender: indexPath)
println(self.selectedCellURL!)

if delegate != nil {
let information:String = self.selectedCellURL!
println("ciao")
delegate?.userDidEnterInfo(WhichInfo: information)
self.navigationController?.popViewControllerAnimated(true)

}

第二个观点

import UIKit




class WebPageController : UIViewController, sendInfoDelegate {


var infoFromSVC: String?

@IBOutlet weak var labelVC: UILabel!


func userDidEnterInfo(WhichInfo info: String) {
self.infoFromSVC = info
labelVC.text = infoFromSVC

}

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "passInfo"{
let firstVController : ViewController = segue.destinationViewController as ViewController
firstVController.delegate = self

}
}
}

谢谢。

最佳答案

要将任何数据传递给第二个 View Controller ,您需要在第一个 View Controller 中实现 prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) 方法,并在此处将任何数据传递给第二个 View Controller segue.destinationViewController 对象。

例如

// this method must be in first view controller
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "passInfo" {
var secondViewController : SecondViewController = segue.destinationViewController as SecondViewController

var indexPath = self.tableview.indexPathForSelectedRow() //get index of data for selected row

secondViewController.data = self.dataArray.objectAtIndex(indexPath.row) // get data by index and pass it to second view controller

}
}

在第二个 View Controller 中获取数据的代码

override func viewDidLoad() {
super.viewDidLoad()

self.label.text = self.data
}

数据变量必须定义为第二个 View Controller 的属性。

关于ios - Swift - 在动态 TableCell 上继续,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26303616/

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