我知道以前有人问过这个问题,但我找到的所有解决方案对我来说真的没有意义。我是 iOS 的初学者,我知道如何左键单击并将按钮拖动到另一个 View 以进行转换,但我不能用 tableview 做到这一点。我的布局只有两个 View Controller ,一个带有 tableview,第二个是空白的,我最终想为每个 tableview 单元格点击更新标签。
下面的代码是第一个 View Controller ,包含使基本 tableview 工作的所有内容
import UIKit
var fullList = [String]()
var dateList = [String]()
class ViewController: UIViewController, UITableViewDelegate {
@IBOutlet var betTable: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
if(NSUserDefaults.standardUserDefaults().objectForKey("fullList") != nil) //check if the to do list exists
{
fullList = NSUserDefaults.standardUserDefaults().objectForKey("fullList") as! [String] //store toDoList array into NSUserDefaults to save to iphone
}
if(NSUserDefaults.standardUserDefaults().objectForKey("dateList") != nil)
{
dateList = NSUserDefaults.standardUserDefaults().objectForKey("dateList") as! [String]
}
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return fullList.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")
cell.textLabel?.text = fullList[indexPath.row]
return cell
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)
{
if(editingStyle == UITableViewCellEditingStyle.Delete)
{
fullList.removeAtIndex(indexPath.row)
NSUserDefaults.standardUserDefaults().setObject(fullList, forKey: "fullList")
NSUserDefaults.standardUserDefaults().setObject(dateList, forKey: "dateList")
betTable.reloadData()
}
}
override func viewDidAppear(animated: Bool)
{
betTable.reloadData()
}
}
最佳答案
- 从您的 tableViewController 拖动到 SecondViewController 以创建一个 Segue。 (像这样)
根据需要选择类型。
- 现在选择 Segue 并在属性检查器中,为字段“Identifier”指定任意名称。我们稍后将使用它作为引用。
- 现在在您的 ViewVontroller.swift 文件中,导航到函数
didSelectRowAtIndexPath
。在此您可以调用函数performSegueWithIdentifier
并提供您之前提供的名称。这是代码:
希望这对您有所帮助。 :)
关于ios - 单击表格 View 单元格时转到另一个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32128710/