Prototype Cell 中的 UILabel 没有占据正确的位置

UILabel in Prototype Cell does not take correct position

我有一个 UITableView 包含在 UIViewController 中。此 table 视图只有一个原型单元格。

该单元格包含一个UIImageView和两个UILabel。第一个标签设置正确(位置和文本)。但是第二个没有按预期定位。

我希望此标签在单元格的内容视图中水平和垂直居中。但是当我执行 print(noFriendRequestLabel) 时,结果是:

<UILabel: 0x7af81c00; frame = (-42 -21; 42 21); text = 'Aucune invitation'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7af82680>>

标签采用了正确的文本,但位置不正确。我正在使用故事板和自动布局。我不明白我做错了什么。

下面是cellForIndexPath()的代码:

// "Friends requests" section
         if indexPath.section == 0 {

            // If requests array is empty, show "No request" label
            if self.friendsRequestsArray.count == 0 {


                let cell = tableView.dequeueReusableCellWithIdentifier("friendCell", forIndexPath: indexPath) as! FriendCustomCell


                cell.noFriendRequestLabel.hidden = false
                cell.noFriendRequestLabel.text   = NSLocalizedString("No request", comment: "")

                cell.friendAvatar.hidden = true
                cell.friendName.hidden   = true
                cell.friendButton.hidden = true

                print(cell.noFriendRequestLabel)
                return cell
            }

            // Else display friends requests
            else {

                let cell = tableView.dequeueReusableCellWithIdentifier("friendCell", forIndexPath: indexPath) as! FriendCustomCell


                cell.noFriendRequestLabel.hidden = true
                cell.friendAvatar.hidden         = false
                cell.friendName.hidden           = false
                cell.friendButton.hidden         = false


                cell.friendAvatar.image = self.friendsRequestsArray[indexPath.row].getAvatar()
                cell.friendName.text    = self.friendsRequestsArray[indexPath.row].toString()

                cell.user = self.friendsArray[indexPath.row]

                cell.user.delegate = UserEventsManager()

                return cell
            }
        }





        // "Your friends" section
        else {

            let cell = tableView.dequeueReusableCellWithIdentifier("friendCell", forIndexPath: indexPath) as! FriendCustomCell

            cell.noFriendRequestLabel.hidden = true
            cell.friendAvatar.hidden         = false
            cell.friendName.hidden           = false
            cell.friendButton.hidden         = false

            cell.friendAvatar.image = self.friendsArray[indexPath.row].getAvatar()
            cell.friendName.text    = self.friendsArray[indexPath.row].toString()

            cell.user = self.friendsArray[indexPath.row]

            cell.user.delegate = UserEventsManager()

            return cell
        }

任何帮助将不胜感激。

这是标签的约束条件:

我明白是怎么回事了。

在包含 UITableViewUIViewController 中,我正在向服务器发送请求,以获取用户的好友请求和当前好友。

因此,当此请求结束时,我重新加载 table 视图的数据。但是,之前,在 numberOfRowsInSection() 中,我在请求结束前 设置了数字

结果是我重新加载了第二部分的数据,不是第一秒的数据(我试图显示标签的地方)。

对我有用的是我在我的自定义 UITableView 中添加了一个标志 loadData,设置为 false。当我的控制器中的请求结束时,我将此标志设置为 true。在 numberOfRowsInSection() 中,我 return 0 如果标志是 false 或每个部分的行数,如果它是 true