iOS 代码自定义cell示例
2015-08-01 22:48
513 查看
底色标黄为代码自定义cell重点处,入手从这几点即可。
MyCell.h
MyCell.m
控制器.m
MyCell.h
#import <UIKit/UIKit.h> @interface MyCell :UITableViewCell @property(nonatomic,strong)UILabel *ageLabel; @property(nonatomic,strong)UILabel *nameLabel; @property(nonatomic,strong)UILabel *additionLabel; - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier; @end
MyCell.m
#import "MyCell.h" @implementation MyCell - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { if (self = [superinitWithStyle:stylereuseIdentifier:reuseIdentifier]) { // for (UIView *view in self.contentView.subviews) { // NSLog(@"%@",view); // [view removeFromSuperview]; // } _ageLabel = [[UILabelalloc]init]; _nameLabel = [[UILabelalloc]init]; _additionLabel = [[UILabelalloc]init]; _additionLabel.text =@"点击"; [self.contentViewaddSubview:_ageLabel]; [self.contentViewaddSubview:_nameLabel]; [self.contentViewaddSubview:_additionLabel]; self.backgroundColor = [UIColorclearColor]; } return self; } - (void)layoutSubviews { [superlayoutSubviews]; //如不调用父类此方法就会导致在使用时出现分隔线位置不正确,主要是因为父类中会调用其分隔线位置调整 self.nameLabel.frame =CGRectMake(0,0,100,44); self.ageLabel.frame =CGRectMake(100,0,100,44); self.additionLabel.frame =CGRectMake(0,40,320,20); } @end
控制器.m
#import "ViewController.h" #import "ConcernViewController.h" #import "MyCell.h" @interface ViewController ()<UITableViewDataSource,UITableViewDelegate,ConcernViewContorllDelegate> @property(nonatomic,strong)UITableView *myTableView; @property(nonatomic,strong)NSMutableArray *myData; @end @implementation ViewController - (NSMutableArray *)myData { if (!_myData) {//如果想修改字典中的值,字典必须为可变字典不然就会出现问题 NSMutableDictionary *dict1 = [NSMutableDictionarydictionaryWithObjects:@[@"hua",@"12",@NO]forKeys:@[@"name",@"age",@"flag"]]; NSMutableDictionary *dict2 = [NSMutableDictionarydictionaryWithObjects:@[@"huhu",@"14",@NO]forKeys:@[@"name",@"age",@"flag"]]; NSMutableDictionary *dict3 = [NSMutableDictionarydictionaryWithObjects:@[@"huxun",@"16",@YES]forKeys:@[@"name",@"age",@"flag"]]; _myData = [NSMutableArray arrayWithObjects:dict1,dict2,dict3, nil]; } return_myData; } - (void)viewDidLoad { [superviewDidLoad]; // Do any additional setup after loading the view, typically from a nib. CGFloat customW =self.view.bounds.size.width; CGFloat customH =self.view.bounds.size.height; UITableView *tableview = [[UITableView alloc] initWithFrame:CGRectMake(10,44, customW -2 *10, customH -80)style:UITableViewStylePlain]; tableview.delegate =self; tableview.dataSource =self; // tableview.separatorStyle = UITableViewCellSeparatorStyleNone; tableview.separatorColor = [UIColorblueColor]; tableview.separatorInset =UIEdgeInsetsMake(0,20,0,20); self.myTableView = tableview; [self.viewaddSubview:self.myTableView]; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } /** * 返回每组有多少行 * */ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.myData.count; } /** * 返回cell * */ - (MyCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { staticNSString *ID =@"MyCell"; MyCell *cell = [tableViewdequeueReusableCellWithIdentifier:ID]; // cell的重用 if (cell ==nil) { cell = [[MyCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:ID]; } // cell值的初始化 return cell; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; { if ([self.myData[indexPath.row][@"flag"]boolValue]) { return 44 + 30; } return 44; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { } - (void)didReceiveMemoryWarning { [superdidReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
相关文章推荐
- 招聘一个靠谱的 iOS(上)
- HDU_1017_AMathematicalCuriosity
- IOS_开发技巧_定位反地理编码的一些坑
- 做一个靠谱的iOS开发者(1)
- iOS开发_Tips(1)
- 关于iOS开发
- iOS开发-项目 知识分享与经验积累(一)
- IOS中Json解析的四种方法
- [精通iOS开发(第5版)]第21章 集合视图 笔记
- IOS开发之NSOperation
- iOS8开发--在Xcode6中设置Launch Image(启动图片)
- iOS8开发-- 如何在Xcode6中添加pch全局引用文件
- 一、IOS程序执行顺序、APPdelegate代理方法执行顺序、ViewController 生命周期
- IOS开发之多线程队列
- IOS autoLayout之使用VFL语言进行代码自动布局
- hdu 1017 A Mathematical Curiosity
- ios 为图片加上水印详解
- iOS 扩展机制category与associative
- iOS内存管理机制
- iOS7.0及以上图片转换成base64编码的方法(oc)