ios - 我试图同时显示不同的 TableViewCells

标签 ios swift uitableview

我想在 iOS swift 中同时显示 4 个不同的 TableViewCells。幸运的是我做到了。我唯一的问题是如何以 Storyboard 中调整的大小显示它们。相反,它们均以标准尺寸(即小尺寸)显示在模拟器上。

Intended Display (Keep in mind that it is a demo

我已经找到并实现了可帮助我显示多个 TableViewCell 的代码行,到目前为止它一直有效。

    struct Storyboard {

    static let posterViewcont = "PosterViewCell"
    static let descriptionCell = "DescriptionCell"
    static let mapCell = "LocationCell"
    static let feedCont = "FeedCell"

}

override func numberOfSections(in tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return 4
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    switch indexPath.row {

    case 0:
        let cell = tableView.dequeueReusableCell(withIdentifier: Storyboard.posterViewcont, for: indexPath)
        cell.textLabel?.text = "Insert Page View Controller Here!"
        return cell
    case 1:
        let cell = tableView.dequeueReusableCell(withIdentifier: Storyboard.descriptionCell, for: indexPath) as! EventDescriptionCell
        cell.eventDesc.text = "Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda."
        return cell
    case 2:
        let cell = tableView.dequeueReusableCell(withIdentifier: Storyboard.mapCell, for: indexPath) as! LocationCell

        return cell
    case 3:
        let cell = tableView.dequeueReusableCell(withIdentifier: Storyboard.feedCont, for: indexPath)
        cell.textLabel?.text = "Insert Social Feed/Who's Going Here!"
        return cell
    default:
        return UITableViewCell()
    }

}

到目前为止,我已经尝试找到一个合适的解决方案来按预期大小显示每个单元格。但没有运气 让我知道我(可能)做错了什么。

最佳答案

所有你需要的是为不同的单元格返回不同的高度,就像你目前在 cellForRow 方法中所做的那样

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
    if indexPath.row == 0 {
        return 40
    }else if indexPath.row == 1 {
        return 60
    }else{
        return 80
    }
}

关于ios - 我试图同时显示不同的 TableViewCells,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45091111/

相关文章:

ios - CAKeyFrameAnimation 在 presentViewController 之后不工作

ios - 对 dequeueReusableCellWithIdentifier 使用不同的初始化方法

iOS Swift - TableView 删除行错误

iphone - 如何获得 UITableView 的可见矩形?

iphone - 需要帮助使用核心数据来保存我的解析数据

iphone - 在 iphone/xcode SDK 上遍历 TBXML 有帮助吗?

ios - 正则表达式在 Swift 中查找和替换字符串开始和结束文本

Swift NSTextField 文本消失

ios - iPad 上的绘制矩形导致内存问题

iphone - EXC_BAD_ACCESS 使用 [UIView animateWithDuration :animations:completion]