Objective-C 学习记录 - 24

1.UITableView索引条的设置

/** 按顺序返回各组的索引,实现以下方法则会自动创建索引条 */
-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return [_phoneGroups valueForKeyPath:@“header”];
}
//以下属性分别可以设置索引条的文字颜色和背景颜色
tableView.sectionIndexColor = [UIColor redColor];
tableView.sectionIndexBackgroundColor = [UIColor yellowColor];

2.自定义等高的cell
创建一个UITableViewCell类,
设置init方法,如initWithStyle,然后在方法中添加需要的子控件,并做一些初始化设置(设置字体、文字颜色等)
设置layoutSubviews方法,在方法中设置子控件的frame

提供一个模型属性,并重写属性的set方法,给子控件传递数据

在控制器中给cell传递模型数据

以数据模型名为phone为例:

/** 在这里初始化需要添加进cell的子控件 */
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        UIImageView *iconImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:iconImageView];
        self.iconImageView = iconImageView;

        UILabel *titleLabel = [[UILabel alloc] init];
        [self.contentView addSubview:titleLabel];
        self.titleLabel = titleLabel;

        UILabel *subtitleView = [[UILabel alloc] init];
        [self.contentView addSubview:subtitleView];
        self.subtitleLabel = subtitleView;

    }
    return self;
}

/** 这里设置cell内各子控件的布局 */
-(void)layoutSubviews
{
    [super layoutSubviews];

    CGFloat iconImageViewX = 10;
    CGFloat iconImageViewY = 10;
    CGFloat iconImageViewW = 80;
    CGFloat iconImageViewH = self.contentView.frame.size.height - 20;
    self.iconImageView.frame = CGRectMake(iconImageViewX, iconImageViewY, iconImageViewW, iconImageViewH);
    self.iconImageView.backgroundColor = [UIColor redColor];

    CGFloat titleLabelX = iconImageViewX + iconImageViewW + 10;
    CGFloat titleLabelY = iconImageViewY;
    CGFloat titleLabelW = 100;
    CGFloat titleLabelH = 20;
    self.titleLabel.frame = CGRectMake(titleLabelX, titleLabelY, titleLabelW, titleLabelH);

    CGFloat subtitleLabelX = iconImageViewX + iconImageViewW + 10;
    CGFloat subtitleLabelY = iconImageViewY + 40;
    CGFloat subtitleLabelW = 100;
    CGFloat subtitleLabelH = 20;
    self.subtitleLabel.frame = CGRectMake(subtitleLabelX, subtitleLabelY, subtitleLabelW, subtitleLabelH);
}

/** 设置数据 */
-(void)setPhone:(phone *)phone
{
    _phone = phone;
    self.titleLabel.text = phone.title;
    self.subtitleLabel.text = phone.subtitle;
    self.iconImageView.image = [UIImage imageNamed:phone.icon];
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值