快捷单元格不显示 [英] Swift Cells Not Displaying

查看:51
本文介绍了快捷单元格不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个配置文件页面,该页面由两个自定义表格视图单元格组成。第一个自定义单元格是用户的信息。第二个自定义单元是用户的朋友。第一行是用户的信息,其后的所有单元格都是用户的朋友。我的代码在Xcode 6中有效,但在更新后停止了工作。

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.

问题:一个有2个朋友的用户,他们的个人资料页面上应该有一个包含三个单元格的表:1个用户信息单元格,2个朋友单元格。但是,第一个和第二个单元格未显示。仅显示第三个单元格。

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.

说明:应该有三个单元格。单元格1未显示。单元格2未显示。但是单元格3正在显示。单元格1是用户的信息。单元2是一位朋友。单元格3是另一个朋友。

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.

这是我的代码:

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
    }
}


推荐答案

感谢Jeremy Andrews( https://stackoverflow.com/a/ 31908684/3783946 ),我找到了解决方法:

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

所有您需要做的就是去文件检查器-取消选中大小类-会有警告等.run,然后有数据-很奇怪-回到文件检查器,再次检查使用大小类,运行并正确反映所有数据。似乎在某些情况下,边距设置为负。

"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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