ios - 如何使用 UITableViews 通过 Swift 创建静态和动态行

标签 ios swift uitableview

这个问题是从这个开始的:Use UICollectionViews to create dynamic and multiple features .

我能够创建一个静态单元格来显示食谱的名称和图像,类似于这个应用程序:

enter image description here

我卡住的地方是创建一个动态行,该行根据内部数据量而变化,即餐具或营养值(value),如下图所示:

enter image description here

我知道如何在tableView上正常显示一行行的数据。但不确定如何将它嵌入到 tableView 内的一个部分中。我试图添加多个原型(prototype)单元格并将它们分配给 UITableViewCell 的子类。然后我尝试在我的 cellForRow 中使用 if 语句,但这并不能解决我的问题。

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

    if indexPath.row == 0 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! FirstCell
        //set the data here

        cell.recipeTitle.text = recipe.name
        cell.imageView.image = UIImage(named: "url")
        return cell
    }
    else if indexPath.row == 1 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath) as! SecondCell
        //set the data here
        return cell
    }
    else {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell3", for: indexPath) as! ThirdCell
        //set the data here
        return cell
    }
}

我也看过这个演示:https://medium.com/ios-os-x-development/ios-how-to-build-a-table-view-with-multiple-cell-types-2df91a206429 ,这接近我想要实现的目标,但我发现很难适应它。

如果有人可以指导我如何最好地处理这个问题或一个很好的例子,那么我将非常感激。

最佳答案

首先,您不能在同一个 tableView 中同时拥有静态单元格和动态单元格。那么你如何解决这个问题?定义它们所属部分中的每个静态单元格以及它们所属部分中的动态单元格。但是,这看起来不像您要尝试做的。您只需要同一个 tableView 中的多个部分,每个部分具有不同的数据列表

要做到这一点,您将需要部分的数量,因此请使用 tableView(_:numberOfSections:) 函数。

override func numberOfSections(in tableView: UITableView) -> Int {
    return 3
}

然后您可以(并且可能应该)通过在 tableViewController 中初始化一个带有标题的数组来给每个部分一个标题(假设这就是您正在使用的内容。它也可以只是一个 tableView)。

let headerTitles = ["Nutritional Values", "Utensils", "Ingredients"]

然后使用 tableView(_:titleForHeaderInSection:)

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    if section < headerTitles.count {
        return headerTitles[section]
    }
    return nil
}

现在您可以开始按部分定义行了。

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell: UITableViewCell

    if indexPath.section == 0 {
        //Setup up the first row
        if indexPath.row == 0 {
            //I'm not sure where or how you defined First/SecondCell but this may not work depending on those two questions.
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! FirstCell                
            return cell
        } else if indexPath.row == 1 {
            let cell = Bundle.main.loadNibNamed("StaticCell", owner: self, options: nil)?.first as! StaticCell
            return cell
        }

    } else if indexPath.section == 1 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as! SecondCell  
        //setup cell1 n your storyboard to have a label with a tag # of 12(or whatever number you want to use)
        //you also want an array with your utensil data accessible here
        let label = cell.viewWithTag(12) as! UILabel
        label.text = utensilNames[indexPath.row]              
            return cell

    } else if indexPath.section == 2 {
        let cellIngredients = tableView.dequeueReusableCell(withIdentifier: "Ingredients", for: indexPath)

        tableView.deselectRow(at: indexPath, animated: true)
        return cellIngreidents
    }
    cell = tableView.dequeueReusableCell(withIdentifier: "cell")!
    return cell
}

这里的要点是使用部分然后使用行来分发数据。

只是为了澄清第 0 节第 0 行 - N 将是您设置静态行的地方。我发现最好使用 XIB 文件子类化 TableViewCell。 希望这会有所帮助。

编辑 因此,我查看“静态”单元格的方式是在第一部分中,xib 是您告诉它放置的唯一位置。在上面的示例中,第二个单元格中的第一部分是

关于ios - 如何使用 UITableViews 通过 Swift 创建静态和动态行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44360222/

相关文章:

ios - UISwipeActionsConfiguration/UIContextualAction with icon AND text AND clear background

ios - UITableView reloadData 花费太多时间

ios - UITableViewCell 打嗝中的自动播放视频

ios - 在文本 Swift 末尾旁边显示更多按钮

ios - 使用 AFNetworking 进行 API 调用

ios - ITMS-90809 : Deprecated API Usage -- Apple will stop accepting submissions of apps that use UIWebView APIs

ios - 如何修复 UITableViewCell segue

ios - 本地保存的 .txt 文件的标题在表格 View 中以随机顺序加载 - 如何强制执行特定顺序?

ios - 如何从其句柄(macOS/iOS)获取库的路径?

ios - 当进入结果 View 并返回时,带有 UISearchController 的 UITableView 进入导航栏下方