tableView - 自定义等高cell-storyBoard方式
来源:互联网 发布:南通阿里云 编辑:程序博客网 时间:2024/05/11 05:00
自定义等高cell-storyBoard方式
- 自定义控制器类
- 1.创建一个继承自UITableViewController的子类,比如WQDealsTableViewController
- 2.实现需要的数据源和代理方法.
- 3.实现懒加载,获取数据模型.
- 4.创建自定义cell.
- 1.创建一个继承自UITableViewController的子类,比如WQDealsTableViewController
#import "WQDealsTableViewController.h"#import "WQdealsData.h"#import "WQTableViewCell.h"@interface WQDealsTableViewController ()/**数据模型*/@property (nonatomic, strong) NSArray *dealsData;@end@implementation WQDealsTableViewController- (void)viewDidLoad { [super viewDidLoad];}/** 懒加载*/- (NSArray *)dealsData{ if(_dealsData == nil) { NSArray *dataArrayFromPlist = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"deals.plist" ofType:nil]]; NSMutableArray *dataArray = [NSMutableArray array]; [dataArrayFromPlist enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { WQdealsData *dealsdata = [WQdealsData dealsWithDict:obj]; [dataArray addObject:dealsdata]; }]; _dealsData = dataArray; } return _dealsData;}-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.dealsData.count;}/** 创建cell*/- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ // 创建自定义的tableViewCell WQTableViewCell *cell = [WQTableViewCell tableViewCellWithTableView:tableView]; // 向自定的tableViewCell中传入数据 cell.dealData = self.dealsData[indexPath.row]; return cell;}@end
- 自定义cell类
- 1.创建一个继承自UITableViewCell的子类,比如WQTableViewCell
- 2.定义一个数据模型接口,用于传入数据
- 3.定义一个类方法,用于创建cell,带有一个从控制器中传入的参数tableview,用于缓冲池的优化.并返回创建好的cell.
#import <UIKit/UIKit.h>@class WQdealsData;@interface WQTableViewCell : UITableViewCell/**cell的数据模型*/@property (nonatomic, strong) WQdealsData *dealData;+ (instancetype)tableViewCellWithTableView:(UITableView *)tableView;@end#import "WQTableViewCell.h"#import "WQdealsData.h"@interface WQTableViewCell()@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;@property (weak, nonatomic) IBOutlet UILabel *titleLabel;@property (weak, nonatomic) IBOutlet UILabel *priceLabel;@property (weak, nonatomic) IBOutlet UILabel *buyCountLabel;@end@implementation WQTableViewCell+ (instancetype)tableViewCellWithTableView:(UITableView *)tableView{ static NSString *ID = @"deals"; WQTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; // 当使用注册方法载入xib或者在sotoryboard中的cell设置标示符来优化cell创建的时候,cell永远不为nil,不进入该方法 if (cell == nil) { cell = [[WQTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; } return cell;}- (void)setDealData:(WQdealsData *)dealData{ _dealData = dealData; self.iconImageView.image = [UIImage imageNamed:self.dealData.icon]; self.titleLabel.text = self.dealData.title; self.priceLabel.text = [NSString stringWithFormat:@"%@¥", self.dealData.price]; self.buyCountLabel.text = [NSString stringWithFormat:@"%@人已购买", self.dealData.buyCount];}
storyboard中的操作
- 1.在storyboard中创建一个tableViewController控制器
- 可以直接拖动一个tableViewController控制器
- 也可以先拖一个普通控制器,添加tableView控件,然后再添加tableViewCell控件
- 设为初始控制器
- 2.为tableViewController定义类型(自定义控制器类继承自tableViewController)
- - 2.在tableViewCell中自定义控件布局
- 完成AutoLayout自动布局,即添加约束
- 3.设置cell的重用标识
- 4.为tableViewCell定义类型(需要自定义cell类)
- 5.将cell中的控件拖线到自定义cell类中,在获取数据模型时,,可以在其setter方法中取到控件赋值
- 1.在storyboard中创建一个tableViewController控制器
定义数据模型类(补充)
#import <Foundation/Foundation.h>@interface WQdealsData : NSObject/**图片*/@property (nonatomic, strong) NSString *icon;/**标题*/@property (nonatomic, strong) NSString *title;/**购买数*/@property (nonatomic, strong) NSString *buyCount;/** 价格*/@property (nonatomic, strong) NSString *price;+ (instancetype)dealsWithDict:(NSDictionary *)dict;@end#import "WQdealsData.h"@implementation WQdealsData+ (instancetype)dealsWithDict:(NSDictionary *)dict{ WQdealsData *dealsData = [[WQdealsData alloc]init]; /* *等价于以下: *dealsData.icon = dict[@"icon"]; *dealsData.title = dict[@"title"]; *dealsData.price = dict[@"price"]; *dealsData.buyCount = dict[@"buyCount"]; */ // KVC - Key Value Coding [dealsData setValuesForKeysWithDictionary:dict]; return dealsData;}@end
0 0
- tableView - 自定义等高cell-storyBoard方式
- tableView - 自定义等高cell-xib方式
- UI 一一 自定义等高cell (storyboard方式)
- tableView - 自定义等高cell(frame和masonry方式)
- 自定义等高的cell(storyboard)
- storyboard自定义非等高的Cell
- tableView-自定义非等高cell(1)
- tableView-自定义非等高cell(2)
- iOS开发总结之自定义等高cell-storyboard
- iOS开发总结之自定义非等高cell-storyboard
- UI 一一 自定义不等高cell (storyboard方式)
- 自定义不等高cell的storyboard方法
- tableview非等高的cell
- UI 一一 自定义等高cell (纯代码-Frame)方式
- UI 一一 自定义等高cell (纯代码-Autolayout)方式
- UI 一一 自定义等高cell (XIB方式)
- 自定义等高cell和不等高cell的方式对比
- 自定义等高的cell
- WIN32编程之.rc资源文件的使用
- Box2d新系列 第七章 物体(Bodies)
- Java synchronized详解
- HDU 5371 Hotaru's problem
- 入职初体验
- tableView - 自定义等高cell-storyBoard方式
- POJ 2007 Scrambled Polygon(凸包)
- HDUOJ_1233(还是畅通工程)(最小生成树)
- JavaScript权威指南_153_第16章_脚本化CSS_16.3-脚本化内联样式
- 【C语言经典实例】-寻找数组的最值
- 如果牛顿是程序员,那么?
- 我的金工实习心得(三):铸工第二天
- 来自Gradle的hello world
- spring 之jdbcTemplate使用