我有一个 UITableViewController,它有一个自定义单元格,我想在其中显示图像和标签。截图可以很好地解释我的问题,看起来像这样
.
当我选择任何单元格时,它看起来像
根据约束,在 TableView Controller 中单元格不以正确的形状可见 这是我的带有自动布局约束的自定义单元格
我该如何解决这个问题? ...我以编程方式创建了这个 tableviewcontroller ,而不使用 Storyboard。 这是数据源和tableviewcontroller委托(delegate)的代码示例
override func numberOfSections(in tableView: UITableView) -> Int {
var numOfSections: Int = 0
let count = conversations.count
if count > 0 {
// tableView.separatorStyle = .none
numOfSections = 1
tableView.backgroundView = nil
}
else
{
let frame = CGRect(x: 0,
y: 0,
width: tableView.bounds.size.width,
height: tableView.bounds.size.height)
let noDataLabel: UILabel = UILabel(frame: frame)
noDataLabel.text = "You don't have any messages. 🙃"
noDataLabel.textColor = UIColor.black
noDataLabel.textAlignment = .center
tableView.backgroundView = noDataLabel
tableView.separatorStyle = .none
}
return numOfSections
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return conversations.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "inboxCell", for: indexPath) as! InboxCell
cell.conversation = conversations[indexPath.row]
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let uids = conversations[indexPath.row].conversationUseruids
for uid in uids{
if uid == Account.account.user.uid{
}
else{
User.getUser(with: uid, completion: { (user) in
self.selectedUser.append(user!)
})
}
}
tableView.deselectRow(at: indexPath, animated: true)
let index = indexPath.row as Int
messageVC.conversationIndex = index
messageVC.conversation = self.conversations[index]
navigationController?.pushViewController(messageVC, animated: true)
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 80
}
最佳答案
发生这种情况是因为如果不起作用,您的图像没有上下约束,请告诉我
关于ios - 来自 Xib 的 UITableViewCell,单元格高度和单元格选择区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49673185/