终于写到了UITableView,用处最广的一个控件,当然也是要记相当多东西的一个控件。
首选创建一个新的项目,并添加一个MainViewController的Class文件

打开MainViewController.h文件
- @interface MainViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
-
- @property (nonatomic, retain) NSArray *dataList;
- @property (nonatomic, retain) UITableView *myTableView;
-
- @end
TableView的数据源UITableViewDataSource
TableView的委托UITableViewDelegate
如果当前类是继承自UIViewController,需要添加上面的代码,如果直接继承自UITableViewController则不需要添加
然后打MainViewController.m文件,初始化UItableView并显示在当前窗口
- - (void)viewDidLoad
- {
- [super viewDidLoad];
-
- NSArray *list = [NSArray arrayWithObjects:@"武汉",@"上海",@"北京",@"深圳",@"广州",@"重庆",@"香港",@"台海",@"天津", nil];
- self.dataList = list;
-
- UITableView *tableView = [[[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain] autorelease];
-
- tableView.dataSource = self;
-
- tableView.delegate = self;
-
- tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Background.png"]];
- self.myTableView = tableView;
- [self.view addSubview:myTableView];
- }
在初始化的时候,可以为TableView设置样式
第一种:列表 UITableViewStylePlain

第二种:分组UITableViewStyleGrouped

创建并设置每行显示的内容
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- static NSString *CellWithIdentifier = @"Cell";
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellWithIdentifier];
- if (cell == nil) {
- cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellWithIdentifier];
- }
- NSUInteger row = [indexPath row];
- cell.textLabel.text = [self.dataList objectAtIndex:row];
- cell.imageView.image = [UIImage imageNamed:@"green.png"];
- cell.detailTextLabel.text = @"详细信息";
- return cell;
- }
UITableViewCell的样式也是可以进行设置的,如果不能满足项目的需要,可以自己定义UITableViewCell的样式
UITableViewCellStyleDefault

UITableViewCellStyleSubtitle

UITableViewCellStyleValue1

UITableViewCellStyleValue2

分组的TableView还可以进行内容的分段,是通过下面的方法实现,返回的数字1代表分为1段
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- {
- return 1;
- }
设置内容缩进
- - (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- return [indexPath row];
- }

设置cell的行高
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- return 70;
- }
设置cell的隔行换色
- - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
- {
- if ([indexPath row] % 2 == 0) {
- cell.backgroundColor = [UIColor blueColor];
- } else {
- cell.backgroundColor = [UIColor greenColor];
- }
- }

当选择指定的cell时,弹出UIAlertView显示选择的内容
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- {
-
- NSString *msg = [[NSString alloc] initWithFormat:@"你选择的是:%@",[self.dataList objectAtIndex:[indexPath row]]];
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:msg delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
- [msg release];
- [alert show];
- }

滑动选择的行后删除
- - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
- {
- NSLog(@"执行删除操作");
- }

