iOS中 自定义cell升级版 (高级)

本文介绍如何创建自定义的UITableViewCell,并实现动态加载数据及显示隐藏子TableView的功能。通过设置WGModel对象来配置单元格的内容,并根据数据调整高度。

接上次分享的自定义cell进行了优化:http://blog.youkuaiyun.com/qq_31810357/article/details/49611255

指定根视图:

[objc]  view plain  copy
  1. self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[[RootTableViewController alloc] initWithStyle:UITableViewStylePlain]];  

RootTableViewController.m

[objc]  view plain  copy
  1. #import "WGModel.h"  
  2. #import "WGCell.h"  
  3.   
  4. @interface RootTableViewController ()  
  5.   
  6. @property (nonatomicstrongNSMutableDictionary *dataDict;  
  7.   
  8. @end  
  9.   
  10. @implementation RootTableViewController  
  11.   
  12. - (void)viewDidLoad  
  13. {  
  14.     [super viewDidLoad];  
  15.       
  16.     self.dataDict = [NSMutableDictionary dictionary];  
  17.       
  18.     [self.tableView registerClass:[WGCell class] forCellReuseIdentifier:@"cell"];  
  19.     [self loadDataAndShow];  
  20. }  

请求数据:

[objc]  view plain  copy
  1. - (void)loadDataAndShow  
  2. {  
  3.     NSURL *url = [NSURL URLWithString:@"http://api.breadtrip.com/trips/2387133727/schedule/"];  
  4.     NSURLRequest *request = [NSURLRequest requestWithURL:url];  
  5.     [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {  
  6.          
  7.         if (data != nil) {  
  8.             NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];  
  9.             for (NSDictionary *dict in array) {  
  10.                 NSString *key = dict[@"date"];  
  11.                 NSArray *placesArray = dict[@"places"];  
  12.                 NSMutableArray *mutableArray = [NSMutableArray array];  
  13.                 for (NSDictionary *placesDict in placesArray) {  
  14.                     WGModel *model = [[WGModel alloc] init];  
  15.                     [model setValuesForKeysWithDictionary:placesDict];  
  16.                     model.isShow = NO;  
  17.                     [mutableArray addObject:model];  
  18.                 }  
  19.                 [self.dataDict setObject:mutableArray forKey:key];  
  20.             }  
  21.             [self.tableView reloadData];  
  22.         }  
  23.           
  24.           
  25.     }];  
  26. }  

#pragma mark - Table view data source

[objc]  view plain  copy
  1. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView  
  2. {  
  3.     return self.dataDict.allKeys.count;  
  4. }  
  5.   
  6. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  
  7. {  
  8.     NSString *key = self.dataDict.allKeys[section];  
  9.     return [self.dataDict[key] count];  
  10. }  
  11.   
  12.   
  13. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  14. {  
  15.     WGCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];  
  16.       
  17.     NSString *key = self.dataDict.allKeys[indexPath.section];  
  18.     NSMutableArray *mutableArray = self.dataDict[key];  
  19.     WGModel *model = mutableArray[indexPath.row];  
  20.     [cell configureCellWithModel:model];  
  21.       
  22.     if (model.isShow == YES) {  
  23.         [cell showTableView];  
  24.     } else {  
  25.           
  26.         [cell hiddenTableView];  
  27.     }  
  28.       
  29.     return cell;  
  30. }  

自适应高

[objc]  view plain  copy
  1. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     NSString *key = self.dataDict.allKeys[indexPath.section];  
  4.     NSMutableArray *mutableArray = self.dataDict[key];  
  5.     WGModel *model = mutableArray[indexPath.row];  
  6.     if (model.isShow) {  
  7.         return (model.pois.count + 1) * 44;  
  8.     } else {  
  9.         return 44;  
  10.     }  
  11. }  

点击cell会走的方法

[objc]  view plain  copy
  1. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     NSString *key = self.dataDict.allKeys[indexPath.section];  
  4.     NSMutableArray *mutableArray = self.dataDict[key];  
  5.     WGModel *model = mutableArray[indexPath.row];  
  6.     model.isShow = !model.isShow;  
  7.     [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];  
  8. }  

自定义cell

