使用 swift 2 在 tableview 上处理单元格
working with cell on tableview using swift 2
我正在使用 Swift 开发应用程序 2. 在我的应用程序中,我正在将 JSON 数据解析为 table 视图。当我点击一个单元格时,它成功移动到另一个视图控制器,但我无法获取 json 数据。
这是我的视图控制器中的代码:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath){
var cell = self.TableView.dequeueReusableCellWithIdentifier("Cell",forIndexPath: indexPath) as! CustomTableCell
let strbookdetail : NSString=arrDict[indexPath.row] .valueForKey("booking_detail") as! NSString
let strdrop : NSString=arrDict[indexPath.row] .valueForKey("drop_address") as! NSString
cell.Bookdetail.text=strbookdetail as String
cell.Dropaddress.text=strdrop as String
return cell as CustomTableCell
}
//It helps to parse JSON data to table view.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
TableView.deselectRowAtIndexPath(indexPath, animated: true)
let tripcontroller = self.storyboard?.instantiateViewControllerWithIdentifier("UpcomingController") as! Secondcontroller
navigationController?.pushViewController(tripcontroller, animated: true)
}
点击单元格有助于移动到其他视图控制器SecondController
在我的 SecondController
中,我有两个 UILabel
。在那些标签中,我想显示数据,即 strbookdetail
和 strdrop
如何在两个视图控制器之间传递引用?
使用property
传递数据很容易做到:
第 2 vc
:
var dataFromBeforeVC:[String:String] = [String:String]()
override func viewDidLoad() {
super.viewDidLoad()
initData ()
}
func initData() {
// set data strbookdetail and strdrop to labelOne & labelTwo
labelOne.text = dataFromBeforeVC["strdrop"]
labelTwo.text = dataFromBeforeVC["strbookdetail"]
}
第 1 vc
:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
TableView.deselectRowAtIndexPath(indexPath, animated: true)
let tripcontroller = self.storyboard?.instantiateViewControllerWithIdentifier("UpcomingController") as! Secondcontroller
let cell:UITableViewCell = tableView.cellForRow(at: indexPath as IndexPath)!
let strbookdetail : NSString=arrDict[indexPath.row] .valueForKey("booking_detail") as! NSString
let strdrop : NSString=arrDict[indexPath.row] .valueForKey("drop_address") as! NSString
tripcontroller.dataFromBeforeVC = [
"strdrop":strbookdetail,
"strdrop":strbookdetail
]
self.navigationController?.pushViewController(tripcontroller, animated: true)
}
我正在使用 Swift 开发应用程序 2. 在我的应用程序中,我正在将 JSON 数据解析为 table 视图。当我点击一个单元格时,它成功移动到另一个视图控制器,但我无法获取 json 数据。
这是我的视图控制器中的代码:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath){
var cell = self.TableView.dequeueReusableCellWithIdentifier("Cell",forIndexPath: indexPath) as! CustomTableCell
let strbookdetail : NSString=arrDict[indexPath.row] .valueForKey("booking_detail") as! NSString
let strdrop : NSString=arrDict[indexPath.row] .valueForKey("drop_address") as! NSString
cell.Bookdetail.text=strbookdetail as String
cell.Dropaddress.text=strdrop as String
return cell as CustomTableCell
}
//It helps to parse JSON data to table view.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
TableView.deselectRowAtIndexPath(indexPath, animated: true)
let tripcontroller = self.storyboard?.instantiateViewControllerWithIdentifier("UpcomingController") as! Secondcontroller
navigationController?.pushViewController(tripcontroller, animated: true)
}
点击单元格有助于移动到其他视图控制器SecondController
在我的 SecondController
中,我有两个 UILabel
。在那些标签中,我想显示数据,即 strbookdetail
和 strdrop
如何在两个视图控制器之间传递引用?
使用property
传递数据很容易做到:
第 2 vc
:
var dataFromBeforeVC:[String:String] = [String:String]()
override func viewDidLoad() {
super.viewDidLoad()
initData ()
}
func initData() {
// set data strbookdetail and strdrop to labelOne & labelTwo
labelOne.text = dataFromBeforeVC["strdrop"]
labelTwo.text = dataFromBeforeVC["strbookdetail"]
}
第 1 vc
:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
TableView.deselectRowAtIndexPath(indexPath, animated: true)
let tripcontroller = self.storyboard?.instantiateViewControllerWithIdentifier("UpcomingController") as! Secondcontroller
let cell:UITableViewCell = tableView.cellForRow(at: indexPath as IndexPath)!
let strbookdetail : NSString=arrDict[indexPath.row] .valueForKey("booking_detail") as! NSString
let strdrop : NSString=arrDict[indexPath.row] .valueForKey("drop_address") as! NSString
tripcontroller.dataFromBeforeVC = [
"strdrop":strbookdetail,
"strdrop":strbookdetail
]
self.navigationController?.pushViewController(tripcontroller, animated: true)
}