背景知识

每个表都是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的更多相关文章

  1. IOS中TableView的使用(1) -创建一个简单的tableView

    创建一个简单的tableView: #import <UIKit/UIKit.h> /*tableView 一定要遵守这两个协议: UITableViewDataSource,UITabl ...

  2. 【转】ios tableView那些事(一)创建一个简单的tableView

    工作也有半年多了!几乎每个项目中的会用到tableview这个神奇而好用的控件,在学习和工作中都会看别人的博客!对我有很大的帮助,就如同站在巨人的肩膀上的感觉吧 哈哈!于是决定重新开始写博客,希望能帮 ...

  3. iOS学习笔记(5)——显示简单的TableView

    1. 创建工程 创建一个新的Xcode工程命名为SimpleTableTest. 删除main.storyboard文件和info.plist中有关storyboard的相关属性. 按command+ ...

  4. tableView

    Table View简单描述: 在iPhone和其他iOS的很多程序中都会看到Table View的出现,除了一般的表格资料展示之外,设置的属性资料往往也用到Table View,Table View ...

  5. iOS学习之Table View的简单使用

    Table View简单描述: 在iPhone和其他iOS的很多程序中都会看到Table View的出现,除了一般的表格资料展示之外,设置的属性资料往往也用到Table View,Table View ...

  6. TableView不显示没内容的Cell怎么办?

    类似这种,我不想让下面那些空的显示. 很简单: self.tableView.tableFooterView = [[UIView alloc] init]; 加完这句之后就变成了这样:

  7. Swift初窥--使用Swift实现TableView

    完毕Swift的语法关之后.来点实际的Task,第一个任务是写一个tableview,使用cocoaTouch里tableview这个经常使用的控件. 创建project.选择Swift语言 首先是用 ...

  8. OC开发_代码片段——代码编写简单的tableViewCell

    许久前写的简单的tableView例子,主要针对处理缓存.协议.数据源datasource.局部刷新等问题进行解析. 其实这是一篇不全面的记录,只是用来记录一些备忘的东西,更全面的是使用TablVie ...

  9. ***iOS学习之Table View的简单使用和DEMO示例(共Plain普通+Grouped分组两种)

    Table View简单描述: 在iPhone和其他iOS的很多程序中都会看到Table View的出现,除了一般的表格资料展示之外,设置的属性资料往往也用到Table View,Table View ...

随机推荐

  1. 图片转成base64, base64转成图片

    1.我们在看代码时经常在img或css背景图片中看到: src=”data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAHgAAAB4CAMAAAAOusbgA ...

  2. struts2+jquery验证注冊用户是否存在

    注冊界面 register.jsp <%@ page language="java" contentType="text/html; charset=UTF-8&q ...

  3. 【Datastage】函数大全

    一. 类型转换函数 类型转换函数用于更改参数的类型. 以下函数位于表达式编辑器的"类型转换"类别中.方括号表示参数是可选的.缺省日期格式为 %yyyy-%mm-%dd. 以下示例按 ...

  4. 6. Laravel5学习笔记:IOC/DI的理解

    介绍 IOC 控制反转 Inversion of Control 依赖关系的转移 依赖抽象而非实践 DI 依赖注入 Dependency Injection 不必自己在代码中维护对象的依赖 容器自己主 ...

  5. IOS后台文件上传

    public ModelAndView GetImage(HttpServletRequest request,   HttpServletResponse response) throws Exce ...

  6. Linux 目录和文件操作

    Linux常用命令--目录和文件操作 [目录]删除.复制.移动 : 1.删除文件夹用:rmdir 文件夹名 但是rmdir不能删除非空的文件夹,那如何删除非空文件夹呢: 2.通常情况下,删除文件用:r ...

  7. 【转】C# 视频监控系列(13):H264播放器——控制播放和截图

    本文原文地址:http://www.cnblogs.com/over140/archive/2009/03/30/1421531.html 阿里云栖社区也有相关的视频开发案例:https://yq.a ...

  8. java基础讲解01-----简单了解一下java

    2017-4-12(晚)  闲来无事,静静把自己了解的java,好好回忆一次.如果能帮助别人也好.会不断补充 java有关的书籍真的很多,我也不过多的介绍了. 1.什么是java 2.java的语言特 ...

  9. 转: python 利用EMQ实现消费者和生产者模型

    消费者 """ 测试emq-消费者 @author me """ import paho.mqtt.client as mqtt impor ...

  10. mysql安装在centos7报错ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

    原文链接:http://blog.csdn.net/kuluzs/article/details/51924086 [问题]:mysql版本:5.7.13 首次在centos下安装MySQL,客户端连 ...