tableView - 自定义等高cell-storyBoard方式

来源:互联网 发布:南通阿里云 编辑:程序博客网 时间:2024/05/11 05:00

自定义等高cell-storyBoard方式

  • 自定义控制器类
    • 1.创建一个继承自UITableViewController的子类,比如WQDealsTableViewController
      这里写图片描述
    • 2.实现需要的数据源和代理方法.
    • 3.实现懒加载,获取数据模型.
    • 4.创建自定义cell.
#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的重用标识
      - ![](images/0603/设置cell的标识符.png)
    • 4.为tableViewCell定义类型(需要自定义cell类)
    • 5.将cell中的控件拖线到自定义cell类中,在获取数据模型时,,可以在其setter方法中取到控件赋值
  • 定义数据模型类(补充)

#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
原创粉丝点击