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];
}