tableView-自定义非等高cell(1)

  • 非等高的cell
    • xib自定义cell
    • storyboard自定义cell
    • 代码自定义cell(frame)
    • 代码自定义cell(Autolayout)

自定义非等高cell-xib(1)

布局内容,返回每一行的高度为固定值

常规设置

  • 自定义控制器类
    • 懒加载,面向模型开发,将字典转为模型,以后数据通过模型获得
    • 实现数据源和代理类方法
#import "WQViewController.h"
#import "WQStatusData.h"
#include "WQStatusCell.h"
@interface WQViewController ()
/**数据*/
@property (nonatomic, strong) NSArray *statusData;
@end

@implementation WQViewController



- (NSArray *)statusData
{

    if (_statusData == nil) {
        NSArray *arrFromPlist = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"statuses.plist" ofType:nil]];
        NSMutableArray *arr = [NSMutableArray array];
        [arrFromPlist enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            WQStatusData *statusdata = [WQStatusData statusWithDict:obj];
            [arr addObject:statusdata];
        }];
        _statusData = arr;
    }
        return _statusData;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.statusData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WQStatusCell *cell = [WQStatusCell statusWithTableView:tableView];
    cell.statusData = _statusData[indexPath.row];
    return cell;
}
/**
 *返回每一行的高度
 */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 250;
}
- (void)viewDidLoad {
    [super viewDidLoad];
}

@end
  • 自定义cell类(同时生成xib)
    • 提供函数用于加载XIB文件,带有一个UITableView参数,用于cell缓冲池优化
    • 从xib向类扩展拖线,获取到自定义布局的控件
    • 提供数据接口,在获取数据的setter方法中,给控件赋值赋值
#import <UIKit/UIKit.h>

@interface WQViewController : UITableViewController
@end

#import <UIKit/UIKit.h>
@class WQStatusData;
@interface WQStatusCell : UITableViewCell
/** 数据*/
@property (nonatomic, strong) WQStatusData *statusData;
+ (instancetype)statusWithTableView:(UITableView *)tableview;
@end

#import "WQStatusCell.h"
#import "WQStatusData.h"
@interface WQStatusCell()
@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UIImageView *pictureImageView;
@property (weak, nonatomic) IBOutlet UIImageView *vipImageView;

@property (weak, nonatomic) IBOutlet UILabel *contentLabel;
@end

@implementation WQStatusCell

+ (instancetype)statusWithTableView:(UITableView *)tableview
{
    static NSString *ID = @"status";
    WQStatusCell *cell = [tableview dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil]lastObject];
    }
    return cell;
}

- (void)setStatusData:(WQStatusData *)statusData
{
    _statusData = statusData;

    if (self.statusData.isVip) {
        self.vipImageView.hidden = NO;
        self.nameLabel.textColor = [UIColor redColor];
    }else
    {
        self.vipImageView.hidden = YES;
        self.nameLabel.textColor = [UIColor blackColor];
    }
    if (_statusData.picture) {
        self.pictureImageView.hidden = NO;
        self.pictureImageView.image = [UIImage imageNamed:_statusData.picture];
    }else
    {
        self.pictureImageView.hidden = YES;
    }
    self.iconImageView.image = [UIImage imageNamed:_statusData.icon];
    self.nameLabel.text = _statusData.name;
    self.contentLabel.text = _statusData.text;
}
@end
  • toryboard中的操作

    • 普通控制器UIViewController控制器(自带一个view),添加tabView(推荐)
      • 可以直接拖一个UITableViewController,但是里面有好多添加好的东西,不一定满足我们的要求,比如:一些需要添加一些工具栏等.
    • 给控制器定义Class类型
    • 给xib中的cell定义Class类型
    • 设置cell的标示符
    • 自定义控件,并AutoLayout自动布局添加约束.
  • 自定义数据模型类

#import <Foundation/Foundation.h>

@interface WQStatusData : NSObject
/**头像*/
@property (nonatomic, strong) NSString *icon;
/**姓名*/
@property (nonatomic, strong) NSString *name;
/** 内容*/
@property (nonatomic, strong) NSString *text;
/**图片*/
@property (nonatomic, strong) NSString *picture;
/** vip*/
@property (nonatomic, assign, getter = isVip) BOOL vip;
+ (instancetype)statusWithDict:(NSDictionary *)dict;
@end

@implementation WQStatusData
+ (instancetype)statusWithDict:(NSDictionary *)dict
{
    WQStatusData *statusData = [[WQStatusData alloc]init];
    [statusData setValuesForKeysWithDictionary:dict];
    return statusData;
}
@end

这里写图片描述
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值