Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 从 ;是否通过此序列将UITableView切换到新的ViewController?_Ios_Swift_Uitableview - Fatal编程技术网

Ios 从 ;是否通过此序列将UITableView切换到新的ViewController?

Ios 从 ;是否通过此序列将UITableView切换到新的ViewController?,ios,swift,uitableview,Ios,Swift,Uitableview,我有ViewController,它包含UITableView。数据通过以下代码加载到该表中: import UIKit class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UsernameSentDelegate { @IBOutlet weak var scrollView: UIScrollView! @IBOutlet weak var receiveUsername:

我有
ViewController
,它包含
UITableView
。数据通过以下代码加载到该表中:

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UsernameSentDelegate {

@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var receiveUsername: UILabel!
@IBOutlet weak var userEmailText: UILabel!
var userEmail: String?

var communities = [String]() { didSet { communitiesTableView.reloadData()
    }
}
var flag = false

@IBOutlet weak var communitiesTableView: UITableView!

@IBAction func unwindToHome(_ segue: UIStoryboardSegue) {

}

//recieves email address from delegate from LoginViewController
func userLoggedIn(data: String) {

  userEmailText.text = data
     }

override func viewDidLoad() {
    super.viewDidLoad()

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

}

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

    return self.communities.count
}

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

    let title = self.communities[indexPath.row]

    let cell = UITableViewCell()

    cell.textLabel?.text = title

    return cell

}
然后,我在
UITableView
中设置了一个原型单元,这样我就可以创建一个到第二个视图控制器
ShowCommunities ViewController
的序列,并将该序列命名为
“showCommunitySegue”

ShowCommunities视图控制器中
我设置了一个标签,可以用作所携带单元格名称的标题,名为
communityName

ViewController
中,我设置了以下函数来处理segue,包括所选单元格标题的目标变量

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        self.performSegue(withIdentifier: "showCommunitySegue", sender: self)
showCommunityController.communityName = //what do I put here?
    }

我需要在最后一行添加什么才能显示CommunityController.communityName显示单元格标题?

只需在您的viewController中将
selectedCellTitle
声明为
String

var selectedCellTitle: String?
这将是跟踪选定单元格标题的全局变量

didSelectRowAt
中添加以下内容:

      func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// Set your global variable to the title
            self.selectedCellTitle = self.communities[indexPath.row]
// Trigger your segue
            performSegue(withIdentifier: "showCommunitySegue", sender: self)
        }
按以下方式重写
prepareforsegue
方法:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            if segue.identifier == "showCommunitySegue" {
// Check if the segue's destination viewcontroller is your viewcontroller
                if let showCommunityController = segue.destination as? ShowCommunityViewController {
// Assign the selected title to communityName
                    showCommunityController.communityName = self.selectedCellTitle
            }
        }
    }

只需在单元格所在的viewController中将
selectedCellTitle
声明为
String

var selectedCellTitle: String?
这将是跟踪选定单元格标题的全局变量

didSelectRowAt
中添加以下内容:

      func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// Set your global variable to the title
            self.selectedCellTitle = self.communities[indexPath.row]
// Trigger your segue
            performSegue(withIdentifier: "showCommunitySegue", sender: self)
        }
按以下方式重写
prepareforsegue
方法:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            if segue.identifier == "showCommunitySegue" {
// Check if the segue's destination viewcontroller is your viewcontroller
                if let showCommunityController = segue.destination as? ShowCommunityViewController {
// Assign the selected title to communityName
                    showCommunityController.communityName = self.selectedCellTitle
            }
        }
    }