自定cell(XIB)团购思路

本文详细介绍如何自定义UITableViewCell,包括解析plist文件创建数据模型、定义XIB文件、实现UITableView代理方法等步骤,帮助开发者掌握自定义Cell技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自定cell(XIB)团购思路

步骤一、先解析plist文件,创建model层数据。

- (instancetype)initWithDict:(NSDictionary *)dict
{
   
self = [super init];
   
if (self) {
        [
self setValuesForKeysWithDictionary:dict];
    }
   
return self;
}

+ (
instancetype)tgWithDict:(NSDictionary *)dict
{
   
return [[self alloc] initWithDict:dict];
}

+ (
NSMutableArray *)tgs
{
   
NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"tgs.plist" ofType:nil]];
   
   
NSMutableArray *arrayM = [NSMutableArray array];
   
for (NSDictionary *dict in array) {
        [arrayM
addObject:[self tgWithDict:dict]];
    }
   
   
return arrayM;
}
二、自己定义Cell
1 创建XIB文件,并设置控件内部细节。


2 创建一个与XIB同名的类,注意自己定义的类继承自的对象,要与XIB中根对象的类保持一致。
3 又一次指定XIB中的根类,打开助理编辑器辅助观察变化。


4 对须要改动属性的控件做IBOutlet连线,注意,不要与TableViewCell的已经存在的属性重名
连线控件的名称不能是:
(
1) textLabel
(
2) detailTextLabel
(
3) imageView

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
   
// 1. 可重用标示符
   
static NSString *ID = @"Cell";
   
// 2. tableView查询可重用Cell
   
HMTgCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
   
   
// 3. 假设没有可重用cell
   
if (cell == nil) {
       
NSLog(@"载入XIB");
       
// XIB载入自己定义视图
        cell = [[[
NSBundle mainBundle] loadNibNamed:@"HMTgCell" owner:nil options:nil] lastObject];
    }
   
   
return cell;
}

- (
void)setTg:(HMTg *)tg
{
   
// setter方法中,第一句要赋值,否则要在其它方法中使用模型,将无法訪问到
   
_tg = tg;

   
self.titleLabel.text = tg.title;
   
self.iconView.image = [UIImage imageNamed:tg.icon];
   
self.priceLabel.text = tg.price;
   
self.buyCountLabel.text = tg.buyCount;
}

#pragma mark - 模板提供的方法
/**
 
初始化方法
 
 
使用代码创建Cell的时候会被调用,假设使用XIB或者Storyboard。此方法不会被调用
 */

- (
id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
   
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
   
if (self) {
       
NSLog(@"%s", __func__);
    }
   
return self;
}

/**
 
XIB被载入之后,会自己主动被调用,假设使用纯代码。不会被运行
 */

- (
void)awakeFromNib
{
   
NSLog(@"%s", __func__);
   
self.contentView.backgroundColor = [UIColor clearColor];
}

/**
 Cell
被选中或者取消选中是都会被调用
 
 
假设是自己定义Cell控件,全部的子控件都应该加入到contentView
 */

- (
void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [
super setSelected:selected animated:animated];

   
if (selected) {
       
self.contentView.backgroundColor = [UIColor redColor];
    }
else {
       
self.contentView.backgroundColor = [UIColor greenColor];
    }
}
三、在控制器实现UITableView的代理方法
#pragma mark - 数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   
return self.tgs.count;
}

- (
UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
// 1. 创建cell
   
HMTgCell *cell = [HMTgCell cellWithTableView:tableView];
   
   
// 2. 通过数据模型。设置Cell内容,能够让视图控制器不须要了解cell内部的实现细节
    cell.
tg = self.tgs[indexPath.row];
   
   
return cell;
}
四、自己定义点击開始下载XIB
+ (instancetype)footerView
{
   
return [[[NSBundle mainBundle] loadNibNamed:@"HMTgFooterView" owner:nil options:nil] lastObject];
}

- (
IBAction)loadMore
{
   
NSLog(@"载入很多其它");
   
// 1. 隐藏button
   
self.loadMoreButton.hidden = YES;
   
// 2. 显示提示视图
   
self.tipsView.hidden = NO;

   
// 3.1 推断代理是否实现了协议方法
   
if ([self.delegate respondsToSelector:@selector(tgFooterViewDidDownloadButtonClick:)]) {
        [
self.delegate tgFooterViewDidDownloadButtonClick:self];
    }
}
/** 视图控制器刷新完毕调用方法 */
在XIB加入View和Button,载入完数据之后,View隐藏,Button显示。


- (void)endRefresh
{
   
// 4. 载入完毕数据
   
self.loadMoreButton.hidden = NO;
   
self.tipsView.hidden = YES;
}
五、自己定义协议,当点击開始下载button,通知控制器
@protocol HMTgFooterViewDelegate <NSObject>

@optional
/** 视图的下载button被点击 */
- (
void)tgFooterViewDidDownloadButtonClick:(HMTgFooterView *)footerView;

@end

- (void)tgFooterViewDidDownloadButtonClick:(HMTgFooterView *)footerView
{
   
// 模拟取网络上获取数据载入数据
   
NSLog(@"努力载入数据中....");
   
   
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
       
// 获得网络数据之后运行的操作
       
       
// 向数组中加入数据,模拟网络载入完毕之后的效果
       
NSDictionary *dict = @{@"title": @"哈哈", @"icon": @"ad_00", @"price": @"100.2", @"buyCount": @"250"};
       
HMTg *tg = [HMTg tgWithDict:dict];
       
       
NSLog(@"加数据前 %d", self.tgs.count);
       
        [
self.tgs addObject:tg];
       
       
NSLog(@"加数据后 %d", self.tgs.count);
       
// 刷新数据
       
//    [self.tableView reloadData];
       
// 新建一个indexPath
       
NSIndexPath *path = [NSIndexPath indexPathForRow:(self.tgs.count - 1) inSection:0];
        [
self.tableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationMiddle];
       
       
// 通知页脚视图调整视图显示状态
        [footerView
endRefresh];
    });
}

转载于:https://www.cnblogs.com/llguanli/p/6944063.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值