繁体   English   中英

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

[英]Swift - Create a UITableViewController with one static cell at the top and the rest be dynamic cells?

我无法在表格视图的顶部使用一个静态单元格制作表格视图。 该表视图将包含4个按钮,其余视图将保留用户歌曲的列表。

我已经在这里研究了其他答案,但是所有答案似乎都是用目标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

    }

这就是我用来设置常规表格视图的方式。 我将如何修改此代码以在顶部保留一个静态单元格而在其余部分保留一个动态单元格?

不要使用Static Cells 在表视图中选择“ Dynamic Prototypes ,然后创建2个原型单元。

在此处输入图片说明

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

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
    }
}

您可以为此使用表的HeaderView,只需将自定义视图提供给即可。 tableHeaderView属性示例:

override func viewDidLoad() {
   super.viewDidLoad()
   let myCustomView = MyCustomView()
   tableView.tableHeaderView = myCustomView
}

文档: https : //developer.apple.com/documentation/uikit/uitableview/1614904-tableheaderview

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM