简体   繁体   English

表格单元格内的UIButton不会在Swift中锁定到视图控制器

[英]UIButton inside Table Cell doesn't segue to a View Controller in Swift

I have a problem I can not solve it anywhere. 我有一个问题,我无法在任何地方解决。 I have a view controller with a table view inside it. 我有一个视图控制器,里面有一个表格视图。 This table view is shown some data and has a button to move on to another view controller through certain data of the line to the button. 此表视图显示了一些数据,并具有一个按钮,可通过到该按钮行的某些数据移至另一个视图控制器。 But unfortunately I can not even move on to the other view controller. 但不幸的是,我什至无法继续使用其他视图控制器。 Already tried using only the Storyboard, and programmatically via protocols / delegate. 已经尝试仅使用情节提要和通过协议/委托以编程方式进行尝试。 Can someone help me? 有人能帮我吗? Storyboard 故事板

Follow some code: func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return NumberOfRows } 遵循一些代码:func tableView(tableView:UITableView,numberOfRowsInSection部分:Int)-> Int {return NumberOfRows}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var Cont = 0
    for var i = indexPath.row ; i>0; i-- {
        Cont += QuantidadeCombustivel[i]
    }
    let cell = tableView.dequeueReusableCellWithIdentifier("PostoTableViewCell", forIndexPath: indexPath) as! PostoTableViewCell

    if (self.NomesArray.count != 0) && (self.imagensArray.count != 0) {
        cell.NomePosto.text = self.NomesArray[indexPath.row]
        cell.BandeiraPosto.imageFromUrl(self.imagensArray[indexPath.row])
        cell.distanciaLabel.text = "Distância: \(self.DistanciaArray[indexPath.row])"
        var result = ""
        for i in Cont...Cont+self.QuantidadeCombustivel[indexPath.row+1]-1 {
            result += ("\(self.NomeCombustivel[i]) : \(self.ValorCombustivel[i])\n")
        }
        cell.ListaCombustivel?.text = result;

    }


    return cell
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    openMapForPlace(indexPath.row)
}

class PostoTableViewCell: UITableViewCell { 类PostoTableViewCell:UITableViewCell {

@IBOutlet weak var BandeiraPosto: UIImageView!

@IBOutlet weak var NomePosto: UILabel!

@IBOutlet weak var ListaCombustivel: UITextView!

@IBOutlet weak var distanciaLabel: UILabel!

@IBOutlet weak var SgInfoPosto: UIButton!

You have to do this 你必须这样做

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! TableViewCell

    cell.button.tag = indexPath.row

    return cell

}

and

@IBAction func buttonPressed(sender: AnyObject) {
    print(sender.tag)
    self.performSegueWithIdentifier("yourSegueIdentifier", sender: tableView)
}

Consider 2 ViewCOntrollers , the one with button let's say it as FirstVC and the destination is called as SecondVC.Check is your FirstVC embedded in Navigation Controller?.If No then firstly embed it into navigation controller.Click on FirstVC , then click on Editor tab , inside it click on embed , then embed in Navigation Controler , then Give a segue from FirstVC to SecondVC in storyboard. 考虑2个ViewCOntrollers,一个带按钮的按钮称为FirstVC,目的地称为SecondVC。检查您的FirstVC是否嵌入在Navigation Controller中?如果否,则首先将其嵌入到导航控制器中。单击FirstVC,然后单击Editor选项卡,在其中单击embed,然后将其嵌入Navigation Controler中,然后在情节提要中从FirstVC到SecondVC进行选择。 After that write the below code in your button action method. 之后,在您的按钮操作方法中编写以下代码。

[self performSegueWithIdentifier:@"SegueIdentifier" sender:nil];

In the sender , you can pass your array as well. 在发件人中,您也可以传递数组。

Then write a method 然后写一个方法

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
      SecondVC *destination = [segue destinationViewController];
}

Or else you can use Below code as well In your button action method , write the below code 否则,您也可以使用以下代码在您的按钮操作方法中,编写以下代码

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];

 SecondVC* secondVC = [storyboard instantiateViewControllerWithIdentifier:@"StoryboardIdentifier"];
[self.navigationController pushViewCpntroller:secondVC animated:YES];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM