IOS--UI 电影列表纯代码
2015-07-27 19:37
441 查看
还没写完就发现 自己之前的知识太多的不牢固 很多东西需要翻之前的代码
1.布局 因为是可以在多个界面切换 界面之前也是平级关系 我们就需要建立一个 UITabBarController 来控制 然后在 建立所需要的界面 继承 UITableViewController 我对于这个继承谁 不是特别的擅长
首先是电影列表 我的思想是:先布出主界面并有标签栏控制–>添加所需要 label 和 image 再利用 mode 赋值
② 为你的 movie 添加你需要的 cell 建立新的继承 UITableViewCell 的文件
③建立 mode 类
④ 所有句布好之后 就要到 movieTableViewCotroller 里面实现了
这样 我们电影界面就布好了了 给大家看我的图片
底部的青色的就是底图 你也可以设计自己喜欢的 只要在你布局的地方修改你的图片名字
这里就要注意了 如果你的图片不是苹果默认的”png” 那你的图片名字后面就要跟上你的图片格式
然后是我的界面
这个界面呢 就是大概模仿很多应用的界面 比如微信里的 我的 或者是聚美 视图电影之类的 就是用户界面
这个界面我打算用比较多的 botton 来响应不一样的事件 用 push 和模态来响应 但是想想模态好像不能用 我试试看吧
我又是自己给自己作死啊%>_<%
①万年不变的布局 这个继承 UIViewController 就可以 原因是 我们不需要很多 uitableviewController 自带的横线 也就说不需要 cell
我做了我的界面主要是 button 的应用
在签到和邀请码的地方我做了两个 push 的页面 一个是添加动态图片 另一个是添加 textFiled 做判断 但是我实在是想不起来怎么做了 就没有继续 写 下面是签到的点击事件
最后是活动界面 这个界面和电影列表的时候挺像 也是取数据 但是我还是犯了很多错误的导致 cell 不显示
发现自己一个毛病 就是喜欢把所有东西写完之后才查看自己的布局 ╮(╯▽╰)╭ 怪我咯 我实在是不知道什么情况下就可以先查看布局了啊
①布局
建立活动需要的 TableViewController 继承 TVC 再创建继承UITableViewCell 和 NSObject 的 model 类
在 cell 中布局
个人喜欢赋适应值 后期不会太麻烦
②Model 类
③ 要在 TVC 中展示了
④ 大概就是这个样子
总结:
1.做的好丑啊 这个东西大概花了我两天时间 很多地方需要别人来帮我检查
2.总归是有收获 再次加强了 如何剥离数据 布局 继承 赋值 model
3.还有很多功能没有实现 活动里面的图片 应该也是请求下来的才对 很多想法没有实现 自己真的太坑了
4.下面还有其他作业 继续努力吧
1.布局 因为是可以在多个界面切换 界面之前也是平级关系 我们就需要建立一个 UITabBarController 来控制 然后在 建立所需要的界面 继承 UITableViewController 我对于这个继承谁 不是特别的擅长
首先是电影列表 我的思想是:先布出主界面并有标签栏控制–>添加所需要 label 和 image 再利用 mode 赋值
//我直接把我所需要的界面都添加到了标签控制器 以后这个文件几乎没有用了 #import "RootViewController.h" #import "MineViewController.h" #import "HuodongViewController.h" #import "MovieTableViewController.h" @interface RootViewController ()<UITabBarControllerDelegate> @end @implementation RootViewController - (void)viewDidLoad { [super viewDidLoad]; [self configureBarController]; // Do any additional setup after loading the view. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(void)configureBarController{ // 创建单视图控制器 然后加入标签控制器 // Movie MovieTableViewController *movieTVC = [[MovieTableViewController alloc]init]; UINavigationController *movieTNV =[[UINavigationController alloc]initWithRootViewController:movieTVC]; movieTVC.title =@" 电影列表"; movieTNV.tabBarItem.image =[ UIImage imageNamed:@"45-movie1"]; // 活动 HuodongViewController *huodongVC =[[HuodongViewController alloc]init]; UINavigationController *huodongNV =[[UINavigationController alloc]initWithRootViewController:huodongVC]; huodongVC.title = @"活动"; huodongNV.tabBarItem.image =[UIImage imageNamed:@"12-eye"]; // Mine MineViewController *mineVC =[[MineViewController alloc]init ]; UINavigationController *mineNC =[[UINavigationController alloc]initWithRootViewController:mineVC]; mineVC.title=@"我的"; mineNC.tabBarItem.image =[UIImage imageNamed:@"51-outlet"]; // // 建立标签控制器 为什么这样就是错了? //这个地方我犯了一个错误 我又建立了一个继承 UITabBarController 的对象 那么这个对象和这个文件不是一个空间 就算是后面我将数组添加到里面 我的模拟机也是无法显示我布局好的一些 因为在 Appdelegate.m 文件里面的设定的根视图控制器是 RootViewController // UITabBarController *barC =[[UITabBarController alloc]init]; NSArray *controller =@[movieTNV,huodongNV,mineNC]; self.viewControllers = controller; // 配置标签栏的颜色 self.tabBar.barTintColor =[UIColor whiteColor]; // 被选中的 button 颜色 self.tabBar.tintColor = [UIColor cyanColor]; // 配置代理属性 self.delegate =self; [movieTVC release]; [movieTNV release]; [mineVC release]; [mineNC release]; [huodongVC release]; [huodongNV release]; }
② 为你的 movie 添加你需要的 cell 建立新的继承 UITableViewCell 的文件
.h #import <UIKit/UIKit.h> @class MovieListMode; @interface MovieListTableViewCell : UITableViewCell @property (nonatomic,retain)UILabel *rating; @property (nonatomic,retain)UILabel *pubdate; @property (nonatomic,retain)UILabel *title; @property (nonatomic,retain)UIImageView *viewBig; @property (nonatomic,retain)UIImageView *viewL; @property (nonatomic,retain)MovieListMode *listMode; @end
#import "MovieListTableViewCell.h" #import "MovieListMode.h" @implementation MovieListTableViewCell - (void)dealloc { [_pubdate release]; [_title release]; [_rating release]; [_viewBig release]; [_viewL release]; [super dealloc]; } //重写父类方法 -(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { [self p_setUp]; }return self; } //重写setting 为什么呢? 我们需要赋值 而这些值 系统没有 所以要重新写 -(void)setListMode:(MovieListMode *)listMode{ if (_listMode !=listMode) { [_listMode release]; _listMode = [listMode retain]; } self.title.text =listMode.title; self.pubdate.text =listMode.pubdate; self.rating.text = listMode.rating; } // 这就是布局 -(void)p_setUp{ //底图 self.viewBig =[[UIImageView alloc]initWithFrame:CGRectMake(0, 10, 310, 130)]; self.viewBig.image =[UIImage imageNamed:@"bg_eventlistcell"]; [self.contentView addSubview:self.viewBig]; [self.viewBig release]; // 海报 self.viewL =[[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetMinX(self.viewBig.frame)+5, CGRectGetMinY(self.viewBig.frame), CGRectGetWidth(self.viewBig.frame)/3, CGRectGetHeight(self.viewBig.frame)-20)]; self.viewL.image =[UIImage imageNamed:@"picholder"]; [self.viewBig addSubview:self.viewL]; [self.viewL release]; // title self.title = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.viewL.frame)+10, CGRectGetMinY(self.viewL.frame)+15, CGRectGetWidth(self.viewBig.frame)/2, CGRectGetHeight(self.viewL.frame)/4)]; // self.title.backgroundColor =[UIColor whiteColor]; [self.contentView addSubview: self.title]; [self.title release]; // 等级 self.rating = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMinX(self.title.frame), CGRectGetMaxY(self.title.frame)+10, CGRectGetWidth(self.title.frame), CGRectGetHeight(self.title.frame))]; // self.rating.backgroundColor =[UIColor whiteColor]; [self.contentView addSubview:self.rating]; [self.rating release]; // 上映时间 self.pubdate =[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMinX(self.title.frame), CGRectGetMaxY(self.rating.frame)+10, CGRectGetWidth(self.title.frame), CGRectGetHeight(self.title.frame))]; // self.pubdate.backgroundColor =[UIColor whiteColor]; [self.contentView addSubview:self.pubdate]; } //注:这里有一个需要注意的地方 如果你有底图 那么你就需要最先布局这个 因为如果你全部都是添加到 self.contentView 上面 他就会有覆盖效果 可以看我的海报地方 我是添加到了底图上面 也可以这样写
③建立 mode 类
#import <Foundation/Foundation.h> @interface MovieListMode : NSObject @property (nonatomic,copy)NSString *pubdate; @property (nonatomic,copy)NSString *title; @property (nonatomic,copy)NSString *rating; @property (nonatomic,copy)NSString *viewBig; @property (nonatomic,copy) NSString *viewLittle; @end
#import "MovieListMode.h" @implementation MovieListMode - (void)dealloc { [_pubdate release]; [_title release]; [_viewBig release]; [_viewLittle release]; [_rating release]; [super dealloc]; } //重申:这个地方 是防止你没有值却继续赋值的崩溃情况 -(void)setValue:(id)value forUndefinedKey:(NSString *)key{ }
④ 所有句布好之后 就要到 movieTableViewCotroller 里面实现了
.h 中没什么好写的 我们直接进入.m 中编写 #import "MovieTableViewController.h" #import "MovieListTableViewCell.h" #import "MovieListMode.h" @interface MovieTableViewController () @property (nonatomic,retain)NSMutableArray *dataSourse; @end @implementation MovieTableViewController - (void)viewDidLoad { [super viewDidLoad]; //这里面是你的方法调用 你写的再好 不在这里调用方法没有一点 P 用 [self configureContact]; //注册 cell 的方法 如果你是用 sb 写的那么这个地方就可以不用要了 你也可以写宏 [self.tableView registerClass:[MovieListTableViewCell class] forCellReuseIdentifier:@"cell"]; } //这里面是数据的方法实现 -(void)configureContact{ //每走一次 就会把之前的数据清空 让他保持干净的状态 这样不会造成数据重复挤压 self.dataSourse =nil; // 找到地址 NSString *filePath =[[NSBundle mainBundle]pathForResource:@"movie" ofType:@"json"]; // 创建需要的数据 对象 NSData *data =[NSData dataWithContentsOfFile:filePath]; // 解析数据 最外层 是个字典 NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil]; NSMutableArray *arr= dic[@"entries"]; // 剥离数据 //这个地方的开辟空间非常重要 如果你没写那么你取出来的数据没地方放 你的 cell 上面就不会显示东西 self.dataSourse = [NSMutableArray arrayWithCapacity:0]; for (NSDictionary *d in arr) { //这个地方就体现了 mode 类的好处 取出之后直接封装到 model 里面 MovieListMode *listMode =[[MovieListMode alloc]init]; [listMode setValuesForKeysWithDictionary:d ]; [self.dataSourse addObject:listMode]; [listMode release]; } [self.tableView reloadData]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { #warning Potentially incomplete method implementation. // Return the number of sections. return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { #warning Incomplete method implementation. // Return the number of rows in the section. return self.dataSourse.count; } //重用 cell 的方法 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { MovieListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath]; MovieListMode *listMode =self.dataSourse [indexPath.row]; cell.listMode = listMode; // Configure the cell... return cell; } //返回 cell 的高度 我们如果没有新闻类的 可以直接看着模拟器来设定好返回值 但是如果需要 cell 自适应就需要写新的方法 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 150; }
这样 我们电影界面就布好了了 给大家看我的图片
底部的青色的就是底图 你也可以设计自己喜欢的 只要在你布局的地方修改你的图片名字
这里就要注意了 如果你的图片不是苹果默认的”png” 那你的图片名字后面就要跟上你的图片格式
然后是我的界面
这个界面呢 就是大概模仿很多应用的界面 比如微信里的 我的 或者是聚美 视图电影之类的 就是用户界面
这个界面我打算用比较多的 botton 来响应不一样的事件 用 push 和模态来响应 但是想想模态好像不能用 我试试看吧
我又是自己给自己作死啊%>_<%
①万年不变的布局 这个继承 UIViewController 就可以 原因是 我们不需要很多 uitableviewController 自带的横线 也就说不需要 cell
我做了我的界面主要是 button 的应用
.h #import <UIKit/UIKit.h> @interface MineViewController : UIViewController @property (nonatomic,retain)UIButton *sigin; @property (nonatomic,retain)UIButton *exchange; @property (nonatomic,retain)UIButton *refer; @property (nonatomic,retain)UIButton *about; @property(nonatomic,retain)UIImageView *photo; @property (nonatomic,retain)UILabel *name; @end
. m #import "MineViewController.h" #import "SiginViewController.h" #import "ExchangeViewController.h" @interface MineViewController () @end @implementation MineViewController - (void)dealloc { [_about release]; [_exchange release]; [_refer release]; [_sigin release]; [_photo release]; [_name release]; [super dealloc]; } - (void)viewDidLoad { [super viewDidLoad]; // self.view.backgroundColor =[UIColor cyanColor]; [self configureMine]; // Do any additional setup after loading the view. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } //这方法要记得调用 -(void)configureMine{ //头像 self.photo =[[UIImageView alloc]initWithFrame:CGRectMake(20, 64, 100, 100)]; self.photo.image = [UIImage imageNamed:@"u=3212426248,1810221190&fm=21&gp=0.jpg"]; self.photo.backgroundColor = [UIColor cyanColor]; [self.photo.layer setMasksToBounds:YES]; [self.photo.layer setCornerRadius:50]; [self.view addSubview:self.photo]; [self.photo release]; // name self.name =[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.photo.frame)+50, CGRectGetMinY(self.photo.frame)+20, CGRectGetWidth(self.view.frame)/2, CGRectGetHeight(self.photo.frame)/2)]; self.name.text = @"Nyx"; self.name.backgroundColor =[UIColor whiteColor]; [self.view addSubview:self.name]; [self.name release]; // 签到 sigin self.sigin =[UIButton buttonWithType:(UIButtonTypeSystem)]; self.sigin.frame =CGRectMake(CGRectGetMinX(self.photo.frame), CGRectGetMaxY(self.photo.frame)+80, CGRectGetWidth(self.view.frame)-40, 50); [self.sigin setTitle:@"每日签到,获大奖" forState:UIControlStateNormal]; self.sigin.backgroundColor =[UIColor whiteColor]; [self.sigin addTarget:self action:@selector(handleSigin:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:self.sigin]; // 兑换 exchange self.exchange =[UIButton buttonWithType:(UIButtonTypeSystem)]; self.exchange.frame =CGRectMake(CGRectGetMinX(self.sigin.frame), CGRectGetMaxY(self.sigin.frame), CGRectGetWidth(self.sigin.frame), CGRectGetHeight(self.sigin.frame)); [self.exchange addTarget:self action:@selector(handleExchange:) forControlEvents:UIControlEventTouchUpInside]; [self.exchange setTitle:@"邀请码兑换" forState:UIControlStateNormal]; self.exchange .backgroundColor =[UIColor whiteColor]; [self.view addSubview:self.exchange]; NSLog(@"123"); [self.exchange release]; // refer 咨询 self.refer =[UIButton buttonWithType:(UIButtonTypeSystem)]; self.refer .frame =CGRectMake(CGRectGetMinX(self.sigin.frame), CGRectGetMaxY(self.exchange.frame), CGRectGetWidth(self.sigin.frame), CGRectGetHeight(self.sigin.frame)); [self. refer addTarget:self action:@selector(handleRefer:) forControlEvents:UIControlEventTouchUpInside]; [self.refer setTitle:@"售前咨询" forState:UIControlStateNormal]; self.refer .backgroundColor =[UIColor whiteColor]; [self.view addSubview:self.refer]; [self.refer release]; // about self. about =[UIButton buttonWithType:(UIButtonTypeSystem)]; self.about .frame =CGRectMake(CGRectGetMinX(self.sigin.frame), CGRectGetMaxY(self.refer.frame), CGRectGetWidth(self.sigin.frame), CGRectGetHeight(self.sigin.frame)); [self. about addTarget:self action:@selector(handleAbout:) forControlEvents:UIControlEventTouchUpInside]; [self.about setTitle:@"关于我们" forState:UIControlStateNormal]; self.about .backgroundColor =[UIColor whiteColor]; [self.view addSubview:self.about]; [self.about release]; } // 下面是点击 Button 的响应事件 -(void)handleSigin:(UIButton *)sender{ SiginViewController *sigin =[[SiginViewController alloc]init]; [self.navigationController pushViewController:sigin animated:YES]; [sigin release]; } -(void)handleExchange:(UIButton *)sender{ ExchangeViewController *exchangeVC =[[ExchangeViewController alloc]init]; [self.navigationController pushViewController:exchangeVC animated:YES]; [exchangeVC release]; } -(void)handleRefer:(UIButton *)sender{ UIAlertView *aler = [[UIAlertView alloc]initWithTitle:@"亲" message:@"不是服务时间哟" delegate:nil cancelButtonTitle:@"离开" otherButtonTitles:@"忽略", nil]; [aler show]; [aler release]; } -(void)handleAbout:(UIButton *)sender{ UIAlertView *aler =[[UIAlertView alloc]initWithTitle:@"神秘人" message:@"寒冬将要到来" delegate:nil cancelButtonTitle:@"离开这里" otherButtonTitles:nil, nil]; [aler show]; [aler release]; } @end
在签到和邀请码的地方我做了两个 push 的页面 一个是添加动态图片 另一个是添加 textFiled 做判断 但是我实在是想不起来怎么做了 就没有继续 写 下面是签到的点击事件
.m #import "SiginViewController.h" @interface SiginViewController () @end @implementation SiginViewController - (void)dealloc { [_titles release]; [_imageV release]; [super dealloc]; } - (void)viewDidLoad { [super viewDidLoad]; [self configuerSigin]; self.view .backgroundColor =[UIColor cyanColor]; // Do any additional setup after loading the view. } -(void)configuerSigin{ self.titles =[[UILabel alloc]initWithFrame:CGRectMake(100, 170, 260, 100)]; self.titles.textColor =[UIColor redColor]; self.titles.text =@"当前签到获得1元"; self.titles.backgroundColor =[UIColor cyanColor]; [self.view addSubview:self.titles]; [self.title release]; // 加载动态图 NSMutableArray *arrImage =[NSMutableArray arrayWithCapacity:9]; //如果你这个地方 i 设的是1 那么下面的 i 就不要再加1 数组是有序的 他不可能跳过一个元素 走 for (int i =1; i <=8; i++) { NSString *fileName =[NSString stringWithFormat:@"321-%d.tiff",i]; UIImage *image = [UIImage imageNamed:fileName]; [arrImage addObject:image]; } self.imageV= [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 300, 500)]; //动态图循环的次数 0是无限大 self.imageV .animationRepeatCount = 0; //循环播放的图片的数组 self.imageV.animationImages = arrImage; // 设置动态度播放的速度 self.imageV.animationDuration = 1; //添加到父视图 [self.view addSubview:self.imageV]; [self.imageV release]; [self.imageV startAnimating]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
最后是活动界面 这个界面和电影列表的时候挺像 也是取数据 但是我还是犯了很多错误的导致 cell 不显示
发现自己一个毛病 就是喜欢把所有东西写完之后才查看自己的布局 ╮(╯▽╰)╭ 怪我咯 我实在是不知道什么情况下就可以先查看布局了啊
①布局
建立活动需要的 TableViewController 继承 TVC 再创建继承UITableViewCell 和 NSObject 的 model 类
在 cell 中布局
HuodongTableViewCell.h #import <UIKit/UIKit.h> @class HuodongMode; @interface HuodongTableViewCell : UITableViewCell @property (nonatomic,retain)UIImageView *backgroundImage; @property (nonatomic,retain)UILabel *title; @property (nonatomic,retain)UILabel *begin_time; @property (nonatomic,retain)UILabel *end_time; @property (nonatomic,retain)UILabel *address; @property (nonatomic,retain)UILabel *category_name; @property (nonatomic,retain)UILabel *participant_count; @property (nonatomic,retain)UILabel *wisher_count; @property (nonatomic,retain)UIImageView *imageView1; @property (nonatomic,retain)HuodongMode *huodongM; @end
HuodongTableViewCell.m #import "HuodongTableViewCell.h" #import "HuodongMode.h" @implementation HuodongTableViewCell - (void)dealloc { [_title release]; [_begin_time release]; [_end_time release]; [_address release]; [_category_name release]; [_participant_count release]; [_wisher_count release]; [_imageView1 release]; [_backgroundImage release]; [super dealloc]; } - (void)awakeFromNib { // Initialization code } //初始化父类方法 - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { [self p_setUp]; }return self; } //重写 setting 方法 重新赋值 -(void)setHuodongM:(HuodongMode *)huodongM{ if (_huodongM !=huodongM) { [_huodongM release]; _huodongM = [huodongM retain]; } self.title.text =huodongM.title; self.begin_time.text =huodongM.begin_time; self.end_time.text = huodongM.end_time; self.begin_time.text =huodongM.begin_time; self.category_name.text =huodongM.category_name; } -(void)p_setUp{ // 标题 self.title = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, 320, 40)]; // self.title.backgroundColor =[UIColor redColor]; [self.contentView addSubview:self.title]; [self.title release]; //活动类型 self.category_name = [[UILabel alloc]initWithFrame:CGRectMake(10, 50, CGRectGetWidth(self.title.frame)-20, CGRectGetHeight(self.title.frame))]; // self.category_name .backgroundColor =[UIColor cyanColor]; [self.contentView addSubview:self.category_name ]; [self.category_name release]; // 开始 结束时间 self.begin_time = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMinX(self.category_name.frame), CGRectGetMaxY(self.category_name.frame)+10, CGRectGetWidth(self.category_name.frame)/2, CGRectGetHeight(self.category_name.frame))]; // self.begin_time .backgroundColor =[UIColor cyanColor]; [self.contentView addSubview:self.begin_time]; [self.begin_time release]; self.end_time =[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.begin_time.frame), CGRectGetMinY(self.begin_time.frame), CGRectGetWidth(self.begin_time.frame), CGRectGetHeight(self.begin_time.frame))]; // self.end_time .backgroundColor =[UIColor cyanColor]; [self.contentView addSubview:self.end_time]; [self.end_time release]; // 参加人数和感兴趣人数 //participant_count:参加人数 self.participant_count= [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMinX(self.begin_time.frame), CGRectGetMaxY(self.begin_time.frame)+10, CGRectGetWidth(self.begin_time.frame), CGRectGetHeight(self.begin_time.frame))]; // self.participant_count .backgroundColor =[UIColor cyanColor]; [self.contentView addSubview:self.participant_count]; [self.participant_count release]; //wisher_count:感兴趣人数 self.wisher_count= [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.participant_count.frame), CGRectGetMinY(self.participant_count.frame), CGRectGetWidth(self.begin_time.frame), CGRectGetHeight(self.begin_time.frame))]; // self.wisher_count .backgroundColor =[UIColor cyanColor]; [self.contentView addSubview:self.wisher_count]; [self.wisher_count release]; // 地址 self.address = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMinX(self.participant_count.frame), CGRectGetMaxY(self.participant_count.frame)+10, CGRectGetWidth(self.title.frame), CGRectGetHeight(self.title.frame))]; // self.address.backgroundColor =[UIColor redColor]; [self.contentView addSubview:self.address]; [self.address release]; // 图片 self.imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.title.frame)/2, CGRectGetMaxY(self.title.frame), CGRectGetWidth(self.title.frame)/2, 50)]; [self.contentView addSubview:self.imageView1]; [self.imageView1 release]; } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } @end
个人喜欢赋适应值 后期不会太麻烦
②Model 类
#import <Foundation/Foundation.h> @interface HuodongMode : NSObject //model 里的属性要写在. h 中公开的 这是我犯的一个错误 因为之前 title 我使用过之后不能在找到 有朋友告诉我说你定义在公开的之后 再使用 系统分不清谁是谁 那我就想这次定在私有的 但是在 TVC 中 我点不出来 这里出了错 @property (nonatomic,copy)NSString *title; @property (nonatomic,copy)NSString *begin_time; @property (nonatomic,copy)NSString *end_time; @property (nonatomic,copy)NSString *address; @property (nonatomic,copy)NSString *category_name; @property (nonatomic,assign)NSInteger participant_count; @property (nonatomic,assign)NSInteger wisher_count; @property (nonatomic,copy)NSString *imageVi; @end #import "HuodongMode.h" @implementation HuodongMode - (void)dealloc { [_title release]; [_begin_time release]; [_end_time release]; [_address release]; [_category_name release]; [_imageVi release]; [super dealloc]; } //防止崩溃 -(void)setValue:(id)value forUndefinedKey:(NSString *)key{ } //这个可以打印你的数据 不会出现地址符那样的编码 也可检测你的数据是否能打印 有的时候 不一定打印出编码就是取到了数据 - (NSString *)description { return [NSString stringWithFormat:@"%@,\n%@,\n%@,\n%@,\n%@,\n%@", _title,_begin_time,_end_time,_address,_category_name,_imageVi]; }
③ 要在 TVC 中展示了
#import "HuodongTableViewController.h" #import "HuodongMode.h" #import "HuodongTableViewCell.h" @interface HuodongTableViewController () @property (nonatomic,retain)NSMutableArray *dataSourse; @end @implementation HuodongTableViewController - (void)viewDidLoad { [super viewDidLoad]; [self configuerHuodong]; //不是 sb 拖出来的 cell 需要注册 [self.tableView registerClass:[HuodongTableViewCell class] forCellReuseIdentifier:@"huodongCell"]; // Do any additional setup after loading the view. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } // 整个下来 这一个方法是最最重要的 剥离json 中的数据 练习很多次 还是犯了错误 -(void)configuerHuodong{ self.dataSourse =nil; // 获取文件地址 NSString *fliePath =[[NSBundle mainBundle]pathForResource:@"ActivityList" ofType:@"json"]; // 文件里面有汉字 转码 NSData *data =[NSData dataWithContentsOfFile:fliePath]; NSDictionary *dic =[NSJSONSerialization JSONObjectWithData:data options:NSUTF8StringEncoding error:nil]; NSMutableArray *mutArr =dic[@"events"]; // NSLog(@"%@",mutArr); self.dataSourse = [NSMutableArray arrayWithCapacity:0]; for (NSDictionary *d in mutArr) { HuodongMode *huodongM =[[HuodongMode alloc]init]; [huodongM setValuesForKeysWithDictionary:d]; // NSLog(@"%@",huodongM); [self.dataSourse addObject:huodongM]; [huodongM release]; } [self.tableView reloadData]; } #pragma mark - Table view data source //cell 分区个数 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { #warning Potentially incomplete method implementation. // Return the number of sections. return 1; } // cell row 的个数 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { #warning Incomplete method implementation. // Return the number of rows in the section. return self.dataSourse.count; } // - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { HuodongTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"huodongCell" forIndexPath:indexPath]; HuodongMode *huodongM =self.dataSourse [indexPath.row]; //这个地方我没有用像电影列表一样的赋值方法 是因为如果你那样打印 你的 NSString 属性的很多东西 会打不出来 而且我们还需要添加一些字给与解释说明 但是这样想想 我的 MOdel 类 里的赋值 好像没什么用╮(╯▽╰)╭ 不过 也不错 // cell.huodongM.title = huodongM.title; // Configure the cell... cell.category_name.text= [NSString stringWithFormat:@"类型:%@",huodongM.category_name]; cell.wisher_count.text = [NSString stringWithFormat:@"感兴趣:% ld",huodongM.wisher_count]; cell.participant_count.text =[NSString stringWithFormat:@"参加:% ld",huodongM.participant_count]; cell.title.text = huodongM.title; cell.begin_time.text = [NSString stringWithFormat:@"开始:%@",huodongM.begin_time]; cell.end_time.text = [NSString stringWithFormat:@"结束:%@",huodongM.end_time]; cell.address.text = [NSString stringWithFormat:@"地址:%@",huodongM.address]; cell.imageView1.image = [UIImage imageNamed:@"picholder"]; return cell; } //返还 cell 的高度 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 250; } @end
④ 大概就是这个样子
总结:
1.做的好丑啊 这个东西大概花了我两天时间 很多地方需要别人来帮我检查
2.总归是有收获 再次加强了 如何剥离数据 布局 继承 赋值 model
3.还有很多功能没有实现 活动里面的图片 应该也是请求下来的才对 很多想法没有实现 自己真的太坑了
4.下面还有其他作业 继续努力吧
相关文章推荐
- EasyUI TextBox的keypress
- iOS UI篇 学习笔记 UILabel
- PAT (Advanced Level) 1056. Mice and Rice (25) 模拟比赛,用queue辅助
- 初学iOS UI 用于交流与学习
- UESTC 94 Bracket Sequence(线段树的区间更新)
- PAT 1099. Build A Binary Search Tree (30)
- flash builder 配置中如果有中文会报错
- iOS - UI-归档解归档
- POJ 2785 4 Values whose Sum is 0 (对半分解 二分搜索)
- leetcode 060 —— Permutation Sequence
- hdu 5306 Gorgeous Sequence
- Intellij IDEA 14.x 菜单项中Compile、Make和Build的区别
- 【读书笔记】UIWindow-WindowLevel
- 【读书笔记】UIWindow-WindowLevel
- codeforce Gym 100500H ICPC Quest (简单dp)
- Unique Binary Search Trees
- UITableViewRowAction的使用
- html元素li的value属性
- iOS-UI-01 UIWindow UIView
- hdu5033 Building 单调队列