iOS UITableView
列表控件,最常用的控件之一,到哪都能看到它。和Android 的ListView 相比,UITableView 自带了好多功能,
比如说支持分组、每组都有Header和Footer、支持快速索引等等,每次使用都会想起 Android 只能靠自定义控件实现,苦逼啊
初始化设置
如果页面就是一个列表页,可以直接继承 UITableViewController 开发,这样比较方便
TestViewController.h
#import <UIKit/UIKit.h>
@interface TestViewController : UITableViewController
@end
如果没有继承 UITableViewController ,需要实现两个协议 UITableViewDataSource,UITableViewDelegate
TestViewController.h
#import <UIKit/UIKit.h>
@interface TestViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *myTableView; // 列表
@end
TestViewController.m
#import "TestViewController.h"
#import "Masonry.h"
@interface TestViewController () {
NSMutableArray *mArrayData; // 列表数据
}
@end
@implementation TestViewController
@synthesize tableView;
- (void)viewDidLoad {
[super viewDidLoad];
// 列表
tableView = [[UITableView alloc] init];
tableView.showsVerticalScrollIndicator = NO; // 不显示滚动条
tableView.dataSource = self; // 数据源,需实现 UITableViewDataSource
tableView.delegate = self; // 代理,需实现 UITableViewDelegate
tableView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:tableView];
// 列表约束
[tableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view.mas_left);
make.right.equalTo(self.view.mas_right);
make.top.equalTo(self.view.mas_top);
make.bottom.equalTo(self.view.mas_bottom);
}];
}
@end
数据源、代理协议方法
基本的
#pragma mark - 基本的方法
// 分组数量
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 2;
}
// 每组行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 10;
}
// 每行的单元格
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = @"meCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
}
cell.imageView.image = [UIImage imageNamed:@"image_0"];
cell.textLabel.text = [NSString StringWithFormat:@"第%ld组第%ld行", indexPath.section, indexPath.row];
return cell;
}
// 每组header文本
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return [NSString stringWithFormat:@"第%ld个header", section];
}
// 每组footer文本
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
return [NSString stringWithFormat:@"第%ld个footer", section];
}
section header 和 footer 自定义
上面默认的方法只能写写文本,如果要显示更多内容,需要实现下面的方法
// 分组 header
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UILabel *lblText = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, 100, 20)];
lblText.textColor = [UIColor colorWithRed:108.0f/255.0f green:108.0f/255.0f blue:108.0f/255.0f alpha:1.0f];
lblText.text = [mArrayCityFirstEn objectAtIndex:section];
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)];
sectionView.backgroundColor = [UIColor colorWithRed:247.0f/255.0f green:247.0f/255.0f blue:247.0f/255.0f alpha:1.0f];
[sectionView addSubview:lblText];
// 更多其它控件
......
return headerView;
}
// 分组 footer
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
......
原理同上
return footerView;
}
高度相关的
header、footer、cell 都是有默认高度的,当为它们指定自定义的view,必须用下面方法指明高度,不然会不显示或者显示不完整
// section footer 高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 50.0f;
}
// section header 高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 50.0f;
}
// cell 高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 100;
}
// 索引条默认背景色
myTableView.sectionIndexBackgroundColor = [UIColor clearColor];
// 索引条按下时背景色
myTableView.sectionIndexTrackingBackgroundColor = [UIColor colorWithRed:146.0f/255.0f green:146.0f/255.0f blue:146.0f/255.0f alpha:0.3];
// 索引默认文本颜色
myTableView.sectionIndexColor = [UIColor colorWithRed:78.0f/255.0f green:188.0f/255.0f blue:195.0f/255.0f alpha:1.0];
// 索引按下时文本颜色,注意:是全部索引文本变色,不是仅仅按下的文本
myTableView.sectionIndexColor = [UIColor colorWithRed:255.0f/255.0f green:100.0f/255.0f blue:124.0f/255.0f alpha:0.3];
// 索引目录
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
if (tableView == tableViewCity) {
return mArrayCityFirstEn;
} else {
return nil;
}
}
// 点击索引目录
-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
lblPrompt.hidden = NO;
lblPrompt.text = title;
[self hiddenAZIndexView];
if (index == 0) {
[tableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];
[tableView setContentOffset:CGPointZero animated:NO];
return -1;
}
return index;
}
未完待续