gpt4 book ai didi

swift - UITABLE查看每个部分中具有不同原型(prototype)单元格的部分

转载 作者:可可西里 更新时间:2023-11-01 01:07:55 26 4
gpt4 key购买 nike

我有一个包含不同部分的 TableView ,每个部分都有不同的原型(prototype)单元格。我已经在 Storyboard中创建了原型(prototype)单元格,但我无法在 cellForRowAtIndexPath 中制作多个单元格。我附上了整个代码以供引用。

这是我的 Storyboard设计

Screen Shot

这是最终的输出屏幕,它应该是这样的

Screen Shot

    import UIKit

class PlacePeopleViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var friendlistTableView: UITableView!

var sectionHead = ["Friends", "Others"]

override func viewDidLoad() {
super.viewDidLoad()
friendlistTableView.delegate = self
friendlistTableView.dataSource = self

// Do any additional setup after loading the view.
}


// MARK: - tableview datasource & delegate

func numberOfSections(in tableView: UITableView) -> Int {
return sectionHead.count
}

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return self.sectionHead[section]
}


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return 5
}else {
return 10
}
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell1") as! RecentVizzTableViewCell
cell.placeName.text = "poland"
cell.placeAddress.text = "Simon Albania"


return cell
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 79
}

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 15
}

}

最佳答案

Note : You can do this using only one cell. just hide/show the connect Button by checking some condition.

如果您想对不同的部分使用不同的单元格,请在您的cellForRowAt indexPath 方法中根据您的部分返回您的单元格,如下所示。

switch  indexPath.section {
case 0:
let cell = tableView.dequeueReusableCell(withIdentifier: "cell1") as! RecentVizzTableViewCellOne
return cell
default:
let defaultcell = tableView.dequeueReusableCell(withIdentifier: "defaultcell") as! RecentVizzTableViewCellDefatult
return defaultcell
}

希望对您有所帮助。祝你好运。

关于swift - UITABLE查看每个部分中具有不同原型(prototype)单元格的部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53736363/

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