tableView 的使用步骤和方法

常用小方法
自定义区头视图
- (UIView )tableView:(UITableView )tableView viewForHeaderInSection:(NSInteger)section;
{

  • (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    {
    return 25;
    }

单元格被选中后接着被取消
- (void)tableView:(UITableView )tableView didSelectRowAtIndexPath:(NSIndexPath )indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}

单元格不能被选中
cell.selectionStyle = UITableViewCellSelectionStyleNone;

一般步奏
第一步 初始化表视图
- (void)loadView // 加载视图用
{
// 创建一个表视图 tableView 赋值给 controller.view
UITableView *tableView = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds] style:(UITableViewStylePlain)];
tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;//分割线样式
tableView.separatorColor = [UIColor yellowColor];//分割线颜色
// 设置页眉与页脚
UIImageView *header = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@”/Users/lanou3g/Desktop/N%07T(F}L8%MRYLR}XD25{U.jpg”]];
UIImageView *header1 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@”/Users/lanou3g/Desktop/N%07T(F}L8%MRYLR}XD25{U.jpg”]];
tableView.tableHeaderView = header; // 页眉想要偏移 先定义UIImageView的 fram 然后给tableHeaderView = 一个空视图 再使用 addsubview
tableView.tableFooterView = header1; // 页脚想要偏移先定义UIImageView的 fram 然后直接传给tableFooterView
// *给 tableView 添加代理和数据源 .h 里添加代理
tableView.dataSource = self;
tableView.delegate = self;
self.view = tableView; // 此处用等号
[tableView release];

}
创建数据
- (void)setData
{
NSArray *zArray = @[@”赵玉凤”,@”赵慧珍”,@”张文泽” ];
NSArray *yArray = @[@”易慧云”,@”瑶瑶”];
NSArray *xArray = @[@”谢为民”];
NSArray *wArray = @[@”吴淑敏”];
self.teacherDic = @{@”Z”:zArray,@”Y”:yArray,@”X”:xArray,@”W”:wArray};
self.keysArry = [[_teacherDic allKeys]sortedArrayUsingSelector:@selector(compare:)];
}
第二步 确定有几个分组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView// 告诉表视图有几个分区
{
// 根据联系人组数
return [_keysArry count];
}

第三步 确定各个分区有几个单元格
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section// 告诉表视图各个分区都有几个单元格 section 表示第几个分组
{
// 1.取到对应的著名
NSString *groupName = _keysArry[section];
//2.根据著名
NSArray *array = _teacherDic[groupName];
return [array count];
//return [_teacherDic[_keysArry[section]] count];
}
第四步 确定单元格高度
// 单元格高度 自适应高度
- (CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath
{
// if (0 == indexPath.section) {
// return 100;
// }
}

第五步 生成单元格
- (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath// 生成单元格
{
// 第一步先判断可重用单元格队列里取可以重用的单元格.
// 第二步 创建一个全局区的变量 static 修饰的 NSString作为重用标志
static NSString *cellIndentifier = @”cell1”;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
// 当没有可以被重用的单元格时创建新单元格加重用标记
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:cellIndentifier];
}
// 文本框 左侧单元格编号
// cell.textLabel.text = [NSString stringWithFormat:@”%ld”,indexPath.row];
// 左侧加载图片
UIImage *image = [UIImage imageNamed:@”/Users/lanou3g/Desktop/PNG/5845/180.png”];
cell.imageView.image = image;
//1. 取分区对应的联系人组名
NSString *groupName = _keysArry[indexPath.section];
// 2.获取组名所对应的联系人组
NSArray *array = _teacherDic[groupName];
//3.取 row 对应的联系人
NSString *name = array[indexPath.row];
//4.给 cell 赋值
cell.textLabel.text = name;
return cell;
}
第六步 设置区头标题
// 区头标题
- (NSString )tableView:(UITableView )tableView titleForHeaderInSection:(NSInteger)section
{
// 获取分区下标对应的分组名
NSString *key = _keysArry[section];
return key;
}
第七步 索引标题
// 右侧分区索引标题
- (NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return self.keysArry;
}
第八步 点击方法
// 选中单元格之后,执行 里面的方法
- (void)tableView:(UITableView )tableView didSelectRowAtIndexPath:(NSIndexPath )indexPath
{
NSLog(@”asfesaefsfe”);
}
第九步

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值