【问题标题】:How to get a UITableView method to run before another override method?如何让 UITableView 方法在另一个覆盖方法之前运行?
【发布时间】:2015-01-08 00:39:58
【问题描述】:

下面是我的代码。在对应用程序进行了一些测试后,我意识到didSelectRowAtIndex 是在prepareForSegue 之后运行的。我怎样才能让didSelectRowAtIndex先运行。

如果答案涉及线程,我不知道它是如何工作的,所以请解释一下。谢谢。

 import UIKit

class AvailableShifts: UITableViewController {

    var shiftData: NSMutableArray = NSMutableArray()
    var shiftID: String!

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(true)

//        self.refreshControl = UIRefreshControl()
//        
//        self.refreshControl?.addTarget(self, action: "refreshList", forControlEvents: .ValueChanged)

        loadData()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Potentially incomplete method implementation.
        // Return the number of sections.
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete method implementation.
        // Return the number of rows in the section.
        return shiftData.count
    }


    override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell {

        let cell:AvailableShiftsCell = tableView!.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath!) as AvailableShiftsCell

        let shift: PFObject = shiftData.objectAtIndex(indexPath!.row) as PFObject

        cell.locationLabel.text = shift.objectForKey("Location") as String?
        cell.shiftLabel.text = shift.objectForKey("Shift") as String?
        cell.dateLabel.text = shift.objectForKey("Date") as String?
        cell.id.text = shift.objectId

        return cell
    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        let shift: PFObject = shiftData.objectAtIndex(indexPath.row) as PFObject
        shiftID = shift.objectId
        println(shiftID)

    }

    func loadData () {


        var getShifts = PFQuery(className: "Shifts")

        getShifts.findObjectsInBackgroundWithBlock {
            (objects:[AnyObject]!, error:NSError!) -> Void in

            if (error == nil) {

                self.shiftData.removeAllObjects()

                for object in objects {

                    self.shiftData.addObject(object)
                }

                let array: NSArray = self.shiftData.reverseObjectEnumerator().allObjects

                self.shiftData = array.mutableCopy() as NSMutableArray

                self.tableView.reloadData()
            }
        }
    }

    @IBAction func refreshButtonPressed(sender: AnyObject) {

        loadData()
    }

//    func refreshList() {
//        
//        loadData()
//        
//        self.refreshControl?.endRefreshing()
//        
//    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
        if (segue.identifier == "details") {
            var svc = segue.destinationViewController as ShiftOverview;

            svc.shiftID = shiftID

            println(shiftID)
        }
    }

}

【问题讨论】:

  • 投反对票,不解释原因。太棒了。
  • 您说要先运行 didSelectRowAtIndex 并说它已经先运行?我不明白你想在这里做什么。
  • 对不起...我的意思是。愚蠢的我。
  • 为什么要先运行 prepareForSegue?它是在您转换视图控制器时调用的,那么为什么要在解决当前视图控制器之前调用它?
  • "我怎样才能让 didSelectRowAtIndex 先运行" 你需要改变你的愿望。理解这一点:它是一个框架。它调用事物的顺序就是它调用事物的顺序。接受现实。你的工作是安排你的架构,这样你就可以接受这个顺序。实际上,您不应该依赖于 任何 特定顺序,因为您永远不知道,事情可能会在没有通知的情况下发生变化(除非它们被明确记录为以特定顺序发生,即使这已被证明是没有对 Apple 的保证)。

标签: ios uitableview swift parse-platform


【解决方案1】:

根本不需要为您的应用程序实现didSelectRowAtIndexPath。如果 segue 是从单元格中进行的,那么 prepareForSegue:sender: 中的 sender 参数将是单元格。您可以使用表格视图方法indexPathForCell: 来获取indexPath,从而获得您需要的PFObject。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-21
    • 2012-03-10
    • 2013-04-11
    • 2017-11-19
    • 1970-01-01
    • 2015-06-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多