ios - 来自 Xib 的 UITableViewCell,单元格高度和单元格选择区域

标签 ios swift uitableview custom-cell

我有一个 UITableViewController,它有一个自定义单元格,我想在其中显示图像和标签。截图可以很好地解释我的问题,看起来像这样

enter image description here .

当我选择任何单元格时,它看起来像

enter image description here

根据约束,在 TableView Controller 中单元格不以正确的形状可见 这是我的带有自动布局约束的自定义单元格 enter image description here

我该如何解决这个问题? ...我以编程方式创建了这个 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/

相关文章:

ios - youtube-ios-player-helper 和 chromecast

ios - 表更新时 NSFetchedResultsController 无法滚动

ios - 设备旋转时 UITableViewHeader 不保持位置

ios - 如何在 IOS 中从 google drive V3 API 下载 pdf?

ios - MKMapView 获取自定义路线坐标之间的距离

ios - 为什么在不应该为大小类安装的约束上会出现自动布局错误?

php - Emoji编码mysqlquery问题

ios - 引用类时了解 iOS 文档 (cocoa touch)

json - 使用 Swift 从 JSON 数组填充 UITableView

ios - 应用审查时应用内购买失败