IOS开发之——使用XIB自定义视图实现团购(38)

一 概述

  • 新建自定义Cell(TgCell.xib)

  • 拖拽一个需要自定义的控件,摆放其他子控件

  • 新建一个类

    • 类名要与XIB的名字保持一致

    • 继承自的子类要与XIB中的根节点的类型一致

  • 要连线之前,需要将XIB的根节点类名修改为刚刚新建的类名

<!--more-->

二 效果图

 

三 代码

TgCell.h

#import <UIKit/UIKit.h>
​
@interface TgCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UILabel *buyCountLabel;
@end

ViewController.m

#import "ViewController.h"
#import "TG.h"
#import "TgCell.h"
​
@interface ViewController ()
@property (nonatomic,strong) NSArray *tgs;
​
@end
​
@implementation ViewController
​
- (NSArray *)tgs
{
    if (_tgs==nil) {
        _tgs=[TG tgs];
    
    }
    return _tgs;
}
-(void)viewDidLoad
{
    [super viewDidLoad];
    self.tableView.rowHeight=80;
    self.tableView.contentInset=UIEdgeInsetsMake(20, 0, 0, 0);
}
#pragma mark -数据源
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tgs.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //1.可重用标识符
    static NSString *ID=@"Cell";
    //2.tableView查询可重用Cell
    TgCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];
    //3.如果没有可重用cell
    if (cell==nil) {
        cell=[[[NSBundle mainBundle]loadNibNamed:@"TgCell" owner:nil options:nil]lastObject];      
    }
    //4.设置cell内容
    TG *tg=self.tgs[indexPath.row];
    cell.titleLabel.text=tg.title;
    cell.iconView.image=[UIImage imageNamed:tg.icon];
    cell.priceLabel.text=tg.price;
    cell.buyCountLabel.text=tg.buyCount;  
    return cell;
}
@end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值