将表视图(行选择)的数据传递到视图控制器
mi问题是这样的:我想选择第一,第二...行,并且视图控制器必须根据所选行(标签,图像,等等),我尝试了很多东西,但没有工作。将表视图(行选择)的数据传递到视图控制器
我使用协议,这一点:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 4
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCellF
cell.l2.text = hname[indexPath.row]
cell.l1.text = prename[indexPath.row]
cell.img1.image = imgs[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
let selectedH = self.nin2[(indexPath as NSIndexPath).row]
self.delegate?.HabiSelected(selectedH)
}
感谢
好吧,我将演示如何使用静态字符串数组来完成此操作。 Analise它使你的代码你需要什么(图片,文字)
步骤1:创建新的项目,并选择视图 - 控制转到 - >嵌入在 - >导航控制器。然后将UITableView
添加到您的ViewController
。然后添加一个原型单元格并命名它(例如.cell)。
第2步:创建新的UITableViewCell
(newcell)可可触摸类文件。
第3步:然后转到identity->自定义类 - >放入创造UITableViewCell
可可触摸类文件到您的原型细胞(细胞)。然后将UILabel
添加到您的prototye单元中。
第4步:创建表视图委托给视图控制器,并创建出口标签到新单元,然后创建表视图来查看控制器。
第5步:添加新的UIViewController
你的故事板进行的CollectionView和遵循UITableView
步骤6类似的方法:创建从视图 - 控制推到Segue前收集的ViewController并命名SEGUE。
之后,将以下代码添加到具有表视图的视图控制器。
import UIKit
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource{
@IBOutlet var tableView: UITableView! //Outlets of table View
var titles = ["Michael Jackson","Taylor Swift","Katy Perry","Avril Lavigne"] //static Array
override func viewDidLoad() {
super.viewDidLoad()
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return titles.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! TableCell
cell.TitlesLabel.text = titles[indexPath.row]
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
performSegueWithIdentifier("collectionSegue", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let index = self.tableView.indexPathForSelectedRow
let indexNumber = index?.row //0,1,2,3
let vc = segue.destinationViewController as! collectionViewController
vc.title = titles[indexNumber!] //NavigationItem.title on collectionViewController
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
添加下面的代码到您的收藏视图控制器。
import UIKit
class collectionViewController: UIViewController, UICollectionViewDataSource,UICollectionViewDelegate{
@IBOutlet var collectionView: UICollectionView!
var collectionTitles = ["Hi","Hello","Hola","Ni hao"]
var taylorSwift = ["Speak Now","Red","Love Story","Blank Space"]
var thirdlabel = ["Jack","Sparrow","William","Mohan Singh"]
var fourthlabel = ["Namasta","Vanakkam","Tamil","Hindi"]
override func viewDidLoad() {
super.viewDidLoad()
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return taylorSwift.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = self.collectionView.dequeueReusableCellWithReuseIdentifier("cell2", forIndexPath: indexPath) as! CollectionViewControllerCell
let identity = navigationItem.title
print(title)
if identity == "Michael Jackson"
{
cell.collectionLabel.text = collectionTitles[indexPath.row]
}
else if identity == "Taylor Swift"
{
cell.collectionLabel.text = taylorSwift[indexPath.row]
}
else if identity == "Katy Perry"
{
cell.collectionLabel.text = thirdlabel[indexPath.row]
}
else
{
cell.collectionLabel.text = fourthlabel[indexPath.row]
}
return cell
}
}
是的,这是我所需要的。非常感谢:D – JuanCa2607
不用客气:);) –
你应该编程为下一个视图控制器,你现在创建的实例和信息传递给这样的视图控制器所以
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = self.tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCellF
let nextVC = self.storyboard?.instantiateViewControllerWithIdentifier("your identifier") as! YourCustomViewController
nextVC.yourVariable = cell.theInfoYouWhatToPass
self.presentViewController(nextVC, animated: true, completion: nil)
}
现在你的下一个VC实例将有你需要的信息存储在你的变量中
我尝试过,但是我得到信号SIGABRT – JuanCa2607
第二视图控制器图像上的收藏视图和标签文本将会改变取决于您的表格行点击第一个视图控制器? –
没错,我需要那个 – JuanCa2607