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

被折叠的 条评论
为什么被折叠?



