use*_*890 16 uitableview ios swift
我想从自定义UITableViewCell中的按钮执行segue,但是当我按下该按钮时,我不知道如何访问单元格的内容.我意识到这可以通过didSelectRowAtIndexPath来完成,但是我有那个方法执行另一个函数,我想使用我在表格单元格中创建的按钮.
这是我的执行segue方法我遇到了麻烦:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
if segue.identifier == "showComments"
{
let vc:CommentOnPostViewController = segue.destinationViewController as CommentOnPostViewController
var buttonPosition:CGPoint = sender?.convertPoint(CGPointZero, toView: self.feed) as CGPoint!
var path:NSIndexPath = self.feed.indexPathForRowAtPoint(buttonPosition) as NSIndexPath!
var postToCommentOn:PFObject = feedList[path.row] as PFObject
vc.post = postToCommentOn as PFObject
}
}
Run Code Online (Sandbox Code Playgroud)
我在显示单元格时标记按钮,并给它一个目标动作:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
// There is code that goes here for creating and displaying the cell.
cell.commentButton.tag = indexPath.row
cell.commentButton.addTarget(self, action: "addComment", forControlEvents: UIControlEvents.TouchUpInside)
}
Run Code Online (Sandbox Code Playgroud)
以下是按下按钮时调用的操作:
func addComment()
{
self.performSegueWithIdentifier("showComments", sender: self)
}
Run Code Online (Sandbox Code Playgroud)
任何帮助表示赞赏.谢谢!
S.H*_*.H. 42
使用Method创建一个Protocol,它将由TableViewController上定义的CustomCell的Delegate调用
//Pass any objects, params you need to use on the
//segue call to send to the next controller.
protocol MyCustomCellDelegator {
func callSegueFromCell(myData dataobject: AnyObject)
}
Run Code Online (Sandbox Code Playgroud)
现在在UITableViewController上使用Protocol
class MyTableViewController : UITableViewController, MyCustomCellDelegator {
//The usual Defined methods by UIViewController and UITableViewController
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//Set the CustomCell new Delegate
var cell = tableView.dequeueReusableCellWithIdentifier(customIdentifier) as MyCustomCell
cell.delagete = self
return cell
}
//MARK: - MyCustomCellDelegator Methods
func callSegueFromCell(myData dataobject: AnyObject) {
//try not to send self, just to avoid retain cycles(depends on how you handle the code on the next controller)
self.performSegueWithIdentifier("showComments", sender:dataobject )
}
}
Run Code Online (Sandbox Code Playgroud)
在自定义单元格上定义委托,在新按钮内调用委托功能.
class MyCustomCell : UITableViewCell {
var delegate:MyCustomCellDelegator!
@IBOutlet weak var myButton:UIButton
@IBAction func buttonPressed(sender:AnyObject){
var mydata = "Anydata you want to send to the next controller"
if(self.delegate != nil){ //Just to be safe.
self.delegate.callSegueFromCell(mydata)
}
}
}
Run Code Online (Sandbox Code Playgroud)
希望这可以清晰明了,让您在代码中理解和实现.
我找到了一个更快捷的答案,可以使用动态单元格中的按钮将数据传递给新的 VC。
设置一个 Tableview、一个自定义单元格(将单元格与您创建的类连接并设置单元格标识符)并创建一个新的 VC 并通过 segue(还为 segue 设置标识符)将其连接到当前的 TableViewController
使用插座 (cellButton) 设置您的单元
class CustomCell: UITableViewCell {
@IBOutlet weak var cellButton: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
Run Code Online (Sandbox Code Playgroud)TableViewController 类中的 CellForRowAt 函数
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) as! CustomCell
//...Do stuff for your cell
cell.cellButton.tag = indexPath.row //or value whatever you want (must be Int)
cell.cellButton.addTarget(self, action: #selector(TableViewController.buttonTapped(_:)), for: UIControlEvents.touchUpInside)
}
Run Code Online (Sandbox Code Playgroud)设置由按钮目标调用的按钮方法
func buttonTapped(_ sender:UIButton!){
self.performSegue(withIdentifier: "customSegueIdentifier", sender: sender)
}
Run Code Online (Sandbox Code Playgroud)准备 segue 函数
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if(segue.identifier == "customSegueIdentifier") {
if let destination = segue.destination as? YourNewViewController {
if let button:UIButton = sender as! UIButton? {
print(button.tag) //optional
destination.valueViaSegue = button.tag
}
}
}
}
Run Code Online (Sandbox Code Playgroud)希望这段代码对你们有帮助。继续编码;)
归档时间: |
|
查看次数: |
9669 次 |
最近记录: |