ios - 如何制作自定义标题然后图像描述属于该标题并且在 TableView 中进行相同的过程

标签 ios objective-c swift xcode uitableview

我正在构建一个项目,我必须制作一个表格 View ,其中我必须包含三个组的图像及其名称和位置。 组别如下:(1).OFFICE BEARERS (2).MEMBERS (3).CO-OPTED MEMBERS.

我想将组设为标题,然后显示与该组相对应的成员的图像以及他们的名称和职务。然后另一个组作为标题和所属成员的图像他们。等等。示例图片如下所示

每个组都有很多成员,所以我想使用动态单元格。

这样做的最佳做法是什么......请帮助我


注意:图像位于我的项目 Assets 中。 tableview 的图像是这样的..


enter image description here

最佳答案

准备一个数据结构,例如像这样:

[ 
      {
         "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/

相关文章:

iphone - 在另一个 View 中呈现 modalViewController

ios - NSURLConnection 取消回调

ios - 将数组转换为 SwiftyJSON

ios - UILabel 行间距

ios - Google OAuth 2.0 invalid_client 未经授权

objective-c - 更改 UIViewController self.view 框架

ios - 如何从minimumLineSpacingForSectionAt获取IndexPath以设置collectionview中项目之间的不同间距?

iphone - 在uitableview中禁用橡皮筋滚动

iphone - NSOperation 和 CoreData 线程

ios - UIView 在滚动时从 UICollectionView 的底部分离