gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 00:40:32 24 4
gpt4 key购买 nike

我在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com