您的位置:首页 > 产品设计 > UI/UE

自定义UITableViewCell (通过XIB:每个CELL形状一样适合用XIB来创建CELL)

2014-11-13 17:44 344 查看
1,自定义CELL创建

A) 新建XIB文件,继承自UITableViewCell ,CELL设置 identify标识,用于性能优化中,将移出手机屏幕的CELL放入缓存池中的标识,这里的值必须与取出缓存池中的值一致

B)新建管理XIB的CLASS文件,此文件也要继承自UITableViewCell,

C) 对XIB和CLASS进行连线,以便可以通过CLASS对XIB中控件属性进行访问

2,思路

A) 主控制器实现Datasource的相关方法,创建CELL不再直接通过UITableViewCell创建,而是通过XIB来实现的,(XIB被哪个CLASS管理,那么主控制器加载XIB返回值就是那个CLASS类型)

B) 谁创建产生CELL?? 谁更清楚创建CELL,就在该CLASS中定义创建CELL的方法

C)主控制器viewDidLoad方法中加载其他,主TableView有TableHeaderView和TableFooterView属性来产生头部和尾部的View,这里头尾VIEW也可以自定义XIB来实现

D)管理XIB的CLASS继承自什么类型,那么在加载该XIB后返回类型就是该CLASS的类型

3,代码(还没实现tableHeaderView的滚动图片效果)





#import <UIKit/UIKit.h>

@class TXTg;

@interface TXTgCell : UITableViewCell

@property (nonatomic, strong) TXTg *tg;

+ (instancetype)cellWidthTableView:(UITableView *)myTableView;

@end

=========

#import "TXTgCell.h"

#import "TXTg.h"

@interface TXTgCell()

@property (weak, nonatomic) IBOutlet UIImageView *myImageView;

@property (weak, nonatomic) IBOutlet UILabel *myTitleLabel;

@property (weak, nonatomic) IBOutlet UILabel *myPriceLabel;

@property (weak, nonatomic) IBOutlet UILabel *myBuyCountLabel;

@end

@implementation TXTgCell

+ (instancetype)cellWidthTableView:(UITableView *)myTableView

{

static NSString *ID = @"tg";

TXTgCell *cell = [myTableView dequeueReusableCellWithIdentifier:ID];

if(cell == nil)

{

cell = [[[NSBundle mainBundle] loadNibNamed:@"TXTgCell" owner:nil options:nil] lastObject];

}

return cell;

}

- (void)setTg:(TXTg *)tg

{

_tg = tg;

self.myImageView.image = [UIImage imageNamed:tg.icon];

self.myTitleLabel.text = tg.title;

self.myPriceLabel.text = [NSString stringWithFormat:@"$%@.00", tg.price];

self.myBuyCountLabel.text = [NSString stringWithFormat:@"%@ bought.",tg.buyCount];

}

@end

===========

#import <UIKit/UIKit.h>

@class TXTgFooterView;

@protocol TXTgFooterViewDelegage <NSObject>

@optional

- (void)tgFooterViewDidLoadButton:(TXTgFooterView *)footerView;

@end

@interface TXTgFooterView : UITableViewCell

@property (nonatomic, strong) id<TXTgFooterViewDelegage> delegate;

+ (TXTgFooterView *)footerView;

@end

=============

@interface TXTgFooterView()

- (IBAction)loadBtnClick;

@property (weak, nonatomic) IBOutlet UIButton *loadBtn;

@property (weak, nonatomic) IBOutlet UIView *loadView;

@end

@implementation TXTgFooterView

+ (TXTgFooterView *)footerView

{

return [[[NSBundle mainBundle] loadNibNamed:@"TXTgFooterView" owner:nil options:nil] lastObject];

}

- (IBAction)loadBtnClick

{

self.loadBtn.hidden = YES;

self.loadView.hidden = NO;

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 *NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

if([self.delegate respondsToSelector:@selector(tgFooterViewDidLoadButton:)])

{

[self.delegate tgFooterViewDidLoadButton:self];

self.loadBtn.hidden = NO;

self.loadView.hidden = YES;

}

});

}

@end

=========

#import "TXViewController.h"

#import "TXTg.h"

#import "TXTgCell.h"

#import "TXTgFooterView.h"

#import "TXTgHeaderView.h"

@interface TXViewController () <UITableViewDataSource, TXTgFooterViewDelegage>

@property (weak, nonatomic) IBOutlet UITableView *iTableView;

@property (nonatomic, strong) NSMutableArray *tgs;

@end

@implementation TXViewController

- (void)viewDidLoad

{

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

self.iTableView.rowHeight = 80;

TXTgFooterView *footer = [TXTgFooterView footerView];

footer.delegate = self;

self.iTableView.tableFooterView = footer;

self.iTableView.tableHeaderView = [TXTgHeaderView headerView];

}

- (void)tgFooterViewDidLoadButton:(TXTgFooterView *)footerView

{

TXTg *tg = [[TXTg alloc] init];

tg.title = @"hhh";

tg.price = @"101";

tg.buyCount = @"8";

tg.icon = @"9b437cdfb3e3b542b5917ce2e9a74890";

[self.tgs addObject:tg];

[self.iTableView reloadData];

}

- (BOOL)prefersStatusBarHidden

{

return YES;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

return self.tgs.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

TXTgCell *cell = [TXTgCell cellWidthTableView:tableView];

cell.tg = self.tgs[indexPath.row];

return cell;

}

- (NSMutableArray *)tgs

{

if(_tgs == nil)

{

NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"tgs.plist" ofType:nil]];

NSMutableArray *tgsArray = [NSMutableArray array];

for (NSDictionary *dict in dictArray) {

TXTg *tg = [TXTg tgWithDict:dict];

[tgsArray addObject:tg];

}

_tgs = tgsArray;

}

return _tgs;

}

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