我正在构建一个项目,我必须制作一个表格 View ,其中我必须包含三个组的图像及其名称和位置。 组别如下:(1).OFFICE BEARERS (2).MEMBERS (3).CO-OPTED MEMBERS.
我想将组设为标题,然后显示与该组相对应的成员的图像以及他们的名称和职务。然后另一个组作为标题和所属成员的图像他们。等等。示例图片如下所示
每个组都有很多成员,所以我想使用动态单元格。
这样做的最佳做法是什么......请帮助我
注意:图像位于我的项目 Assets 中。 tableview 的图像是这样的..
最佳答案
准备一个数据结构,例如像这样:
[
{
"name": "group name"
"members": [{
"image":"member image"
"designation":"member image"
},
{
"image":"member image"
"designation":"member image"
}]
},
{
"name": "group name"
"members": [{
"image":"member image"
"designation":"member image"
},
{
"image":"member image"
"designation":"member image"
}]
}
]
将此数据结构解析为如下所示的模型对象,或者您可以使用字典。
类组:NSObject {
变种名称:字符串!
变量成员:[成员]!
}
类成员:NSObject {
变种图像:UIImage!
变量指定:字符串!
}
然后在您的 tableView 中使用,如下所示
var groups:[Group]!
func numberOfSections(in tableView: UITableView) -> Int {
return groups.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return groups[section].members.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
let group = groups[section] as Group
return group.name
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell Identifier", for: indexPath)
let member = groups[indexPath.section].members[indexPath.row]
cell.imageView?.image = member.image
cell.textLabel?.text = member.designation
return cell
}
关于ios - 如何制作自定义标题然后图像描述属于该标题并且在 TableView 中进行相同的过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41460694/