gpt4 book ai didi

swift - 带有阴影和角半径的分组 UITableView?

转载 作者:行者123 更新时间:2023-11-30 10:48:21 27 4
gpt4 key购买 nike

我想知道如何创建一个分组的 UITableView,其中包含具有阴影和圆角的部分。此外,部分标题是可选的,因此当用户单击标题时,该部分中的单元格应该折叠;因此,阴影和圆角需要更改为该部分的新大小,只是标题。有人知道如何做我正在谈论的事情吗?我附上了一个 UI 模型供引用以及一些代码。

这是我想要设计的 UI 模型: UI Design Mockup

这是我必须设置 tableView 的代码:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if section == 0 {
return nil
} else if settings[section - 1].isEmpty == false {
let headerView = Bundle.main.loadNibNamed("HeaderTableViewCell", owner: self, options: nil)?.first as! HeaderTableViewCell

let settingCategories = ["Account", "Notifications", "Personalized Feed", "Color Theme"]

headerView.settingCategoryTitle.text = settingCategories[section - 1]

let totalCellHeight = CGFloat(settings[section - 1].count * 63)
let height = headerView.frame.height + totalCellHeight

print("Needed Height: \(height)")

headerView.clipsToBounds = false
headerView.headerBackground.frame.size.height = height



print("HeaderView Height: \(headerView.headerBackground.frame.size.height)")

headerView.headerBackground.layer.shadowRadius = 10
headerView.headerBackground.layer.shadowOffset = CGSize(width: 0, height: 1)
headerView.headerBackground.layer.shadowColor = UIColor.black.cgColor
headerView.headerBackground.layer.shadowOpacity = 0.1
headerView.headerBackground.backgroundColor = UIColor(named: "Background")
headerView.headerBackground.layer.cornerRadius = 10

headerView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleDropDown)))

return headerView
} else {
return nil
}
}

@objc func handleDropDown() {
print("Handle Drop Down")
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if section == 0 {
return 0.0001
} else {
return 54
}
}

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


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

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return 1
} else {
return settings[section - 1].count
}
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "SettingTitleCell", for: indexPath) as! SettingsTitleTableViewCell

cell.titleLabel.text = "Settings"

return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "SettingCell", for: indexPath) as! SettingTableViewCell

cell.setSetting(setting: settings[indexPath.section - 1][indexPath.row])
print("Index Path: \(indexPath.row) SettingCount: \(settings[indexPath.section - 1].count)")


return cell
}

}

该代码产生以下结果: Current Result

我希望获得一些关于如何按照我在设计中所示的方式进行这项工作的帮助。提前感谢您的帮助。

最佳答案

关于swift - 带有阴影和角半径的分组 UITableView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55292021/

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