Tôi đang học nhanh chóng và tôi đang gặp sự cố khi cố gắng phân tích khi một TableViewCell được chạm vào, được cho là chuyển url tới chế độ xem thứ hai cho thời điểm này chỉ hiển thị nó trong một nhãn. i Tự động tạo (tôi đã thấy mọi người sử dụng Lập trình, có lẽ là từ đúng) mỗi ô duy nhất, vì vậy, trong bảng phân cảnh tôi không có đối tượng nào để liên kết đến chế độ xem khác ngoại trừ chính chế độ xem ... và đó là tôi đã làm gì. Vì vậy, tôi đã kết nối bộ điều khiển xem đầu tiên với bộ điều khiển thứ hai và thêm mã để thực hiện phân đoạn.Swift - Segue on Dynamic TableCell
Tôi không chắc liệu điều đó có đúng hay không, kiến thức của tôi đến từ các hướng dẫn không giải thích chính xác những gì tôi muốn làm.
ở dưới đó có mã của hai chế độ xem.
đầu tiên xem
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)
}
điểm thứ hai
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
}
}
}
Cảm ơn.
Làm cách nào để truy xuất thuộc tính dữ liệu của bộ điều khiển chế độ xem thứ hai từ bộ điều khiển đầu tiên? bộ điều khiển xem đầu tiên không biết thuộc tính nào mà trình điều khiển chế độ xem thứ hai có. – DeepVinicius
Bạn cần phải cast segue.destinationViewController vào bộ điều khiển SecondView. Tôi đã thực hiện các chỉnh sửa cần thiết cho câu trả lời của mình –