自定义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
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
相关文章推荐
- [ios]如何通过XIB 创建自定义的UIViewTableCell
- 通过Xib创建 UICollectionView 和自定义UICollectionViewCell
- 自定义UITableViewCell (通过代码实现:每个CELL格式不一样)
- 如何通过XIB 创建自定义的UIViewTableCell
- 通过xib自定义cell的说明
- IOS xib在tableview上的简单应用(通过xib自定义cell)
- 自定义xib tableviewcell 无法通过nsdata dataWithContentsOfFile读取的问题
- IOS xib在tableview上的简单应用(通过xib自定义cell)
- 使用xib创建自定义Cell
- 进击的KFC:利用Xib来创建UItableviewCell,并实现cell的自适应高度
- [iOS]通过xib自定义cell的时候在awakeFromNib方法里面修改view的frame无效
- iphone 自定义uiTableViewCell,如何通过其数据成员来访问cell本身
- IOS开发 - 05 通过Xib创建自定义视图
- iOS UI-团购案例(通过xib文件自定义UITableViewCell)
- 通过xib和代码自定义cell
- iOS_14_tableViewController_xib创建和封装自定义cell
- 通过创建Cell子类,来实现自定义Cell
- 不自定义cell,最原始的创建UITableViewCell
- 通过cell的UIButton获取UITableViewCell的行数
- MSSQL通过存储过程自动创建自定义数据表