DEMO下载
http://pan.baidu.com/share/link?shareid=77810&uk=101519637
相关推荐
iOS 开发中 UITableView 的使用详解 UITableView 是 iOS 开发中最常用的控件之一,用于显示列表数据。它类似于 Android 中的 ListView,都是用于显示列表数据的控件。在 iOS 开发中,UITableView 是一个非常重要的...
UITableView几乎是iOS开发中用处最广的一个控件,当然也是要记相当多东西的一个控件。 创建 首先创建一个新的项目,并添加一个MainViewController的Class文件 打开MainViewController.h文件 @interface ...
### IPhone之UITableView详解 #### 一、前言 UITableView 是 iOS 开发中非常重要的一个控件,用于显示数据列表。相比于 UIButton 等简单控件,UITableView 的使用较为复杂,涉及较多的概念与方法。本文将详细介绍 ...
对于初学者,这是一个绝佳的学习资源,能够快速掌握iOS控件的基础知识。对于经验丰富的开发者,它则提供了一个快速参考和测试控件特性的平台。 总的来说,UICatalog是iOS开发者的必备参考资料,它以直观和实践的...
UITableView是iOS开发中不可或缺的一部分,它是苹果iOS SDK提供的一种用于展示数据列表的视图控件。在本篇文章中,我们将深入探讨UITableView的核心知识点,包括如何删除、添加、上移单元格,以及如何设置分组和分组...
在iOS开发中,UITableView是一种非常常见的控件,用于展示列表数据。`UITableView`的`cell`展开和收缩功能是提高用户体验的重要特性,特别是在处理层级结构数据时。标题"ios uitableview cell的展开收缩功能"涉及到...
在iOS开发中,UITableView是用于显示列表数据的核心控件,它允许用户滚动浏览和交互。本教程将深入探讨如何在Objective-C环境下实现UITableView的数据刷新功能。我们将通过一个具体的实例——英雄展示界面,来讲解...
PNChart是一个美观的图表库,常用于数据可视化,而UITableView则是展示列表数据的标准控件。本文将深入探讨如何在iOS项目中实现PNChart与UITableView之间的联动效果,以便更好地展示和操作数据。 首先,当用户点击...
#### 二、UITableView 控件详解 **2.1 设置组间间距** 在 UITableView 中,可以通过设置 `sectionHeaderHeight` 和 `sectionFooterHeight` 来调整不同组之间的间距。 示例代码: ```swift tableView....
- **UITableView** 和 **UICollectionView** 分别是用于展示列表和网格布局的iOS控件。 - 掌握这两个控件的使用方法可以帮助开发者更好地呈现数据,并实现丰富的交互效果。 ### 7. iOS与Android平台对比 **知识点...
在iOS应用开发中,UICollectionView是一种强大的控件,用于创建自定义布局和展示复杂的数据集合。在本篇文章中,我们将深入探讨如何使用UICollectionView来构建一个可拖动的桌面样式的界面,这将提升用户交互的体验...
iOS系统提供了多种内置控件,如`UITextView`、`UITextField`和`UIWebView`,它们已经集成了基本的复制、剪切和粘贴功能。除此之外,开发人员可以通过UIKit框架提供的API来自定义剪贴板的操作。 `UIPasteboard`是iOS...
在iOS中,我们可以用工厂方法来动态地创建UI控件或模型对象。例如,创建不同类型的UIButton: ```swift enum ButtonType { case custom, system } func createButton(for type: ButtonType) -> UIButton { ...
在iOS开发中,UIScrollView是一个至关重要的控件,它允许用户查看和交互超出了屏幕边界的内容。这个控件的出现解决了移动设备屏幕尺寸有限的问题,使得应用程序可以显示大量信息而无需用户频繁地翻页或者切换视图。...
- **UITableView基础**:UITableView是iOS应用中最常用的控件之一,用于展示列表形式的数据。学会如何创建、配置和填充数据到表视图。 - **数据源与代理模式**:UITableView通过数据源(UITableViewDataSource)获取...
2. ** UITableView与UICollectionView**:这两个组件是iOS应用中常用的列表展示控件。UITableView通常用于显示一维数据,如商品列表;UICollectionView则可以展示更复杂的二维或多维度布局,如商品分类页面。 3. **...
1. 视图层次结构:利用UIStackView、UITableView或UICollectionView等控件,构建游戏棋盘布局,展示可点击的图像单元格。 2. 触摸事件:通过UIResponder链捕获用户的触摸事件,识别用户点击的图像并执行相应的匹配...
下面主要是介绍在UITableView下的使用。 使用 在github上下载之后,将MJRefresh文件添加到项目中,并且在需要使用的文件上引入MJRefresh.h。然后在该文件的viewDidLoad方法中指定tableView的header和footer,如下:...
《iOS平台上的闹钟应用开发详解》 在iOS平台上,开发一款功能丰富的闹钟应用是一项技术性和创新性并存的任务。本项目名为“GeiniableClock”,它不仅提供了基础的闹钟功能,还集成了天气预报显示、城市选择、自定义...