ios - 在 tableView 中添加两个自定义单元格

标签 ios swift uitableview

我在 mainStoryboard 上有一个带有两个自定义单元格的 tableView。

我想在不同的行再设置两个单元格。

但是,当我实现代码时,添加的单元格替换了原始单元格。 (“基本语法3”和“基本语法5”的自定义单元格正在消失。)

我试图寻找答案,但找不到。

我在下面添加了图像和代码。

enter image description here

import UIKit
class HomeViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

      @IBOutlet var tblStoryList: UITableView!      
      var array = PLIST.shared.mainArray

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

      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
                return self.array.count + 1
            }

      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
      if indexPath.row == 0 || indexPath.row == 3 || indexPath.row == 5 {
      let cell  = tableView.dequeueReusableCell(withIdentifier: "HeaderCell", for: indexPath) as! HeaderCell
     cell.headerTitle.text = indexPath.row == 0 ? "First Stage" : indexPath.row == 3 ? "Second Stage" : "Third Stage"
                    return cell
                }

     let cell = tableView.dequeueReusableCell(withIdentifier: "StoryTableviewCell", for: indexPath) as! StoryTableviewCell


                //making plist file
                let dict = self.array[indexPath.row - 1]
                let title = dict["title"] as! String
                let imageName = dict["image"] as! String
                let temp = dict["phrases"] as! [String:Any]
                let arr = temp["array"] as! [[String:Any]]
                let detail = "progress \(arr.count)/\(arr.count)"

                //property to plist file をつなぐ
                cell.imgIcon.image = UIImage.init(named: imageName)
                cell.lblTitle.text = title
                cell.lblSubtitle.text = detail

                cell.selectionStyle = UITableViewCellSelectionStyle.none


                return cell
            }

            func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
                if indexPath.row == 0 {
                    return
                }
                tableView.deselectRow(at: indexPath as IndexPath, animated:true)
                if indexPath.row == 3 {
                    return
                }
                tableView.deselectRow(at: indexPath as IndexPath, animated:true)
                if indexPath.row == 5 {
                    return
                }
                tableView.deselectRow(at: indexPath as IndexPath, animated:true)

                let messagesVc = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
                messagesVc.object = self.array[indexPath.row - 1]
                self.navigationController?.show(messagesVc, sender: self)
            }

最佳答案

您可以使用表格 View 的部分。现在,您将在 numberOfSections 函数中返回 1。而且它只创建一个部分。如果您想使用标题,您可以根据需要使用部分。您还可以用多维数组填充表格 View 单元格。例如:

用于调整部分标题:

let lessonTitles = ["First Stage", "Second Stage"]

章节标题:

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

用于调整部分和行:

let lessons = [["Basic Grammar 1", "Basic Grammar 2"], ["Basic Grammar 3", "Basic Grammar 4"]]

函数的节数应该是:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return lessons.count
}

部分中的行数应为:

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return lessons[section].count
}

创建单元格是这样的:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cellText = data[indexPath.section][indexPath.row]
    ...
}

关于ios - 在 tableView 中添加两个自定义单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44261149/

相关文章:

ios - 使用 mask 显示/隐藏带有动画的 View

ios - EKEventKit - 如何在批量提交后检索 eventIdentifier?

ios - 处理连接到 Web 服务的 native 应用程序的登录功能

swift - 计算属性与在 Swift 中将函数设置为变量之间的区别

iPhone : How to set ImageView's frame in table?

ios - 在 View 接收到手势时做某事

swift - 如何以编程方式快速同时制作两个 View 的动画?

ios - Swift - 两个不同类的相同协议(protocol)

ios - Swift - 内部带有水平堆栈 View 的单元格 TableView 包含不同的文本 - 自动高度

ios - UITableView 类型不符合 IntervalType 协议(protocol)