Search code examples
iosswiftuitableviewcell

Swift Cells Not Displaying


I have a profile page that is made up of two custom tableview cells. The first custom cell is the user's info. The second custom cell is the user's friend. The first row is the user's info, and all of the cells after that are the user's friends. My code worked in Xcode 6, but stopped working after the update.

Problem: A user with 2 friends, their profile page should have a table with three cells: 1 user info cell, 2 friend cells. However, the first and second cell aren't showing. Only the third cell is showing.

Clarification: There should be three cells. Cell 1 is not showing. Cell 2 is not showing. But Cell 3 is showing. Cell 1 is the user's info. Cell 2 is one friend. Cell 3 is another friend.

Here's my code:

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return friendList.count + 1
}

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if indexPath.row == 0{
        return 182.0
    }else{
        return 95.0
    }
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    if indexPath.row != 0{
        let cell = tableView.dequeueReusableCellWithIdentifier("friendCell", forIndexPath: indexPath) as! ProfileFriendTableViewCell

        let friend = friendList[indexPath.row - 1]

        cell.nameLabel.text = friend[1]
        cell.usernameLabel.text = friend[2]
        cell.schoolLabel.text = friend[3]
        cell.sendRequestButton.tag = indexPath.row

        var profileImageExists = false

        if profileImages != nil{
            for profileImage in profileImages{
                if profileImage.forUser == friend[2]{
                    profileImageExists = true
                    cell.friendImageProgress.hidden = true
                    cell.profilePic.image = UIImage(data: profileImage.image)
                    UIView.animateWithDuration(0.2, animations: {
                        cell.profilePic.alpha = 1
                    })
                }
            }
        }else if loadingImages == true{
            profileImageExists = true
            cell.friendImageProgress.hidden = true
            cell.profilePic.image = UIImage(named: "profileImagePlaceholder")
            UIView.animateWithDuration(0.2, animations: {
                cell.profilePic.alpha = 1
            })
        }

        if profileImageExists == false{
            if Reachability.isConnectedToNetwork() == true{
                let query = PFUser.query()
                query?.getObjectInBackgroundWithId(friend[0], block: { (object, error) -> Void in
                    if error == nil{
                        if let object = object as? PFUser{
                            let friendProfilePicture = object.objectForKey("profileImage") as? PFFile
                            friendProfilePicture?.getDataInBackgroundWithBlock({ (data, error) -> Void in
                                if data != nil{
                                    let image = UIImage(data: data!)
                                    cell.profilePic.image = image

                                    if let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext {

                                        let newProfileImage = NSEntityDescription.insertNewObjectForEntityForName("ProfileImageEntity", inManagedObjectContext: managedObjectContext) as! ProfileImage

                                        newProfileImage.forUser = friend[2]
                                        newProfileImage.image = UIImagePNGRepresentation(image!)

                                        do{
                                            try managedObjectContext.save()
                                        }catch _{
                                            print("insert error")
                                        }
                                    }

                                }else{
                                    cell.friendImageProgress.hidden = true
                                    cell.profilePic.image = UIImage(named: "profileImagePlaceholder")
                                }
                                }, progressBlock: { (progress: Int32) -> Void in
                                    let percent = progress
                                    let progressPercent = Float(percent) / 100
                                    cell.friendImageProgress.progress = progressPercent
                                    cell.friendImageProgress.hidden = true
                            })
                        }
                    }
                })
            }
            else{
                cell.friendImageProgress.hidden = true
                cell.profilePic.image = UIImage(named: "profileImagePlaceholder")
            }
        }

        return cell
    }else{
        let cell = tableView.dequeueReusableCellWithIdentifier("profileTopCell", forIndexPath: indexPath) as! ProfileTableViewCell

        var profileImageExists = false

        if profileImages != nil{
            for profileImage in profileImages{
                if profileImage.forUser == PFUser.currentUser()!.username!{
                    profileImageExists = true
                    cell.profilePic.image = UIImage(data: profileImage.image)
                                        }
            }
        }else if loadingImages == true{
            profileImageExists = true
            cell.profilePic.image = UIImage(named: "profileImagePlaceholder")
        }

        if profileImageExists == false{
            if Reachability.isConnectedToNetwork() == true{
                let profilePicture = PFUser.currentUser()!.objectForKey("profileImage") as? PFFile
                profilePicture?.getDataInBackgroundWithBlock({ (data, error) -> Void in
                    if data != nil{
                        let image = UIImage(data: data!)
                        cell.profilePic.image = image

                        if let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext {

                            let newProfileImage = NSEntityDescription.insertNewObjectForEntityForName("ProfileImageEntity", inManagedObjectContext: managedObjectContext) as! ProfileImage

                            newProfileImage.forUser = PFUser.currentUser()!.username!
                            newProfileImage.image = UIImagePNGRepresentation(image!)

                            do{
                                try managedObjectContext.save()
                            }catch _{
                                print("insert error")
                            }
                        }
                    }else{
                        cell.profilePic.image = UIImage(named: "profileImagePlaceholder")
                    }
                    })
            }else{
                cell.profilePic.image = UIImage(named: "profileImagePlaceholder")
            }
        }

        cell.nameLabel.text = PFUser.currentUser()!.objectForKey("Name") as? String
        cell.usernameLabel.text = PFUser.currentUser()!.objectForKey("username") as? String
        let friendNumber = PFUser.currentUser()!.objectForKey("numberOfFriends") as? Int
        if friendNumber != 1{
            cell.numberOfFriendsLabel.text = "\(friendNumber!) Friends"
        }else{
            cell.numberOfFriendsLabel.text = "1 Friend"
        }

        return cell
    }
}

Solution

  • Thanks to Jeremy Andrews (https://stackoverflow.com/a/31908684/3783946), I found the solution:

    "All you have to do is go to file inspector - uncheck size classes - there will be warnings etc.run and there is the data - strangely - go back to file inspector and check "use size classes" again, run and all data correctly reflected. Seems like in some cases the margin is set to negative."

    It was just a bug.