基本术语:我们把Table View的一行称为Cell,多个Cell组成一个Section,每个Section上下有Header和Footer,多个Section组成了整个Table ,Table也有Header和Footer。
Table View主要分为以下两种:Plain 普通的列表风格 和 Grouped分块/组风格。
---------------------------------------------------------------------------------------
先做一个普通列表风格的例子:
1、新建项目
打开ViewController.xib文件,往ViewController.xib界面上拖拽一个Table View控件到现有的View上,对齐。
#import <UIKit/UIKit.h>
@property (strong, nonatomic) NSArray *list;
@end
5、打开.m文件,添加:
@synthesize list = _list;
这是发现有两个警告,提示未完成的实现,这提示的是UITableViewDelegate, UITableViewDataSource这个两个头文件里的协议的方法未实现。待会我们去实现它。
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSArray *array = [[NSArray alloc] initWithObjects:@"美国", @"菲律宾",
@"黄岩岛", @"中国", @"泰国", @"越南", @"老挝",
@"日本" , nil];
self.list = array;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
self.list = nil;
}
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. NSArray *array = [[NSArray alloc] initWithObjects:@"美国", @"菲律宾", @"黄岩岛", @"中国", @"泰国", @"越南", @"老挝", @"日本" , nil]; self.list = array; } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. self.list = nil; }
7、生成row
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *TableSampleIdentifier = @"TableSampleIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
TableSampleIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:TableSampleIdentifier];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [self.list objectAtIndex:row];
return cell;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *TableSampleIdentifier = @"TableSampleIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: TableSampleIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableSampleIdentifier]; } NSUInteger row = [indexPath row]; cell.textLabel.text = [self.list objectAtIndex:row]; return cell; }
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: TableSampleIdentifier];
这个语句根据标识符TableSampleIdentifier寻找当前可以重用的UITableViewCell。当某行滑出当前可见区域后,我们重用它所对应的UITableViewCell对象,那么就可以节省内存和资源。
注意参数(NSIndexPath *)indexPath,它将行号row和部分号section合并了,通过[indexPath row];获取行号。之后给cell设置其文本:
cell.textLabel.text = [self.list objectAtIndex: row];
8、现在一个简单的TableView就弄好看,运行下看效果
cell.textLabel.text = [self.list objectAtIndex:row];
UIImage *image = [UIImage imageNamed:@"qq"];
cell.imageView.image = image;
UIImage *highLighedImage = [UIImage imageNamed:@"youdao"];
cell.imageView.highlightedImage = highLighedImage;
return cell;
NSUInteger row = [indexPath row]; cell.textLabel.text = [self.list objectAtIndex:row]; UIImage *image = [UIImage imageNamed:@"qq"]; cell.imageView.image = image; UIImage *highLighedImage = [UIImage imageNamed:@"youdao"]; cell.imageView.highlightedImage = highLighedImage; return cell;
效果如下:
UITableViewCellStyleDefault
UITableViewCellStyleSubtle
UITableViewCellStyleValue1
UITableViewCellStyleValue2
cell.detailTextLabel.text =@"打打打打";
return cell;
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
选中是做个提示,提示选中了那个信息,代码实现如下:
NSString *rowString = [self.list objectAtIndex:[indexPath row]];
UIAlertView * alter = [[UIAlertView alloc] initWithTitle:@"选中的行信息" message:rowString delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alter show];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ NSString *rowString = [self.list objectAtIndex:[indexPath row]]; UIAlertView * alter = [[UIAlertView alloc] initWithTitle:@"选中的行信息" message:rowString delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; [alter show]; }
效果:
难是难在如何凑成那些暂时数据。
相关推荐
swift---tableview
RxSwift基础控件的使用、RxSwif-Tableview的使用、RxSwift-SectionTableview结合RxDataSources的使用、RxSwift 网络请求封装的使用
OC-串行-BreakPoint-tableview
Swift-串行-BreakPoint-tableview
类似百度外卖首页---tableview内嵌套collectionview,代码抽离分类,首页控制器瘦身,代码剥离
Swift-TableView-Example, 关于Swift语言的简单UITableView应用 Swift-TableView-ExampleSwift(3.2) 语言的简单 UITableView 应用。 使用的自定义单元格和结构。要求Xcode 8 iOS 10 应用程序截图
ios--tableview加入购物车飞入效果;
ios中最重要的一个控件tableView的各种使用,包括自定义,字典的各种使用(涵7套代码),希望各位屌丝能用的上
ios-tableview-上拉加载更多,可以直接下载后再xcode运行
react-native-tableview, 本机 iOS UITableView支持JSON支持和更多的本地 响应本机代码本机 iOS UITableView支持JSON支持本机 电子邮件内容特性安装工具样式示例自定义插件特性iOS ,因为它是 ! ( 具有组/普通...
iOS-TableView下拉放大效果
让Tableview 每个Section拥有圆角加阴影
最新-TableView汽车品牌导航(图标和plist文件),可以让你得到很多汽车的信息。
Swift 写的tableview 的学习;tableview 的数据来源--http://apistore.baidu.com/apiworks/servicedetail/987.html 数据请求用Alamofire框架和AFNetworking 同一个作者
IOS开发-TableView表视图LV2 源码 http://write.blog.csdn.net/postedit/9061845
IOS开发-TableView表视图基础 源码下载 http://blog.csdn.net/lmnotlm/article/details/9035481
UITableViewDataSource,UITableViewDelegate中的代理方法
TableView For Android TableView is a powerful Android library for displaying complex data structures and rendering tabular data composed of rows, columns and cells. TableView relies on a separate...
Tableview测试
tableView联动oc和swift