这是我的情况。当我按下按钮时,我移动到下一个屏幕,同时它将我的2个参数放在TableView中。它必须像MasterDetailApplication一样工作,我努力。当我按下按钮一切正常。我的问题是,我怎么能这样做,当我按下按钮时,数据(2个参数)保存在TableView中,当我再次执行时(按下后退按钮并再次按下Go按钮),新数据将覆盖旧的一。我希望新数据不会覆盖旧数据,它们必须高于它们。这是我的代码:
//Parameters.swift
import Foundation
struct Parameters {
let toPass : String
let toPass2: String
}
//SecondViewController
import UIKit
class SecondViewController: UIViewController {
var timer = NSTimer()
var counter = 0
@IBOutlet weak var labelCounter: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.hidesBackButton = true
labelCounter.text = String(counter)
timer = NSTimer.scheduledTimerWithTimeInterval(1,target:self, selector: Selector("update"),userInfo: nil, repeats :true)
}
func update(){
labelCounter.text = String(++counter)
if counter == 15 {
timer.invalidate()
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd hh:mm:ss"
if (segue.identifier == "segueTest") {
var transfer = segue.destinationViewController as TableViewController
transfer.toPass = labelCounter.text
transfer.toPass2 = "\(formatter.stringFromDate(NSDate()))"
}
}
}
//TableViewCOntroller
import UIKit
class TableViewController: UITableViewController {
@IBOutlet weak var label1: UILabel!
var toPass: String!
var toPass2: String!
var objects = [Parameters]()
override func viewDidLoad() {
super.viewDidLoad()
self.objects = [Parameters(toPass: toPass2, toPass2: toPass)]
// self.view.backgroundColor = UIColor(red :184.0, green: 219.0, blue: 243.0)
// self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
// tableView.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.objects.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("tableCell", forIndexPath: indexPath) as UITableViewCell
var param : Parameters
param = objects[indexPath.row]
cell.textLabel.text = param.toPass
cell.detailTextLabel?.text = param.toPass2 + " sec"
return cell
}
}