【问题标题】:How to set the new object in TableView, swift?如何在 TableView 中快速设置新对象?
【发布时间】:2014-11-06 17:58:58
【问题描述】:

我的任务是做类似MasterDetailApplication 的事情,当我按下按钮(firstViewController)时,当前日期应该保存在我的tableViewsecondViewController)中。当我这样做时,新日期应该在最后一个日期之上。 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


    【解决方案1】:

    将此添加到 viewDidLoad

    let addButton = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "insertNewObject:")
    

    将此添加到您的 MasterViewController

    func insertNewObject(sender: AnyObject) {
        objects.insertObject(NSDate(), atIndex: 0)
        let indexPath = NSIndexPath(forRow: 0, inSection: 0)
        self.tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
    }
    

    这是编辑的样板代码-

    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
            if editingStyle == .Delete {
                objects.removeObjectAtIndex(indexPath.row)
                tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
            } else if editingStyle == .Insert {
                // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
            }
        }
    

    这是您在 Xcode 中创建默认主视图应用程序时所得到的。如果您想将此日期传递给 SecondViewController,则需要在 PrepareforSegue 函数中发送此日期。您将此日期保存在 secondviewcontroller 的 ivar 中。然后一旦你有了你就可以创建一个像这样的单元格

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
    
            let object = objects[indexPath.row] as NSDate
            cell.textLabel.text = object.description
            return cell
        }
    

    如果您将所有这些都添加到您的 Master 中,它会起作用,但是如果您在 SecondViewController 中需要此日期。用户单击 Master 中的一行后,将日期保存为您的 ivar(SecondViewController)。使用 prepareForSegue 向 DestinationViewController 提供日期。

    【讨论】:

    • 其实我知道如何做 MasterDetailApplication 但看起来我有 ViewController 和 TableViewController。当我按下 ViewController 中的按钮时,我移动到 TableViewController,然后我的信息已保存在表中。我的问题是当我从 TableViewController 返回并再次按下按钮并且新信息保存在最后一个信息之上时如何执行此操作。当我按下按钮时,我正处于阶段,我移动到 TableViewController,我的数据保存在 tableView 中,但是当我再次这样做时,我的新数据保存在旧数据下。怎么改?
    • 在 MasterDetailApplication 这种情况下工作正常。新信息保存在旧信息之上。我认为这行代码对此负责:(NSDate(),atIndex:0)。我怎样才能把它放到我的代码中?
    • 在tableViewController中创建一个私有数组。在该数组中插入数据应始终位于索引 0 处。不要附加到数组中。在索引 0 处使用插入。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-11
    • 2014-10-03
    • 2015-11-14
    • 1970-01-01
    • 1970-01-01
    • 2015-05-28
    相关资源
    最近更新 更多