`

IOS控件UITableView详解(转载)

 
阅读更多

终于写到了UITableView,用处最广的一个控件,当然也是要记相当多东西的一个控件。

首选创建一个新的项目,并添加一个MainViewController的Class文件

打开MainViewController.h文件

 

[cpp] view plain copy
  1. @interface MainViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>  
  2.   
  3. @property (nonatomic, retain) NSArray *dataList;  
  4. @property (nonatomic, retain) UITableView *myTableView;  
  5.   
  6. @end  

 

 

TableView的数据源UITableViewDataSource

TableView的委托UITableViewDelegate

如果当前类是继承自UIViewController,需要添加上面的代码,如果直接继承自UITableViewController则不需要添加

然后打MainViewController.m文件,初始化UItableView并显示在当前窗口

 

[cpp] view plain copy
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     // 初始化tableView的数据  
  5.     NSArray *list = [NSArray arrayWithObjects:@"武汉",@"上海",@"北京",@"深圳",@"广州",@"重庆",@"香港",@"台海",@"天津", nil];  
  6.     self.dataList = list;  
  7.       
  8.     UITableView *tableView = [[[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain] autorelease];  
  9.     // 设置tableView的数据源  
  10.     tableView.dataSource = self;  
  11.     // 设置tableView的委托  
  12.     tableView.delegate = self;  
  13.     // 设置tableView的背景图  
  14.     tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Background.png"]];  
  15.     self.myTableView = tableView;  
  16.     [self.view addSubview:myTableView];  
  17. }  

 

 

在初始化的时候,可以为TableView设置样式

第一种:列表 UITableViewStylePlain

第二种:分组UITableViewStyleGrouped

 

创建并设置每行显示的内容

 

[cpp] view plain copy
  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     static NSString *CellWithIdentifier = @"Cell";  
  4.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellWithIdentifier];  
  5.     if (cell == nil) {  
  6.         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellWithIdentifier];  
  7.     }  
  8.     NSUInteger row = [indexPath row];  
  9.     cell.textLabel.text = [self.dataList objectAtIndex:row];  
  10.     cell.imageView.image = [UIImage imageNamed:@"green.png"];  
  11.     cell.detailTextLabel.text = @"详细信息";  
  12.     return cell;  
  13. }  
UITableViewCell的样式也是可以进行设置的,如果不能满足项目的需要,可以自己定义UITableViewCell的样式

 

 

UITableViewCellStyleDefault


 

UITableViewCellStyleSubtitle


 

UITableViewCellStyleValue1


 

UITableViewCellStyleValue2


 

分组的TableView还可以进行内容的分段,是通过下面的方法实现,返回的数字1代表分为1段

 

[cpp] view plain copy
  1. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView  
  2. {  
  3.     return 1;  
  4. }  

设置内容缩进

 

 

[cpp] view plain copy
  1. - (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     return [indexPath row];  
  4. }  

 

设置cell的行高

 

[cpp] view plain copy
  1. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     return 70;  
  4. }  
设置cell的隔行换色

 

 

[cpp] view plain copy
  1. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     if ([indexPath row] % 2 == 0) {  
  4.         cell.backgroundColor = [UIColor blueColor];  
  5.     } else {  
  6.         cell.backgroundColor = [UIColor greenColor];  
  7.     }  
  8. }  


当选择指定的cell时,弹出UIAlertView显示选择的内容

 

 

[cpp] view plain copy
  1. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.       
  4.     NSString *msg = [[NSString alloc] initWithFormat:@"你选择的是:%@",[self.dataList objectAtIndex:[indexPath row]]];  
  5.     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:msg delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];  
  6.     [msg release];  
  7.     [alert show];  
  8. }  


滑动选择的行后删除

 

[cpp] view plain copy
  1. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     NSLog(@"执行删除操作");  
  4. }  

 

DEMO下载

http://pan.baidu.com/share/link?shareid=77810&uk=101519637

分享到:
评论

相关推荐

    UITableView、UITableView基本用法、UITableView详解

    iOS 开发中 UITableView 的使用详解 UITableView 是 iOS 开发中最常用的控件之一,用于显示列表数据。它类似于 Android 中的 ListView,都是用于显示列表数据的控件。在 iOS 开发中,UITableView 是一个非常重要的...

    详解iOS App中UITableView的创建与内容刷新

    UITableView几乎是iOS开发中用处最广的一个控件,当然也是要记相当多东西的一个控件。 创建 首先创建一个新的项目,并添加一个MainViewController的Class文件 打开MainViewController.h文件 @interface ...

    iphone UITableView详解 带实例 例子

    ### IPhone之UITableView详解 #### 一、前言 UITableView 是 iOS 开发中非常重要的一个控件,用于显示数据列表。相比于 UIButton 等简单控件,UITableView 的使用较为复杂,涉及较多的概念与方法。本文将详细介绍 ...

    IOS开发控件源代码 UICatalog

    对于初学者,这是一个绝佳的学习资源,能够快速掌握iOS控件的基础知识。对于经验丰富的开发者,它则提供了一个快速参考和测试控件特性的平台。 总的来说,UICatalog是iOS开发者的必备参考资料,它以直观和实践的...

    UITableView详解

    UITableView是iOS开发中不可或缺的一部分,它是苹果iOS SDK提供的一种用于展示数据列表的视图控件。在本篇文章中,我们将深入探讨UITableView的核心知识点,包括如何删除、添加、上移单元格,以及如何设置分组和分组...

    ios uitableview cell的展开收缩功能

    在iOS开发中,UITableView是一种非常常见的控件,用于展示列表数据。`UITableView`的`cell`展开和收缩功能是提高用户体验的重要特性,特别是在处理层级结构数据时。标题"ios uitableview cell的展开收缩功能"涉及到...

    详解iOS开发中UItableview控件的数据刷新功能的实现

    在iOS开发中,UITableView是用于显示列表数据的核心控件,它允许用户滚动浏览和交互。本教程将深入探讨如何在Objective-C环境下实现UITableView的数据刷新功能。我们将通过一个具体的实例——英雄展示界面,来讲解...

    iOS中PNChart与UITableView的联动示例详解

    PNChart是一个美观的图表库,常用于数据可视化,而UITableView则是展示列表数据的标准控件。本文将深入探讨如何在iOS项目中实现PNChart与UITableView之间的联动效果,以便更好地展示和操作数据。 首先,当用户点击...

    iOSUI基础控件常用方法探微

    #### 二、UITableView 控件详解 **2.1 设置组间间距** 在 UITableView 中,可以通过设置 `sectionHeaderHeight` 和 `sectionFooterHeight` 来调整不同组之间的间距。 示例代码: ```swift tableView....

    iOS成长之路2017夏v1.0

    - **UITableView** 和 **UICollectionView** 分别是用于展示列表和网格布局的iOS控件。 - 掌握这两个控件的使用方法可以帮助开发者更好地呈现数据,并实现丰富的交互效果。 ### 7. iOS与Android平台对比 **知识点...

    使用iOS控件UICollectionView生成可拖动的桌面的实例

    在iOS应用开发中,UICollectionView是一种强大的控件,用于创建自定义布局和展示复杂的数据集合。在本篇文章中,我们将深入探讨如何使用UICollectionView来构建一个可拖动的桌面样式的界面,这将提升用户交互的体验...

    iOS软件开发之详解剪贴板.pdf

    iOS系统提供了多种内置控件,如`UITextView`、`UITextField`和`UIWebView`,它们已经集成了基本的复制、剪切和粘贴功能。除此之外,开发人员可以通过UIKit框架提供的API来自定义剪贴板的操作。 `UIPasteboard`是iOS...

    iOS涉及到的设计模式代码方式详解

    在iOS中,我们可以用工厂方法来动态地创建UI控件或模型对象。例如,创建不同类型的UIButton: ```swift enum ButtonType { case custom, system } func createButton(for type: ButtonType) -&gt; UIButton { ...

    iOS开发之UIScrollView控件详解

    在iOS开发中,UIScrollView是一个至关重要的控件,它允许用户查看和交互超出了屏幕边界的内容。这个控件的出现解决了移动设备屏幕尺寸有限的问题,使得应用程序可以显示大量信息而无需用户频繁地翻页或者切换视图。...

    一步一步学习ios

    - **UITableView基础**:UITableView是iOS应用中最常用的控件之一,用于展示列表形式的数据。学会如何创建、配置和填充数据到表视图。 - **数据源与代理模式**:UITableView通过数据源(UITableViewDataSource)获取...

    iOS高仿淘宝商城

    2. ** UITableView与UICollectionView**:这两个组件是iOS应用中常用的列表展示控件。UITableView通常用于显示一维数据,如商品列表;UICollectionView则可以展示更复杂的二维或多维度布局,如商品分类页面。 3. **...

    IOS连连看demo

    1. 视图层次结构:利用UIStackView、UITableView或UICollectionView等控件,构建游戏棋盘布局,展示可点击的图像单元格。 2. 触摸事件:通过UIResponder链捕获用户的触摸事件,识别用户点击的图像并执行相应的匹配...

    iOS上下拉刷新控件MJRefresh使用方法详解

    下面主要是介绍在UITableView下的使用。 使用 在github上下载之后,将MJRefresh文件添加到项目中,并且在需要使用的文件上引入MJRefresh.h。然后在该文件的viewDidLoad方法中指定tableView的header和footer,如下:...

    闹钟app源码 ios

    《iOS平台上的闹钟应用开发详解》 在iOS平台上,开发一款功能丰富的闹钟应用是一项技术性和创新性并存的任务。本项目名为“GeiniableClock”,它不仅提供了基础的闹钟功能,还集成了天气预报显示、城市选择、自定义...

Global site tag (gtag.js) - Google Analytics