在DidSelectAtRow函数

时间:2016-11-23 10:09:00

标签: ios swift uitableview uiviewcontroller

我的情况包括一个UITable,当选择一行时,它会打开一个新的VC并发送几个变量。

我的问题是在运行DoSelectAtRow函数之前会执行segue。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
   print ("hello")
    self.selectedCellTitle = self.communities[indexPath.row]
    self.cellId = indexPath.row
    print ("now here: ", communityIds[cellId!])
   self.performSegue(withIdentifier: "showCommunitySegue", sender: self)
}

我知道这是因为上面的print命令没有被执行。然后应用程序在下一个屏幕时崩溃,因为它预期存在的变量(cellId)为空。

如果我在故事板中删除了segue并运行,那么该函数中的所有调试输出都会正确运行。只要我再次创建segue,应用程序就会切换到新屏幕并在运行上述任何代码之前崩溃。

创建我的segue我是:

1)右键单击Storyboard中VC1上的UITableView中的单元格并拖动到我的VC2

2)选择类型为show

3)从VC 1中的prepare for segue函数复制segue标识符名称,并将其粘贴到Storyboard中的identifier属性中以获取新的segue。

有什么想法吗?

以下是VC1的完整代码:

import UIKit

class CommunitiesViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {


var selectedCellTitle: String?
var cellId: Int?
var communities = [String]() //{ didSet {     communitiesTableView.reloadData()
//     }
//  }
var communityIds = [String]()
var flag = true

var userEmailText: String?
var tempComId: Int?

@IBOutlet weak var joinCommunityButton: UIButton!
@IBOutlet weak var createCommunityButton: UIButton!


@IBOutlet weak var communitiesTableView: UITableView!

override func viewDidLoad() {


    self.communitiesTableView.delegate = self
    self.communitiesTableView.dataSource = self

    super.viewDidLoad()

}


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return self.communities.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {



    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath as IndexPath)
    cell.textLabel?.text = self.communities[indexPath.row]

    return cell

}


func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
   print ("hello")
    self.selectedCellTitle = self.communities[indexPath.row]
    self.cellId = indexPath.row
    print ("now here: ", communityIds[cellId!])
   self.performSegue(withIdentifier: "showCommunitySegue", sender: self)
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(true)

    if flag == true
    {

        self.communitiesTableView.reloadData()
        let myUrl = URL(string: "http://www.quasisquest.uk/KeepScore/getDetails.php?");
        var request = URLRequest(url:myUrl!);
        request.httpMethod = "POST";
        let postString = "email=\(self.userEmailText!)";

        request.httpBody = postString.data(using: String.Encoding.utf8);

        let task = URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in

            DispatchQueue.main.async
                {

                    if error != nil {
                        print("error=\(error)")
                        return
                    }

                    do{
                        let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String:AnyObject]

                        if let arr = json?["communities"] as? [[String:String]] {
                            self.communities = arr.flatMap { $0["name"]!}
                            self.communitiesTableView.reloadData()

                        }



                    } catch{
                        print(error)
                    }
            }
        }
        task.resume()
    }


}



override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{


    if segue.identifier == "createCommunitySegue" {
        let createCommunityController: CreateNewCommunity = segue.destination as! CreateNewCommunity
        createCommunityController.myEmail = self.userEmailText
    }

    if segue.identifier == "joinCommunitySegue" {
        let joinCommunityController: JoinCommunity = segue.destination as! JoinCommunity
        joinCommunityController.myEmail = self.userEmailText
    }

    if segue.identifier == "showCommunitySegue" {
       let showCommunityController: ShowCommunityViewController = segue.destination as!ShowCommunityViewController
        print("yes here: ", self.cellId!)
       showCommunityController.communityIsCalled = self.selectedCellTitle
       showCommunityController.comIds = self.communityIds
        showCommunityController.communityId = self.cellId


    }
}

}

1 个答案:

答案 0 :(得分:0)

你正在从Cell到ViewController创建你的segue,你需要像这样从ViewController到ViewController创建segue,

enter image description here