Swift - 单击行后在表视图中显示每一行的数据
Swift - displaying data for each row in tableview after row is clicked
单击行后在表视图中显示每一行的数据时出现问题,我想使用 didselectRowAtIndexPath,不是没有它! :)
ViewController.swift
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
let textToDisplayInCell:[String] = ["item 1", "item 2", "item 3", "item 4"]
let textDataforEachRowWhenClicked:[String] = ["first item", "second item ", "third item", "fourth item"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.tableView.delegate = self
self.tableView.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.textToDisplayInCell.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Tablecell") as! UITableViewCell
cell.textLabel!.text = self.textToDisplayInCell[indexPath.row]
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.performSegueWithIdentifier("toDetailSegue", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var whatToPass = self.textDataforEachRowWhenClicked
let detailVC = segue.destinationViewController as! DetailViewController
detailVC.array = whatToPass
}
}
DetailViewcontroller.swift
导入 UIKit
class DetailViewController: UIViewController {
@IBOutlet weak var label: UILabel!
var array:[String] = [String]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
var connection = self.array
self.label.text = String(stringInterpolationSegment: connection)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
我在单击每一行后得到了这个,相同的数据,我想要当行 - 单击第 1 项以在 detailviewcontroller 中显示标签中的第一项,依此类推其他行,帮助,ty
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
let textToDisplayInCell:[String] = ["item 1", "item 2", "item 3", "item 4"]
let textDataforEachRowWhenClicked:[String] = ["first item", "second item ", "third item", "fourth item"]
var selectedValue:String!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.tableView.delegate = self
self.tableView.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.textToDisplayInCell.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Tablecell") as! UITableViewCell
cell.textLabel!.text = self.textToDisplayInCell[indexPath.row]
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
selectedValue = textDataforEachRowWhenClicked[indexPath.row] as String
self.performSegueWithIdentifier("toDetailSegue", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let detailVC = segue.destinationViewController as! DetailViewController
detailVC.message = selectedValue
}
}
在详细视图中:
class DetailViewController: UIViewController {
@IBOutlet weak var label: UILabel!
var message:String!
override func viewDidLoad() {
super.viewDidLoad()
var message = self.message
self.label.text = message
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
在 prepareForSegue 中,您将整个数组传递给子控制器,而不是单个特定元素。您需要在 didSelectRowAtIndexPath 中将一些变量 (_rowToPass) 设置为您要传递的元素,并在 prepareForSegue 中仅传递该元素。
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
_rowToPass = indexPath.row
self.performSegueWithIdentifier("toDetailSegue", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var whatToPass = self.textDataforEachRowWhenClicked[_rowToPass]
let detailVC = segue.destinationViewController as! DetailViewController
detailVC.array = whatToPass
}
单击行后在表视图中显示每一行的数据时出现问题,我想使用 didselectRowAtIndexPath,不是没有它! :)
ViewController.swift
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
let textToDisplayInCell:[String] = ["item 1", "item 2", "item 3", "item 4"]
let textDataforEachRowWhenClicked:[String] = ["first item", "second item ", "third item", "fourth item"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.tableView.delegate = self
self.tableView.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.textToDisplayInCell.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Tablecell") as! UITableViewCell
cell.textLabel!.text = self.textToDisplayInCell[indexPath.row]
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.performSegueWithIdentifier("toDetailSegue", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var whatToPass = self.textDataforEachRowWhenClicked
let detailVC = segue.destinationViewController as! DetailViewController
detailVC.array = whatToPass
}
}
DetailViewcontroller.swift
导入 UIKit
class DetailViewController: UIViewController {
@IBOutlet weak var label: UILabel!
var array:[String] = [String]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
var connection = self.array
self.label.text = String(stringInterpolationSegment: connection)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
我在单击每一行后得到了这个,相同的数据,我想要当行 - 单击第 1 项以在 detailviewcontroller 中显示标签中的第一项,依此类推其他行,帮助,ty
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
let textToDisplayInCell:[String] = ["item 1", "item 2", "item 3", "item 4"]
let textDataforEachRowWhenClicked:[String] = ["first item", "second item ", "third item", "fourth item"]
var selectedValue:String!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.tableView.delegate = self
self.tableView.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.textToDisplayInCell.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Tablecell") as! UITableViewCell
cell.textLabel!.text = self.textToDisplayInCell[indexPath.row]
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
selectedValue = textDataforEachRowWhenClicked[indexPath.row] as String
self.performSegueWithIdentifier("toDetailSegue", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let detailVC = segue.destinationViewController as! DetailViewController
detailVC.message = selectedValue
}
}
在详细视图中:
class DetailViewController: UIViewController {
@IBOutlet weak var label: UILabel!
var message:String!
override func viewDidLoad() {
super.viewDidLoad()
var message = self.message
self.label.text = message
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
在 prepareForSegue 中,您将整个数组传递给子控制器,而不是单个特定元素。您需要在 didSelectRowAtIndexPath 中将一些变量 (_rowToPass) 设置为您要传递的元素,并在 prepareForSegue 中仅传递该元素。
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
_rowToPass = indexPath.row
self.performSegueWithIdentifier("toDetailSegue", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var whatToPass = self.textDataforEachRowWhenClicked[_rowToPass]
let detailVC = segue.destinationViewController as! DetailViewController
detailVC.array = whatToPass
}