iOS UITableView(表格)

UITableView(表格)在实际开发中用得非常之多,下面将介绍它简单的使用方法:

表视图(UITableView 一讲)          
1.定义:UITableView使用了重用机制,通过重用tableView的cell,达到节省内存的母的,使用一个字符串类型的ID判断是哪一种cell
2.UITableView是UIScrollView的子类,有两种样式 平铺和重组
3.初始化以及使用(它有两个重要的代理,基本上每次用的时候都会导入UITableViewDelegate,UITableViewDataSource):
UITableViewDataSource有两个必须实现的方法:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
 UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, margin, WIDTH, HEIGHT) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];

1⃣️:- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return dataList.count;
}

2⃣️:    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //    cell的唯一标识
    NSString *cellID = @"citys";
//    UITableView 的重用 是通过查找cell的唯一标识cellID 来判断这个cell对象是否存在  如果不存在 再去实例化
    
    
//    通过cellID查找tableView上的cell dequeueReusable查找可重用的cell
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
//    如果没有查找到可重用的cell 就实例化cell对象
    if (! cell) {
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        
//        cell.textLabel.text = dataList[indexPath.row]; 写在这里回引起数据混乱

        
//    不要再初始化cell的地方去加载数据 如果在这里加载 数据就会混乱
    }
    
//    显示数据的时候 写在初始化cell的外面
    
//    不能在这个方法里面去加载初始化数据 因为这个会不断的调用
    
//    通过indexPath 得到哪一组(indexPat.section) 也可以得到是哪一行 (indexPath.row) 都是从 0 开始
    
    cell.textLabel.text = dataList[indexPath.row];
    
    
    cell.imageView.image = [UIImage imageNamed:@"wifi-full"];
    return cell;
}
3⃣️:/**
 *  选中cell的时候调用
 *
 *  @param tableView
 *  @param indexPath
 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSLog(@"%@",dataList[indexPath.row]);
    
    UIAlertView *tishi = [[UIAlertView alloc]initWithTitle:nil message:@"ooo" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    
    [tishi show];
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值