我有一个包含在 UIViewController 中的 UITableView
。此 TableView 只有一个原型(prototype)单元格。
单元格包含一个UIImageView
,和两个UILabel
。第一个标签设置正确(位置和文本)。但是第二个没有按预期定位。
我希望此标签在单元格的内容 View 中水平和垂直居中。但是当我执行 print(noFriendRequestLabel)
时,结果是:
<UILabel: 0x7af81c00; frame = (-42 -21; 42 21); text = 'Aucune invitation'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7af82680>>
标签采用了正确的文本,但位置不正确。我正在使用 Storyboard和自动布局。我不明白我做错了什么。
下面是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
}
如有任何帮助,我们将不胜感激。
这是标签的约束:
最佳答案
我已经弄清楚发生了什么。
在包含 UITableView
的 UIViewController
中,我向服务器发送请求,以获取用户的好友请求和当前好友。
因此,当此请求结束时,我重新加载 TableView 的数据。但是,之前,在 numberOfRowsInSection()
中,我已将数字设置为 before 请求结束。
结果是我重新加载了第二部分的数据,不是第一秒的数据(我试图显示标签的地方)。
对我有用的是我在我的自定义 UITableView
中添加了一个标志 loadData
,设置为 false
。当我的 Controller 中的请求结束时,我将此标志设置为 true
。在 numberOfRowsInSection()
中,如果标志为 false
,则返回 0;如果为 true
,则返回每个部分的行数。
关于ios - Prototype Cell 中的 UILabel 没有占据正确的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35156408/