gpt4 book ai didi

ios - 如何从 INDEXPATH.ROW 的属性设置标题部分的标题

转载 作者:行者123 更新时间:2023-11-30 12:37:53 25 4
gpt4 key购买 nike

我从 API 得到如下响应:

[{
"name":"apple"
"type": "Fruit"
"taste": sweet

}
{
"name":"lemon"
"type": "Fruit"
"taste": "not tasty"
}]

现在我想将该部分的标题设置为 UITable View 的名称

为此,我在 View Controller 中将其称为:

 var modelclass = [FruitsModelclass]()

override func viewDidLoad()
{
self.loaddata()
}

func loaddata()
{
//here i have called the API and sucessfully get the response in data and assign as

self.modelclass = data
self.tableView.reloadData()
}

override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? {
let cell = tableView!.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath!) as FruitCell
let model = self.Modeldata[indexpath.row]
cell.textlabel.text = model.name
return cell
}

这就是我将名称设置为单元格的方式。我如何将其设置为我的 headerSection..?我的 Api 中有很多 json 对象,但它不是固定的。这里我只介绍了两个,有人可以帮助我吗??

最佳答案

如果您只想添加标题...

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "SOME TEXT" //return your text here, may be
}

如果你想控制标题 View ,那么像这样

    public func tableView(_ tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat{
return 50 //height you want of section
}
public func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?{

let vw = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 50))
//view for header, you can also use view from nib, but thats another matter
vw.backgroundColor = UIColor.white

let label = UILabel(frame: CGRect(x: 10, y: 0, width: tableView.frame.size.width-10, height: 50))
//add label or buttons or what ever u want to view....
label.text = “SOME TEXT“

vw.addSubview(label)

return vw
}

作为旁注,numberOfSections 是用来显示标题的

关于ios - 如何从 INDEXPATH.ROW 的属性设置标题部分的标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42628595/

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