IOS表没有限制行数,行数仅受可用存储空间的限制,表只有一列。是UITableView类的一个实例。
表视图不负责存储表中的数据。表视图从遵循UITableViewDelegate协议的对象获取配置数据,从遵循UITableViewDataSource协议的对象获取行数据。表中每一行都由一个UITableViewCell表示。
在.h文件中遵循UITableViewDelegate协议和UITableViewDataSource协议,声明表视图对象,表中数据对象。
@interface LinViewController : UIViewController<UITableViewDataSource,UITableViewDelegate> //创建表视图的对象 @property (retain, nonatomic) UITableView * mTableView; //创建数组对象,存储表中数据 @property (retain, nonatomic) NSArray * array; @end
在.m文件中加载表视图对象、数组对象,对协议的方法选择性实现,示例如下:
//释放创建的对象 - (void)dealloc { [_mTableView release]; [_array release]; [super dealloc]; } - (void)viewDidLoad { [super viewDidLoad]; //初始化数组,存入系统的字体作为表视图中的数据 self.array = [UIFont familyNames]; //创建初始化表视图 self.mTableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain]; //创建一个视图,作为表头 UIView * pView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 40)]; //设置视图的颜色 pView.backgroundColor = [UIColor blueColor]; //设置表头视图 self.mTableView.tableHeaderView = pView; //设置页眉的高度 self.mTableView.sectionHeaderHeight = 50; //修改行分隔线颜色 self.mTableView.separatorColor = [UIColor redColor]; //设置分割线的类型,此处设置为消失 self.mTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLineEtched; //设置委托的对象----表的委托,切记添加 self.mTableView.dataSource = self; self.mTableView.delegate = self; //把表视图添加到当前的视图中 [self.view addSubview:self.mTableView]; //释放创建的临时变量 [pView release]; } #pragma mark----tableViewDataSource---协议的方法 //设置分段中的行数-----必须实现 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self.array count]; } //表中每一行数据的绘制-----必须实现 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //设置静态变量,作为标志 static NSString * identifer = @"identifer"; //创建TableViewCell对象,根据静态变量标志判断缓存中是否已存在 UITableViewCell * pCell = [tableView dequeueReusableCellWithIdentifier:identifer]; //判断TableViewCell是否为空,为空则创建新的 if (nil == pCell) { pCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifer]; } //获取当前要绘制的行数 NSUInteger cellRow = [indexPath row]; //根据行数找到数组中对应下标的数据 NSString * pString = [self.array objectAtIndex:cellRow]; //设置行中文本内容 pCell.textLabel.text = pString; //设置行中文本的字体 pCell.textLabel.font = [UIFont fontWithName:pString size:16]; //设置行中文本字体的颜色 pCell.textLabel.textColor = [UIColor blueColor]; //设置行中文本的注释,根据创建TableViewCell的格式,可显示出来 pCell.detailTextLabel.text = @"detailText"; //设置行中小图标 pCell.imageView.image = [UIImage imageNamed:@"text.png"]; //设置行中标志格式,此处为每行添加✅ pCell.accessoryType = UITableViewCellAccessoryCheckmark; return pCell; } //-----------可选实现的方法--------------------- //为表头设置文本 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return @"I'm Header_Title"; } //为标为设置文本,一般省略 - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section { return @"I'm Footer_Title"; } #pragma mark----tableViewDelegate---都是可选实现 //选中某行的时候调用此方法,最常用 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { //获取当前点击的行数 NSUInteger row = [indexPath row]; //创建字符串,存储选中行信息 NSString * pString = [NSString stringWithFormat:@"你选中了第%i行!",row]; //弹出警告框,显示选中某行 UIAlertView * pAlert = [[UIAlertView alloc]initWithTitle:@"attention" message:pString delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; //展示警告框,释放创建的对象 [pAlert show]; [pAlert release]; } //调整行高 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 44; } //调整header 高度 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 20; } //行内容进行偏移 - (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath { //获取当前行数,移动相应的距离 NSUInteger row = [indexPath row]; return row; }