简单的TableView
背景知识
每个表都是UITableView的实例,表中的每一行都是UITableViewCell的实例。
TableView的种类
- Grouped table
- Plain table without index
- Plain table with index

NSIndexPath
- NSIndexPath.section 返回int,表示第几个Section
- NSIndexPath.row 返回int,表示该Section下的第几行
UITableViewCell包含的元素
- Image (imageView.image)
- Text Label (textLabel.text, textLabel.font)
- Detail Text Label (detailTextLabel.text)
UITableViewCell样式
- UITableViewCellStyleDefault //左文,图(如果有则最左)
- UITableViewCellStyleSubtitile //左文,图(如果有则最左),detailText在Text下面
- UITableViewCellStyleValue1 //左文,图(如果有则最左),detailText在最右
- UITableViewCellStyleValue2 //左文,图(如果有则最左),detailText在Text右边

简单例子
StoryBoard拖入一个TableView,然后设置DataSource和Delegate为ViewController。
ViewController.h声明协议
#import <UIKit/UIKit.h> @interface XYZViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>
@end
ViewController.m文件如下
//
// XYZViewController.m
// TableView
//
// Created by Norcy on 14-7-24.
// Copyright (c) 2014年 QQLive. All rights reserved.
// #import "XYZViewController.h" @interface XYZViewController ()
@property (strong, nonatomic)NSArray *array;
@end @implementation XYZViewController - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. self.array = [[NSArray alloc] initWithObjects:@"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", nil];
} - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} #pragma mark -
#pragma mark Data Source Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return ;
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.array count];
} #pragma mark Delegate Methods
- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellId = @"MyCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId]; if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellId];
} cell.textLabel.text = self.array[indexPath.row]; UIImage *image = [UIImage imageNamed:@"1.png"]; cell.imageView.image = image; cell.detailTextLabel.text = @"Details"; return cell;
} - (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
{
return indexPath.row;
}
@end
代码说明
代码说明1:
static NSString *CellID = @"MyCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellID];
}
从ReusableCell的队列中取出带有Identifier标识的Cell,如果Cell为空,则新创建一个。
ReusableCell队列是专门存放那些生成过的,但是后来由于滚动tableView而隐藏起来的cell。这样做可以节约资源。
注意CellIdentifier这个参数是可以自定义的,如果使用storyboard的话需要跟storyboard中的cell的Identifier相同。
- dequeueReusableCellWithIdentifier: 可能返回nil(没有为TableView注册Cell的情况返回nil)
- dequeueReusableCellWithIdentifier:forIndexPath: 不可能返回nil
所以如果使用dequeueReusableCellWithIdentifier:forIndexPath的话就不用检查nil的情况了。
But,以上这种写法已经过时了。推荐最新写法
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:CELL_ID];
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CELL_ID forIndexPath:indexPath];
如果TableView已经注册了Cell,那么dequeueReusableCellWithIdentifier:一定不为空;因为当系统从重用队列中取cell取不到的时候,会自动帮我们生成并初始化一个cell。
所以建议直接用registerClass:forCellReuseIdentifier:CELL_ID + dequeueReusableCellWithIdentifier:forIndexPath: 这个组合。
代码说明2:
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
{
return indexPath.row;
}
设置缩进。
代码说明3:
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == )
return nil;
else
return indexPath;
}
选中某行之前调用,返回nil表示该行不能被选择,返回indexPath表示可以继续选择(可以返回其他路径但最好不要更改用户的选择,所以一般返回nil或indexPath来表示禁止或允许某个选择)
代码说明4:
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
选中某行之后调用,一般要调用deselectRowAtIndexPath
代码说明5:
cell.textLabel.font = [UIFont boldSystemFontOfSize:]; - (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return ;
}
设置TableViewCell的行高以适应TableViewCell的字体
附录,联系人Demo(Based in IOS7.0) or 直接查看主要代码
//
// ViewController.m
// Contact
//
// Created by Norcy on 15/4/16.
// Copyright (c) 2015年 Norcy. All rights reserved.
// #import "ViewController.h" @interface ViewController ()
{
}
@property (strong, nonatomic) NSDictionary* names;
@property (strong, nonatomic) NSArray* keys;
@end NSMutableArray* filteredNames;
UISearchDisplayController* searchController;
static NSString* CELL_ID = @"MyCell"; @implementation ViewController - (void)viewDidLoad
{
[super viewDidLoad]; //Screen
int screenWidth = [[UIScreen mainScreen] bounds].size.width;
int screenHeight = [[UIScreen mainScreen] bounds].size.height; //Filter
filteredNames = [[NSMutableArray alloc] init]; //TableView
UITableView* tableView = [[UITableView alloc] initWithFrame:CGRectMake(, , screenWidth, screenHeight)]; [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:CELL_ID]; tableView.delegate = self; tableView.dataSource = self; tableView.tag = ; UIEdgeInsets insets = tableView.contentInset; //设置与屏幕顶部的距离,防止被状态栏挡住 insets.top = ; [tableView setContentInset:insets]; //Search Bar
UISearchBar* searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(, , screenWidth, screenHeight / )]; tableView.tableHeaderView = searchBar; searchController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self]; searchController.delegate = self; searchController.searchResultsDataSource = self; //Names and Keys
NSString* path = [[NSBundle mainBundle] pathForResource:@"sortednames" ofType:@"plist"]; self.names = [NSDictionary dictionaryWithContentsOfFile:path]; self.keys = [[self.names allKeys] sortedArrayUsingSelector:@selector(compare:)]; [self.view addSubview:tableView];
} #pragma mark -
#pragma mark Data Source Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
if (tableView.tag == )
return [self.keys count];
else
return ;
} - (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView.tag == )
{
NSString* key = self.keys[section];
NSArray* curSection = self.names[key];
return [curSection count];
}
else
return [filteredNames count];
} - (NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section
{
if (tableView.tag == )
return self.keys[section];
else
return nil;
} - (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CELL_ID forIndexPath:indexPath]; // UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CELL_ID]; // if (cell == nil)
// {
// NSLog(@"asd");
// cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CELL_ID];
// } if (tableView.tag == )
{
NSString* key = self.keys[indexPath.section]; NSArray* curSection = self.names[key]; cell.textLabel.text = curSection[indexPath.row];
}
else
cell.textLabel.text = filteredNames[indexPath.row]; return cell;
} #pragma mark Delegate Methods
//显示索引
- (NSArray*)sectionIndexTitlesForTableView:(UITableView*)tableView
{
if (tableView.tag == )
return self.keys;
else
return nil;
} - (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
} #pragma mark Search Delegate Methods
- (void)searchDisplayController:(UISearchDisplayController*)controller didLoadSearchResultsTableView:(UITableView*)tableView
{
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:CELL_ID];
} - (BOOL)searchDisplayController:(UISearchDisplayController*)controller shouldReloadTableForSearchString:(NSString*)searchString
{
[filteredNames removeAllObjects]; if (searchString.length > )
{
NSPredicate* predicate = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary* bindings) {
NSRange range = [evaluatedObject rangeOfString:searchString options:NSCaseInsensitiveSearch];
return range.location != NSNotFound;
}]; for (NSString* key in self.keys)
{
NSArray* matches = [self.names[key] filteredArrayUsingPredicate:predicate]; [filteredNames addObjectsFromArray:matches];
}
} return YES;
} @end
简单的TableView的更多相关文章
- IOS中TableView的使用(1) -创建一个简单的tableView
创建一个简单的tableView: #import <UIKit/UIKit.h> /*tableView 一定要遵守这两个协议: UITableViewDataSource,UITabl ...
- 【转】ios tableView那些事(一)创建一个简单的tableView
工作也有半年多了!几乎每个项目中的会用到tableview这个神奇而好用的控件,在学习和工作中都会看别人的博客!对我有很大的帮助,就如同站在巨人的肩膀上的感觉吧 哈哈!于是决定重新开始写博客,希望能帮 ...
- iOS学习笔记(5)——显示简单的TableView
1. 创建工程 创建一个新的Xcode工程命名为SimpleTableTest. 删除main.storyboard文件和info.plist中有关storyboard的相关属性. 按command+ ...
- tableView
Table View简单描述: 在iPhone和其他iOS的很多程序中都会看到Table View的出现,除了一般的表格资料展示之外,设置的属性资料往往也用到Table View,Table View ...
- iOS学习之Table View的简单使用
Table View简单描述: 在iPhone和其他iOS的很多程序中都会看到Table View的出现,除了一般的表格资料展示之外,设置的属性资料往往也用到Table View,Table View ...
- TableView不显示没内容的Cell怎么办?
类似这种,我不想让下面那些空的显示. 很简单: self.tableView.tableFooterView = [[UIView alloc] init]; 加完这句之后就变成了这样:
- Swift初窥--使用Swift实现TableView
完毕Swift的语法关之后.来点实际的Task,第一个任务是写一个tableview,使用cocoaTouch里tableview这个经常使用的控件. 创建project.选择Swift语言 首先是用 ...
- OC开发_代码片段——代码编写简单的tableViewCell
许久前写的简单的tableView例子,主要针对处理缓存.协议.数据源datasource.局部刷新等问题进行解析. 其实这是一篇不全面的记录,只是用来记录一些备忘的东西,更全面的是使用TablVie ...
- ***iOS学习之Table View的简单使用和DEMO示例(共Plain普通+Grouped分组两种)
Table View简单描述: 在iPhone和其他iOS的很多程序中都会看到Table View的出现,除了一般的表格资料展示之外,设置的属性资料往往也用到Table View,Table View ...
随机推荐
- python&php数据抓取、爬虫分析与中介,有网址案例
近期在做一个网络爬虫程序.后台使用python不定时去抓取数据.前台使用php进行展示 站点是:http://se.dianfenxiang.com
- Sybase数据库应用系统调优的五大领域
Sybase数据库应用系统调优的五大领域 2011/3/14/13:49来源:慧聪it网 本 文以“某大型商业银行的网上银行系统”这一很具有典型意义的企业级大型Sybase数据库应用系统为例,涉及了数 ...
- Failure to transfer org.apache.maven.plugins:maven-resources-plugin:pom:2.7
导入maven项目时,pom.xml文件报错如下: Failure to transfer org.apache.maven.plugins:maven-resources-plugin:pom:2. ...
- spring 代理 演变过程
动态代理演变 拿JDBC开事务举例子 最初 写代码 每个CUD 都需要开启事务 所以出现很多累赘代码 因此提出静态代理的构想,把事务交给后台做,程序员只需要 调用update(sql)就行了,upda ...
- python 安装 pyinstall 编译exe文件
$ pip install future 安装PyInstaller之前需确认首先安装了pywin32 下载地址:http://nchc.dl.sourceforge.net/project/pywi ...
- H5 获取地理位置
只能通过手机浏览器访问,并且用户必须允许访问才可以生效 <!doctype html> <html> <head> <title>Mobile Cook ...
- atitit.提取zip rar文件列表 java php c# 的原理与设计
atitit.java提取zip rar文件列表 1. 取zip rar文件的场景问题 1 1.1. 多重压缩的问题 1 1.2. 文件名编码的问题 1 1.3. 目录的判定 1 2. rar的解析 ...
- vue 插件
开发插件 插件通常会为vue添加全局功能,插件的范围没有限制--一般有下面几种: 1,添加全局方法或者属性,例:vue-coustom-element 2,添加全局资源:指令.过滤器,.过渡等,如vu ...
- 如何在uboot上实现从网络下载版本镜像并直接在内存中加载之?
这是作者近期项目上遇到的一个需求,描述如下: 一块MT7620N的路由器单板,Flash中已存放一个版本并可以通过uboot正常加载并启动.现在需要:在uboot上电启动过程中,通过外部按键触发干涉, ...
- Yii2 Api认证和授权(翻译)
Authentication 认证 RESTful Api 是无状态的, 因此这意味着不能使用 sessions && cookies. 因此每一个请求应该带有一些 authentic ...