`

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

分享到:
评论

相关推荐

    iOS开发之UITableView详解

    在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似于微信、QQ、新浪微博等软件基本上随处都是UITableView。当然它的广泛使用自然离不开它强大的功能,今天这篇...

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

    主要介绍了详解iOS开发中UItableview控件的数据刷新功能的实现,代码基于传统的Objective-C,需要的朋友可以参考下

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

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

    iOS UITableView 与 UITableViewController实例详解

    这些控件其实都是UITableView 对象,可以用来显示一组对象,例如,用户地址薄中的一组人名。  UITableView 对象虽然只能显示一行数据,但是没有行数限制。 •编写新的应用程序 JXHomepwner 应用  创建应用,...

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

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

    详解iOS开发中UIPickerView控件的使用方法

    和其他UITableView控件相似,UIPickerView也需要数据源。 我们要实现的效果如下: 下面开始使用的步骤。 1、打开XCode 4.3.2,新建一个Single View Application ,命名为PickerViewDemo,Company Identifier 为:...

    iOS中UIRefreshControl的基本使用详解

    UIRefreshControl是iOS6自带的UITableView下拉刷新控件。iOS6中,UITableViewController已经内置了UIRefreshControl控件。UIRefreshControl目前只能用于UITableViewController,如果用在其他ViewController中,运行...

    iOS开发之UIScrollView详解

    介绍:UIScrollView用于在一个小范围里显示很大的内容的控件。通过用户平滑、手捏手势,在这个小区域里查看不同内容。是UITableView和UITextView的父类。它是视图,但是比较特殊,可以看成把它看成2层的结构。上面是...

    详解ios11中estimatedRowHeight属性

    相信大家都已经升级了iOS11,而且也做了相应的适配,其中对于tableView这个控件进行适配的时候,比如:集成MJRefresh的时候,当然还有其他很多情况下,很多资料都有说需要把estimatedRowHeight属性设置为0,那么它...

    tableView上面空出20个像素的解决办法

    您可能感兴趣的文章:用JS写的一个TableView控件代码JavaFX之TableView的使用详解UITableView 实现汽车品牌(demo)使用UItableview在iOS应用开发中实现好友列表功能iOS开发中UITableview控件的基本使用及性能优化...

Global site tag (gtag.js) - Google Analytics