【问题标题】:Swift Cells Not DisplayingSwift 单元格不显示
【发布时间】:2016-03-01 04:19:05
【问题描述】:

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

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

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

这是我的代码:

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

【问题讨论】:

    标签: ios swift uitableview cell


    【解决方案1】:

    尝试在您的viewDidLoadviewWillAppearheightForRowAtIndexPath 上使用estimatedRowHeightrowHeight = UITableViewAutomaticDimension 返回UITableViewAutomaticDimension,记得对您的自定义单元格设置约束,这样它们才能正常工作。

    【讨论】:

    • 很抱歉,这不起作用。第三个单元格仍然是唯一出现的单元格。
    【解决方案2】:

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

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

    这只是一个错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-14
      • 1970-01-01
      • 2019-08-21
      • 2017-06-01
      • 1970-01-01
      相关资源
      最近更新 更多