PrepareForSegue 从自定义原型单元格中的 UIButton [英] PrepareForSegue from a UIButton in a custom prototype cell
问题描述
正如标题所说,我有一个带有原型单元格的 tableView;单元格是一个自定义单元格(所以我在为图像、标签、按钮等创建了 IBOutlet
的女巫中创建了一个名为 CustomCell.swift
的类);这是我的课
As the title say I have a tableView with prototype cell; cell is a custom cell (so I made a class called CustomCell.swift
in witch I created the IBOutlet
for image, label, button etc); here my class
import UIKit
class CustomCell: UITableViewCell
{
@IBOutlet var imageSquadra: UIImageView!
@IBOutlet var button: UIButton!
override func awakeFromNib()
{
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool)
{
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
然后我制作了UITableViewController
:
import UIKit
class SquadreController: UITableViewController
{
var index: NSIndexPath?
var isScrolling = Bool()
override func viewDidLoad()
{
super.viewDidLoad()
DataManager.sharedInstance.createCori()
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int
{
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return DataManager.sharedInstance.arrayCori.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomCell
let squadra = DataManager.sharedInstance.arrayCori[indexPath.row]
cell.backgroundColor = UIColor.clearColor()
if (indexPath.row % 2 == 0)
{
cell.backgroundColor = UIColor.greenColor()
}
else
{
cell.backgroundColor = UIColor.blueColor()
}
//Here I added target to the button in the cell, and below in the class I implemented the fun makeSegue()
cell.button.addTarget(self, action: "makeSegue", forControlEvents: UIControlEvents.TouchUpInside)
return cell
}
//Following 4 method are used to detect UIScollView scrolling and to change cell height.
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
tableView.deselectRowAtIndexPath(indexPath, animated: true)
index = indexPath
tableView.beginUpdates()
tableView.endUpdates()
}
override func scrollViewWillBeginDragging(scrollView: UIScrollView)
{
isScrolling = true
tableView.beginUpdates()
tableView.endUpdates()
}
override func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool)
{
isScrolling = false
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
if isScrolling
{
return 100
}
if index == indexPath
{
return 200
}
else
{
return 100
}
}
//Here I implemented the makeSegue() func, the action I had made as target of the button.
func makeSegue() {
self.performSegueWithIdentifier("toCoriViewController", sender: self)
}
}
好的,现在是困难的部分:制作prepareForSegue
;我没有任何想法如何解决这个问题,我试过了
Ok ok now comes the hard part: to make the prepareForSegue
; I do not have any ideas how to solve this problem, I tried
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
if segue.identifier == "toCoriViewController"
{
if let indexPath = ?????????????
{
let controller = segue.destinationViewController as! CoriViewController
controller.coriSquadra = DataManager.sharedInstance.arrayCori[indexPath.row]
}
}
}
但我不知道如何设置常量indexPath
.
but I don't know how to set the constant indexPath
.
哦,首先我通过 control-right 从按钮到第二个控制器做了一个 segue:也许我必须从 tableView 单元格做那个 segue???
Oh, first of all I made a segue by control-right from the button to the second controller: maybe I must make that segue from the tableView cell???
希望有人能帮助我!
推荐答案
可以这样获取单元格的索引路径
You could get the index path of the cell like so
let indexPath : NSIndexPath
if let button = sender as? UIButton {
let cell = button.superview?.superview as! UITableViewCell
indexPath = self.tableView.indexPathForCell(cell)
}
您还需要像这样更改makeSegue
:
func makeSegue(button:UIButton) {
self.performSegueWithIdentifier("toCoriViewController", sender: button)
}
在您的 cellForRowAtIndexPath
中,只需将设置操作的行更改为 cell.button.addTarget(self, action: "makeSegue:", forControlEvents: UIControlEvents.TouchUpInside)代码>.
and in your cellForRowAtIndexPath
just change the line where you set the action to cell.button.addTarget(self, action: "makeSegue:", forControlEvents: UIControlEvents.TouchUpInside)
.
或者,您可以在自定义单元格类中创建一个 squadra
属性来保存该单元格的 arrayCori
值,因此您将拥有一些如下所示的代码:
Alternatively, you could create a squadra
property inside your custom cell class to hold the arrayCori
value of that cell, so you'd have some code that looks like this:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomCell
let squadra = DataManager.sharedInstance.arrayCori[indexPath.row]
cell.squadra = squadra #add this line
cell.backgroundColor = UIColor.clearColor()
if (indexPath.row % 2 == 0)
{
cell.backgroundColor = UIColor.greenColor()
}
else
{
cell.backgroundColor = UIColor.blueColor()
}
//Here I added target to the button in the cell, and below in the class I implemented the fun makeSegue()
cell.button.addTarget(self, action: "makeSegue", forControlEvents: UIControlEvents.TouchUpInside)
return cell
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
if segue.identifier == "toCoriViewController"
{
let controller = segue.destinationViewController as! CoriViewController
if let button = sender as? UIButton {
let cell = button.superview?.superview as! CustomCell
controller.coriSquadra = cell.squadra
}
}
}
这篇关于PrepareForSegue 从自定义原型单元格中的 UIButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!