swift自动重新加载数据

时间:2015-02-23 03:14:13

标签: ios xcode swift

您好我正在试图弄清楚如何制作它以便我的时间轴的所有数据都可以自行重新加载。这是我目前整个TimeLineViewController的代码

import UIKit
import CoreData

class TimelineTableViewController: UITableViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UITextFieldDelegate, UIAlertViewDelegate {

var timelineData:NSMutableArray! = NSMutableArray()

override init(style: UITableViewStyle) {
    super.init(style: style)
    // Custom initialization
}
@IBAction func reportButton(sender: AnyObject) {
    performSegueWithIdentifier("reportSegue", sender: self)
}

@IBAction func composeStatus(sender: AnyObject) {
    performSegueWithIdentifier("composeSegue", sender:self)
}

@IBAction func logout(sender: AnyObject) {
    PFUser.logOut()
    var currentUser = PFUser.currentUser() // this will now be nil
        performSegueWithIdentifier("logoutSegue", sender:self)
    }

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

func loadData(){

    timelineData.removeAllObjects()

    var findTimelineData:PFQuery = PFQuery(className: "woofs")

    findTimelineData.findObjectsInBackgroundWithBlock{
        (objects:[AnyObject]!, error:NSError!)->Void in
        if error == nil{
            for object in objects{
                let woofs:PFObject = object as PFObject
                self.timelineData.addObject(woofs)
            }

            let array:NSArray = self.timelineData.reverseObjectEnumerator().allObjects
            self.timelineData = NSMutableArray(array: array)

            self.tableView.reloadData()
        }
    }
}

override func viewDidAppear(animated: Bool) {
    self.loadData()
}

func refresh(sender:AnyObject)
{
    // Updating your data here...
    self.tableView.reloadData()
    self.refreshControl?.endRefreshing()
}

override func viewDidLoad() {
    super.viewDidLoad()

    var statusBarHidden: Bool

    /*var newFeatureAlert:UIAlertController = UIAlertController(title: "Attention", message: "New feature added. You can now Report content that you find offensive. Just tap on Report to get the instructions on how to.", preferredStyle: UIAlertControllerStyle.Alert)

    newFeatureAlert.addAction(UIAlertAction(title: "AWESOME!", style: UIAlertActionStyle.Default, handler: nil))
    self.presentViewController(newFeatureAlert, animated: true, completion: nil)*/

    self.refreshControl?.addTarget(self, action: "refresh:", forControlEvents: UIControlEvents.ValueChanged)

    UITextView.appearance().tintColor = UIColor.blackColor().colorWithAlphaComponent(1.0)
    UITextView.appearance().backgroundColor = UIColor.clearColor().colorWithAlphaComponent(0.4)
    self.tableView.backgroundView = UIImageView(image: UIImage(named: "hdwallpaper"))
    }

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 timelineData.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell:PAHTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as PAHTableViewCell

    cell.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.3)

    let woofers:PFObject = self.timelineData.objectAtIndex(indexPath.row) as PFObject

    cell.pahTextView.alpha = 0

    cell.timestampLabel.alpha = 0

    cell.usernameLabel.alpha = 0

    cell.pahTextView.text = woofers.objectForKey("content") as String

    var dataFormatter:NSDateFormatter = NSDateFormatter()

    dataFormatter.dateFormat = "dd-MM-yyyy HH:mm"

    cell.timestampLabel.text = dataFormatter.stringFromDate(woofers.createdAt)

    var findwoofers:PFQuery = PFUser.query()

    findwoofers.whereKey("objectId", equalTo: woofers.objectForKey("woofers").objectId)

    findwoofers.findObjectsInBackgroundWithBlock{

        (objects:[AnyObject]!, error:NSError!)->Void in if error == nil{

            if let user:PFUser = (objects as NSArray).lastObject as? PFUser{

                cell.usernameLabel.text = user.username
                cell.timestampLabel.alpha = 0
                cell.profileImageView.alpha = 0
                cell.pahTextView.alpha = 0

                if let profileImage = user["profileImage"] as? PFFile{

                    profileImage.getDataInBackgroundWithBlock {
                    (imageData: NSData!, error: NSError!) -> Void in
                    if error == nil {
                        let image = UIImage(data:imageData)
                        cell.profileImageView.image = image
                    }else{
                        let image = UIImage(named: "BLUEPAW")
                        }
                    }
                }
            }
        }
        UIView.animateWithDuration(1.0, animations:{
            cell.profileImageView.alpha = 1
            cell.usernameLabel.alpha = 2
            cell.timestampLabel.alpha = 4
            cell.pahTextView.alpha = 3
        })
    }
    return cell
}

让我知道我需要改变什么来使这项工作。谢谢。

0 个答案:

没有答案