iOS中 自定义cell升级版 (高级)
接上次分享的自定义cell进行了优化:http://blog.csdn.net/qq_31810357/article/details/49611255
指定根视图:
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[[RootTableViewController alloc] initWithStyle:UITableViewStylePlain]];
RootTableViewController.m
#import "WGModel.h"
#import "WGCell.h"
@interface RootTableViewController ()
@property (nonatomic, strong) NSMutableDictionary *dataDict;
@end
@implementation RootTableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.dataDict = [NSMutableDictionary dictionary];
[self.tableView registerClass:[WGCell class] forCellReuseIdentifier:@"cell"];
[self loadDataAndShow];
}
请求数据:
- (void)loadDataAndShow
{
NSURL *url = [NSURL URLWithString:@"http://api.breadtrip.com/trips/2387133727/schedule/"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (data != nil) {
NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
for (NSDictionary *dict in array) {
NSString *key = dict[@"date"];
NSArray *placesArray = dict[@"places"];
NSMutableArray *mutableArray = [NSMutableArray array];
for (NSDictionary *placesDict in placesArray) {
WGModel *model = [[WGModel alloc] init];
[model setValuesForKeysWithDictionary:placesDict];
model.isShow = NO;
[mutableArray addObject:model];
}
[self.dataDict setObject:mutableArray forKey:key];
}
[self.tableView reloadData];
}
}];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.dataDict.allKeys.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSString *key = self.dataDict.allKeys[section];
return [self.dataDict[key] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
WGCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
NSString *key = self.dataDict.allKeys[indexPath.section];
NSMutableArray *mutableArray = self.dataDict[key];
WGModel *model = mutableArray[indexPath.row];
[cell configureCellWithModel:model];
if (model.isShow == YES) {
[cell showTableView];
} else {
[cell hiddenTableView];
}
return cell;
}
自适应高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *key = self.dataDict.allKeys[indexPath.section];
NSMutableArray *mutableArray = self.dataDict[key];
WGModel *model = mutableArray[indexPath.row];
if (model.isShow) {
return (model.pois.count + 1) * 44;
} else {
return 44;
}
}
点击cell会走的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *key = self.dataDict.allKeys[indexPath.section];
NSMutableArray *mutableArray = self.dataDict[key];
WGModel *model = mutableArray[indexPath.row];
model.isShow = !model.isShow;
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
自定义cell
//.h
#import <UIKit/UIKit.h>
@class WGModel;
@interface WGCell : UITableViewCell<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UILabel *aLabel;
@property (nonatomic, strong) UITableView *tableView;
- (void)configureCellWithModel:(WGModel *)model;
- (void)showTableView;
- (void)hiddenTableView;
@end
//.m
#import "WGCell.h"
#import "WGModel.h"
@interface WGCell ()
@property (nonatomic, strong) NSMutableArray *dataArray;
@end
@implementation WGCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
self.dataArray = [NSMutableArray array];
[self addAllViews];
}
return self;
}
- (void)addAllViews
{
self.aLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44)];
self.aLabel.backgroundColor = [UIColor greenColor];
[self.contentView addSubview:self.aLabel];
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 44, [UIScreen mainScreen].bounds.size.width, 0) style:UITableViewStylePlain];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"testCell"];
// [self.contentView addSubview:self.tableView];
}
- (void)showTableView
{
[self.contentView addSubview:self.tableView];
}
- (void)hiddenTableView
{
[self.tableView removeFromSuperview];
}
- (void)configureCellWithModel:(WGModel *)model
{
[self.dataArray removeAllObjects];
self.aLabel.text = model.place[@"name"];
NSArray *array = model.pois;
for (NSDictionary *dict in array) {
NSString *str = dict[@"name"];
[self.dataArray addObject:str];
}
CGRect frame = self.tableView.frame;
frame.size.height = 44 * array.count;
self.tableView.frame = frame;
[self.tableView reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"testCell" forIndexPath:indexPath];
NSString *str = self.dataArray[indexPath.row];
cell.textLabel.text = str;
return cell;
}
准备一个model类
//.h
#import <Foundation/Foundation.h>
@interface WGModel : NSObject
@property (nonatomic, assign) BOOL isShow;
@property (nonatomic, strong) NSDictionary *place;
@property (nonatomic, strong) NSArray *pois;
@end
//.m
#import "WGModel.h"
@implementation WGModel
- (void)setValue:(id)value forUndefinedKey:(NSString *)key
{
}
@end
最终效果:
每日更新关注:http://weibo.com/hanjunqiang
新浪微博
iOS中 自定义cell升级版 (高级)的更多相关文章
- ios中自定义cell 设置cell的分组结构
ios系统默认的cell并不能满足我们的需求 这个时候就需要自定义我们的cell 自定义cell为分组的时候 需要设置分组样式 以下是我常用分组的二种方法: 第一是 在自定义的UITableView ...
- iOS中 自定义cell分割线/分割线偏移 韩俊强的博客
在项目开发中我们会常常遇到tableView 的cell分割线显示不全,左边会空出一截像素,更有甚者想改变系统的分割线,并且只要上下分割线的一个等等需求,今天重点解决以上需求,仅供参考: 每日更新关注 ...
- iOS 中自定义 cell,点击cell的时候文字不出现的原因
解决方案: 在setSelected方法中设置要显示label的背景颜色即可
- ios中自定义tableView,CollectionView的cell什么时候用nib加载,什么时候用标识重用
做了一段时间的iOS,在菜鸟的路上还有很长的路要走,把遇到的问题记下来,好记性不如烂笔头. 在项目开发中大家经常会用到tableView和collectionView两个控件,然而在cell的自定义上 ...
- ios之UI中自定义cell
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- iOS中 UITableViewCell cell划线那些事 韩俊强的博客
每日更新关注:http://weibo.com/hanjunqiang 在开发中经常遇到cell分割线显示不全或者想自定义线的宽高等; 最近总结了一下,希望帮到大家: 1.不想划线怎么办? Table ...
- ios中自定义checkbox
//自定义button#import <UIKit/UIKit.h> @interface CKButton : UIButton @end #import "CKButton. ...
- iOS 获取自定义cell上按钮所对应cell的indexPath.row的方法
在UITableView或UICollectionView的自定义cell中创建一button,在点击该按钮时知道该按钮所在的cell在UITableView或UICollectionView中的行数 ...
- iOS中自定义UITableViewCell的用法
1.先创建一个View继承 UITableViewCell并使用xib快速建立模型. #import <UIKit/UIKit.h> #import "Score.h" ...
随机推荐
- nginx 网络模型,cpu亲和等优点
nginx优点1.IO多路复用epollIO多路复用:多个描述符的I/O操作都能在一个线程内并发交替地顺序完成,这里的"复用" 指的是复用同一个线程epollIO多路复用的实现方式 ...
- 关于html+ashx开发中几个问题的解决方法
在跟html+ashx打交道的园友们肯定会发现,这种模式虽然优美,但在开发中会遇到一些难处理的地方.我也不例外,下面是自己在实际开发中总结出来的几条经验,希望跟大家分享,更希望得到大家的建议和更好的解 ...
- Win10 下Cmake编译配置 Opencv3.1 + Cuda7.5 + VS2013
折腾了三天终于配置成功了,在此写下编译配置的全部步骤和遇到的很多坑. 整体介绍: OpenCV 中 CUDA 实现的函数还不是太多,使用前要在OpenCV的官网上确认以下你想要的功能是否已经实现,否则 ...
- javascript 中如何判断是否是JSON格式的字符串
var stringToJson = function(value){ try{ eval('('+value+')'); return angular.fromJson(value); } catc ...
- CSS中display:block属性的作用
display:block可以理解为块,举个简单的例子!比如你做一个超链接,<li><a href="#">超链接</a></li> ...
- /usr,/usr/local/ 还是 /opt ?
Linux 的软件安装目录是也是有讲究的,理解这一点,在对系统管理是有益的(好吧处女座表示完全不能接受不正确的路径选择,看着会不舒服的……) /usr:系统级的目录,可以理解为C:/Windows/, ...
- 手把手教你做一个Shell命令窗口
这是一个类似于win下面的cmd打开后的窗口,可以跨平台使用,可以在win和linux下面同时使用,主要功能如下: 首先我们需要把这些功能的目录写出来,通过写一个死循环,让其每次回车之后都可以保持同样 ...
- 理解性能的奥秘——应用程序中慢,SSMS中快(5)——案例:如何应对参数嗅探
本文属于<理解性能的奥秘--应用程序中慢,SSMS中快>系列 接上文:理解性能的奥秘--应用程序中慢,SSMS中快(4)--收集解决参数嗅探问题的信息 首先我们需要明白,参数嗅探本身不是问 ...
- EJB开发第一个无状态会话bean、开发EJB客户端
开发第一个无状态会话bean EJB中的三中bean: 会话Bean(Session Bean) 负责与客户端交互,是编写业务逻辑的地方,在会话bean中可以通过JDBC直接操作数据库,但大多数情况下 ...
- hive分组排序 取top N
pig可以轻松获取TOP n.书上有例子 hive中比较麻烦,没有直接实现的函数,可以写udf实现.还有个比较简单的实现方法: 用row_number,生成排名序列号.然后外部分组后按这个序列号多虑, ...