我知道以前有人问过这个问题,但我找到的所有解决方案对我来说都没有意义。我是iOS初学者,我知道如何左键单击并拖动按钮到另一个视图进行转换,但我无法使用表视图。我的布局只有两个视图控制器,一个带有表视图,另一个为空,我最终希望为每次单击表视图单元格更新标签。
下面的代码是第一个视图控制器的代码,它具有使基本表视图工作的所有功能
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()
}
}