您的位置:首页 > 其它

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

2015-08-15 01:14 239 查看
非等高的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




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: