【发布时间】:2014-11-06 17:58:58
【问题描述】:
我的任务是做类似MasterDetailApplication 的事情,当我按下按钮(firstViewController)时,当前日期应该保存在我的tableView(secondViewController)中。当我这样做时,新日期应该在最后一个日期之上。 MasterDetailApplication 就像我想要的那样工作,但在同一个 tableVieController 中有按钮。我想我应该使用atIndex : 0,但我不知道如何使用它。
Parameters.swift:
import UIKit
var param: Parameters = Parameters()
struct data{
var timer = NSString()
}
class Parameters: NSObject {
var datas = [data]()
func passingData(timer: NSString)
{
datas.append(data(timer: timer))
}
}
ViewController.swift
import UIKit
class ViewController: UIViewController {
var counter = 0
var timer = NSTimer()
@IBOutlet weak var counterLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
counterLabel.text = String(counter)
timer = NSTimer.scheduledTimerWithTimeInterval(1,target:self, selector: ("update"),userInfo: nil, repeats :true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func update(){
counterLabel.text = String(++counter)
if counter == 50 {
timer.invalidate()
}
}
@IBAction func pressedButton(sender: UIButton) {
param.passingData(counterLabel.text!)
}
}
TableViewController.swift:
import UIKit
class TableViewController: UITableViewController, UITableViewDataSource, UITableViewDelegate{
var data = NSMutableArray()
override func viewDidLoad() {
super.viewDidLoad()
}
/*
func insertNewObject(sender: AnyObject) {
data.insertObject(NSDate(), atIndex: 0)
let indexPath = NSIndexPath(forRow: 0, inSection: 0)
self.tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
}
*/
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 param.datas.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("default", forIndexPath: indexPath) as UITableViewCell
let date = NSDateFormatter()
date.dateFormat = "yyyy-MM-dd\nhh:mm:ss"
cell.detailTextLabel?.text = param.datas[indexPath.row].timer
cell.textLabel.text = "\(date.stringFromDate(NSDate()))"
return cell
}
}
【问题讨论】:
标签: ios xcode swift tableview master-detail