如何在Swift中的iOS设置中创build类似UITableView的选项?

我正在开发一个应用程序,要求用户从表格视图列表中select一个选项。 这有点类似于iPhone的设置中的铃声选项。 我附上了我正在努力完成的一个屏幕插图。

现在我想要select的选项能够作为一个值发送到我的PHP脚本与HTTPMethod 。 这是我使用的请求的types,我猜,只是将数据存储在variables中。

HTTP请求演示

  let requestURL = NSURL(string: "http://example.com/script.php") let request = NSMutableURLRequest(URL: requestURL!) request.HTTPMethod = "POST" 

插图: 在这里输入图像说明

我明白这是一个初学者,但我还没有find任何教程,在迅速做到这一点。

所有这些都可以使用segue来完成

  1. 你只需要点击tableview中的一个button或一个单元格来转到下一个viewController。你可以使用show segue
  2. 当使用select单元格时, unwind segue一个unwind segue继续。 并使用unwind segue将数据传回。
  3. 用传回的数据更新模型和UI。

这是使用导航控制器和UITableView完成的。

由于导航控制器,带有2个button的顶部栏已经预设。

在导航控制器中,分组的UITableView与标题“提示音”一起使用,单元格是音调的名称。

这是一个使用导航控制器和UITableViewvideo教程 。 另外这个教程是快速的。 https://itunesu.itunes.apple.com/WebObjects/LZDirectory.woa/ra/directory/courses/961180099/feed

我写下了一个示例代码。 看一下这个。

如果您有任何疑问,请随时询问我。

MainViewController

 class MainViewController: UIViewController, ListViewControllerProtocol { @IBOutlet weak var button:UIButton? override func viewDidLoad() { super.viewDidLoad() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } func toList() { var listViewController = ListViewController(nibName: "ListViewController", bundle: nil) listViewController.delegate = self self.pushViewController(listViewController, animated: true) } @IBAction func buttonClicked(sender: UIButton) { self.toList() } func onSelectObj(obj:NSDictionary) { // set data to button } } 

ListViewController

 class ListViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { @IBOutlet weak var tableView:UITableView? var delegate:ListViewControllerProtocol? var identifier = "ListViewController" var data = [] override func viewDidLoad() { super.viewDidLoad() self.tableView?.registerClass(UITableViewCell.self, forCellReuseIdentifier: identifier) self.updateData() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } // MARK: UITableViewDataSource func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return data.count } func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { return 37 } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier(identifier, forIndexPath: indexPath) as! UITableViewCell var row = UInt(indexPath.row) var obj = self.data[0] as! NSDictionary cell.textLabel?.text = obj["name"] return cell; } func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { tableView.deselectRowAtIndexPath(indexPath, animated: true) var obj = self.data[0] as! NSDictionary self.selectObj(obj) } private func selectObj(obj: NSDictionary) { self.delegate?.onSelectObj(obj) self.navigationController?.popViewControllerAnimated(true) } private func updateData() { // Code for getting data self.data = someDeserializedData self.tableView!.reloadData() // reload! } } protocol ListViewControllerProtocol { func onSelectObj(obj:NSDictionary) }