jeudi 13 août 2015

UITableView not displaying content unless scrolling, or touching cells

I have 2 different UITableViews displaying posts that users are able to post on the app. I am using the Parse Framework to get that information on the cloud.

After working on my first TableView, it works just fine, so I recycled that code and I modified it so it works with a different UITableView that is displaying data from a different table on the database.

The first UITableView works just as expected, but the second one is not, even though the code is pretty much the same, the only thing that changes is the table where it's getting its data from.

Here's the code I'm using for getting the data from the cloud and displaying on the app:

@IBAction func loadData() {
    timelineData.removeAllObjects()

    var findTimelineData:PFQuery = PFQuery(className: "posts")
    findTimelineData.orderByAscending("createdAt")

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

        if error == nil{
            for object in objects!{
                let post:PFObject = object as! PFObject
                self.timelineData.addObject(post)
            }

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

            self.tableView.reloadData()
        }
    }
}


override func viewDidLoad() {
    super.viewDidLoad()

    navigationController?.navigationBarHidden = false
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    //return # of sections
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    //# of elements in the timeLineData array
    return timelineData.count
}


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

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

    cell.postTextView.alpha = 0
    cell.usernameLabel.alpha = 0
    cell.timestampLabel.alpha = 0

    cell.postTextView.text = post.objectForKey("content") as! String

    var dataFormatter:NSDateFormatter = NSDateFormatter()
    dataFormatter.dateFormat = "yyyy-MM-dd HH:mm"
    cell.timestampLabel.text = dataFormatter.stringFromDate(post.createdAt!)

    // to get username from the post
    var showUsername:PFQuery = PFUser.query()!
    //the objectID is the same as the user in the two different tables
    showUsername.whereKey("objectId", equalTo: post.objectForKey("user")!.objectId!!)

    showUsername.findObjectsInBackgroundWithBlock{
        (objects: [AnyObject]?, error: NSError?) -> Void in
        if error == nil{
            let user = (objects as! [PFUser]).last
            cell.usernameLabel.text = user!.username

            UIView.animateWithDuration(0.5, animations: {
                cell.postTextView.alpha = 1
                cell.usernameLabel.alpha = 1
                cell.timestampLabel.alpha = 1
            })
        }
    }

    return cell
}

any ideas of what's going on? I already tried other posts on here, but nothing has been able to fix this issue.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire