关于“UITableView”;
1.UIITable的创建:
UITableView *table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, self.view.frame.size.height - 64) style:UITableViewStylePlain];
[table setRowHeight:44];//设置行高度
[table setSeparatorColor:[UIColor redColor]];//设置间隔线的颜色
2.在UITableView中添加UIImageView:
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 132)];
//[imageView setBackgroundColor:[UIColor redColor]];
[imageView setImage:[UIImage imageNamed:@"1.png"]];
[table setTableHeaderView:imageView];
[imageView release];//必须释放
1.设置头标题的名称
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
{
return @"名称";
}
2.设置头标题的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 44;
}
3.设置脚标题的名称
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
return @"进入";
}
4.设置脚标题的高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 44;
}
5.右侧添加一个索引表“A-Z”,方便查找
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
NSMutableArray *arr = [NSMutableArray array];
for (int i = 65; i <= 90; i++) {
[arr addObject:[NSString stringWithFormat:@"%c", i]];
}
return arr;
}
6.设置不同的“A-Z”中各个索引中的成员个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//NSLog(@"%s",__func__);
//return [_tableArray count];//任意数值,数值 = 行
if (section == 0) {
return 3;
}else if (section == 1){
return 2;
}
return [_tableArray count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
// if (indexPath.section == 0) {
// if (indexPath.row == 0) {
// return 44;
// }else if (indexPath.row == 1){
// return 88;
// }else if (indexPath.row == 2){
// return 100;
// }else if (indexPath.row == 3){
// return 120;
// }
// }//可以根据不同的情况,设置不同的行高
return 44;
}
UITableView中必须遵循和实现的两个协议:
UITableViewDelegate;
UITableViewDataSource;
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 26;//必须有返回值
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//NSLog(@"%s",__func__);
static NSString *cellIdentify = @"cellIdentify";//static是设置的全局变量
TableViewCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"cellIdentify"];//去以key值命名的重用池里取self;
if (!cell) {
cell = [[[TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentify] autorelease];//创建一个self,并且把重命名的key放到重用池里面
}
NSLog(@"section == %d,row == %d",indexPath.section,indexPath.row);
NSString *str = [_tableArray objectAtIndex:indexPath.row];
[cell.titleLabel setText:str];
return cell;
}