gpt4 book ai didi

ios - Swift - 创建一个 UITableViewController,顶部有一个静态单元格,其余为动态单元格?

转载 作者:行者123 更新时间:2023-11-28 07:27:13 25 4
gpt4 key购买 nike

我在制作表格 View 时遇到问题,其中一个静态单元格位于表格 View 的最顶部。此 TableView 将包含 4 个按钮,其余 View 将包含用户歌曲列表。

我已经研究过这里的其他答案,但它们似乎都是用 Objective C 编写的,不是很快。

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return sortedSongs.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "RecentCell", for: indexPath) as! RecentCell

//cell.songTitle.text = albumList[indexPath.row]
//cell.songArtist.text = artistList[indexPath.row]
//cell.songImage.image = imageList[indexPath.row]

return cell

}

这就是我一直用来设置常规表格 View 的方法。我将如何修改此代码以允许顶部的静态单元格和其余的动态单元格?

最佳答案

不要使用静态单元格。在表格 View 中选择 Dynamic Prototypes 并创建 2 个原型(prototype)单元格。

enter image description here

并返回第一部分的第一个单元格,第二部分的其他单元格

override func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return 1
} else if section == 1 {
return sortedSongs.count
} else if section == 2 {
return anotherArrayCount
}
return 0
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "FirstCell", for: indexPath) as! FirstCell
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "RecentCell", for: indexPath) as! RecentCell
//cell.songTitle.text = albumList[indexPath.row]
//cell.songArtist.text = artistList[indexPath.row]
//cell.songImage.image = imageList[indexPath.row]
return cell
}
}

关于ios - Swift - 创建一个 UITableViewController,顶部有一个静态单元格,其余为动态单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56204019/

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