IOS - 关于UITableView的用法

本文介绍如何在 iOS 应用中使用 UITableView 控件。通过实现 UITableViewDelegate 和 UITableViewDataSource 协议,可以定制化显示数据列表。文中提供了代码示例,包括 UITableView 的初始化、数据源配置及不同样式单元格的创建。

创建一个object-c的工程ios工程。

UITableView继承UIScrollView,它具有UIScrollView的功能。

在ViewController类中实现UITableViewDelegate,UITableViewDataSource的协议;UITableViewDelegate没有必须实现的方法,都是可选,UITableViewDataSource有两个方法必须实现,其他的可选(具体方法可点击类查询)

//表图视图有多少item

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return  data.count;

}

//表视图的item
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   
    return nil;
}
下面是一个简单的UITableView用法

//
//  ViewController.m
//  UITableView
//
//  Created by ho on 16/11/20.
//  Copyright © 2016年 ho. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView *tableView;
    NSArray *data;
    NSString *cellID;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIRefreshControl *refreshControl = [[UIRefreshControl alloc]init];
    refreshControl.tintColor = [UIColor grayColor];
    refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:@"下拉刷新"];
    
    data = [[NSArray alloc]initWithObjects:@"UILabel  文本标签", @"UIButton  按钮",@"UITextField  输入文本框",@"UITableView  列表",@"UINavigationBar  导航",@"ImageView  图片显示",@"UIStep  计步器",@"UIProgressView  进度条", nil];
    cellID = @"cellID";
    CGRect rect = [[UIScreen mainScreen]bounds];
    tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 44,rect.size.width, rect.size.height - 44) ];
    tableView.delegate = self;
    [tableView setDataSource:self];
    
    tableView.refreshControl =  refreshControl;
    [self.view addSubview:tableView];
    
}

- (void)refreshData{
    //此处可以加一个请求网络 request
    
    //停止刷新
    [tableView.refreshControl endRefreshing];
    //表格数据重新加载
    [tableView reloadData];
    
}


//表视图分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

//分区表头
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return nil;
}
//分区表尾
- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    return nil;
}

//表图视图有多少项
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        //四种不同的风格
        switch (indexPath.row % 4) {
            case 0: //有一个子标题的cell风格
                cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:cellID];
                break;
            case  1: //默认风格,内置一个UILabel和UIImageView
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
                break;
            case 2: //这个风格是设置中cell的风格
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID];
                break;
            case 3: //这种风格是联系人中cell的风格 
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:cellID];
                break;
            default:
                break;
        }
        cell.layer.cornerRadius = 12;
        cell.layer.masksToBounds = YES;
    }
    NSInteger rowNo = indexPath.row;
    cell.textLabel.text = data[rowNo];
    return cell;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

结果如下


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值