[objc]  view plain  copy
  1. //.h  
  2. #import <UIKit/UIKit.h>  
  3. @class WGModel;  
  4. @interface WGCell : UITableViewCell<UITableViewDataSource, UITableViewDelegate>  
  5.   
  6. @property (nonatomicstrongUILabel *aLabel;  
  7. @property (nonatomicstrongUITableView *tableView;  
  8.   
  9.   
  10. - (void)configureCellWithModel:(WGModel *)model;  
  11.   
  12. - (void)showTableView;  
  13. - (void)hiddenTableView;  
  14.   
  15. @end  
  16.   
  17. //.m  
  18. #import "WGCell.h"  
  19. #import "WGModel.h"  
  20.   
  21. @interface WGCell ()  
  22.   
  23. @property (nonatomicstrongNSMutableArray *dataArray;  
  24.   
  25. @end  
  26.   
  27. @implementation WGCell  
  28.   
  29.   
  30. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier  
  31. {  
  32.     if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {  
  33.         self.dataArray = [NSMutableArray array];  
  34.         [self addAllViews];  
  35.     }  
  36.     return self;  
  37. }  
  38.   
  39. - (void)addAllViews  
  40. {  
  41.     self.aLabel = [[UILabel alloc] initWithFrame:CGRectMake(00, [UIScreen mainScreen].bounds.size.width44)];  
  42.     self.aLabel.backgroundColor = [UIColor greenColor];  
  43.     [self.contentView addSubview:self.aLabel];  
  44.     self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(044, [UIScreen mainScreen].bounds.size.width0) style:UITableViewStylePlain];  
  45.       
  46.     self.tableView.delegate = self;  
  47.     self.tableView.dataSource = self;  
  48.     [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"testCell"];  
  49. //    [self.contentView addSubview:self.tableView];  
  50. }  
  51.   
  52. - (void)showTableView  
  53. {  
  54.     [self.contentView addSubview:self.tableView];  
  55. }  
  56.   
  57. - (void)hiddenTableView  
  58. {  
  59.     [self.tableView removeFromSuperview];  
  60. }  
  61.   
  62. - (void)configureCellWithModel:(WGModel *)model  
  63. {  
  64.     [self.dataArray removeAllObjects];  
  65.     self.aLabel.text = model.place[@"name"];  
  66.       
  67.     NSArray *array = model.pois;  
  68.     for (NSDictionary *dict in array) {  
  69.         NSString *str = dict[@"name"];  
  70.         [self.dataArray addObject:str];  
  71.     }  
  72.     CGRect frame = self.tableView.frame;  
  73.     frame.size.height = 444 * array.count;  
  74.     self.tableView.frame = frame;  
  75.     [self.tableView reloadData];  
  76.       
  77. }  
  78.   
  79.   
  80. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView  
  81. {  
  82.     return 1;  
  83. }  
  84.   
  85. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  
  86. {  
  87.     return self.dataArray.count;  
  88. }  
  89.   
  90. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  91. {  
  92.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"testCell" forIndexPath:indexPath];  
  93.     NSString *str = self.dataArray[indexPath.row];  
  94.     cell.textLabel.text = str;  
  95.     return cell;  
  96. }  

准备一个model类

[objc]  view plain  copy
  1. //.h  
  2. #import <Foundation/Foundation.h>  
  3.   
  4. @interface WGModel : NSObject  
  5.   
  6. @property (nonatomic, assign) BOOL isShow;  
  7. @property (nonatomicstrongNSDictionary *place;  
  8. @property (nonatomicstrongNSArray *pois;  
  9.   
  10. @end  
  11.   
  12.   
  13. //.m  
  14. #import "WGModel.h"  
  15.   
  16. @implementation WGModel  
  17.   
  18. - (void)setValue:(id)value forUndefinedKey:(NSString *)key  
  19. {  
  20.       
  21. }  
  22.   
  23. @end  

最终效果:



每日更新关注:http://weibo.com/hanjunqiang  新浪微博


原文地址:http://blog.youkuaiyun.com/qq_31810357/article/details/49847711
内容概要:本文介绍了ENVI Deep Learning V1.0的操作教程,重点讲解了如何利用ENVI软件进行深度学习模型的训练与应用,以实现遥感图像中特定目标(如集装箱)的自动提取。教程涵盖了从数据准备、标签图像创建、模型初始化与训练,到执行分类及结果优化的完整流程,并介绍了精度评价与通过ENVI Modeler实现一键化建模的方法。系统基于TensorFlow框架,采用ENVINet5(U-Net变体)架构,支持通过点、线、面ROI或分类图生成标签数据,适用于多/高光谱影像的单一类别特征提取。; 适合人群:具备遥感图像处理基础,熟悉ENVI软件操作,从事地理信息、测绘、环境监测等相关领域的技术人员或研究人员,尤其是希望将深度学习技术应用于遥感目标识别的初学者与实践者。; 使用场景及目标:①在遥感影像中自动识别和提取特定地物目标(如车辆、建筑、道路、集装箱等);②掌握ENVI环境下深度学习模型的训练流程与关键参数设置(如Patch Size、Epochs、Class Weight等);③通过模型调优与结果反馈提升分类精度,实现高效自动化信息提取。; 阅读建议:建议结合实际遥感项目边学边练,重点关注标签数据制作、模型参数配置与结果后处理环节,充分利用ENVI Modeler进行自动化建模与参数优化,同时注意软硬件环境(特别是NVIDIA GPU)的配置要求以保障训练效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值