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

所属分类: 软件编程 / IOS 阅读数: 559
收藏 0 赞 0 分享

UITableView几乎是iOS开发中用处最广的一个控件,当然也是要记相当多东西的一个控件。

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

201642491802522.png (214×66)

打开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]; 
 // 初始化tableView的数据 
 NSArray *list = [NSArray arrayWithObjects:@"武汉",@"上海",@"北京",@"深圳",@"广州",@"重庆",@"香港",@"台海",@"天津", nil]; 
 self.dataList = list; 
  
 UITableView *tableView = [[[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain] autorelease]; 
 // 设置tableView的数据源 
 tableView.dataSource = self; 
 // 设置tableView的委托 
 tableView.delegate = self; 
 // 设置tableView的背景图 
 tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Background.png"]]; 
 self.myTableView = tableView; 
 [self.view addSubview:myTableView]; 
} 

在初始化的时候,可以为TableView设置样式
第一种:列表 UITableViewStylePlain

201642491911192.png (329×491)

第二种:分组UITableViewStyleGrouped

201642491955065.png (330×493)

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

- (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

201642492019623.png (330×492)

UITableViewCellStyleSubtitle

201642492040034.png (331×491)

UITableViewCellStyleValue1

201642492059266.png (325×492)

UITableViewCellStyleValue2

201642492116014.png (328×489)

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

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
 return 1; 
} 

设置内容缩进

- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
 return [indexPath row]; 
} 

201642492158931.png (324×490)

设置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]; 
 } 
} 

201642492228564.png (333×494)

当选择指定的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]; 
} 

201642492258056.png (331×492)

滑动选择的行后删除

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

201642492330091.png (321×71)

UITableView的刷新:

[self.tableView reloadData];

reloadData是刷新整个UITableView,有时候,我们可能需要局部刷新。比如:只刷新一个cell、只刷新一个section等等。这个时候在调用reloadData方法,虽然用户看不出来,但是有些浪费资源。

刷新局部cell:

复制代码 代码如下:

 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
 [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationFade];

这样就可以很方便的刷新第一个section的第一个cell。虽然看起来代码多了,但是确实比较节省资源。尽量少的刷新,也是UITableView的一种优化。

局部刷新section:

NSIndexSet *indexSet = [[NSIndexSet alloc] initWithIndex:0];
[self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationFade];

上面这段代码是刷新第0个section。

刷新动画:

刷新UITableView还有几个动画:

typedef NS_ENUM(NSInteger, UITableViewRowAnimation) {
 UITableViewRowAnimationFade, //淡入淡出
 UITableViewRowAnimationRight, //从右滑入   // slide in from right (or out to right)
 UITableViewRowAnimationLeft, //从左滑入
 UITableViewRowAnimationTop,  //从上滑入
 UITableViewRowAnimationBottom, //从下滑入
 UITableViewRowAnimationNone,   // available in iOS 3.0
 UITableViewRowAnimationMiddle,   // available in iOS 3.2. attempts to keep cell centered in the space it will/did occupy
 UITableViewRowAnimationAutomatic = 100 // available in iOS 5.0. chooses an appropriate animation style for you
};

更多精彩内容其他人还在看

iOS逆向教程之跟踪函数调用详解

这篇文章主要给大家介绍了关于iOS逆向教程之跟踪函数调用的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
收藏 0 赞 0 分享

iOS App连续闪退时上报crash日志的方法详解

iOS App 有时可能遇到启动必 crash 的绝境:每次打开 App 都闪退,无法正常使用App。下面这篇文章主要给大家介绍了iOS App连续闪退时上报crash日志的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享

如何为Xcode添加删除整行、复制整行及在下方新建一行快捷键详解

xcode是苹果公司向开发人员提供的集成开发环境,开发者们经常会使用到,下面这篇文章主要给大家介绍了关于如何为Xcode添加删除整行、复制整行及在下方新建一行快捷键的相关资料,需要的朋友可以参考下。
收藏 0 赞 0 分享

iOS指纹登录(TouchID)集成方案详解

这篇文章主要为大家详细介绍了iOS指纹登录TouchID的集成方案,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

iOS动态验证码实现代码

本文通过实例代码给大家介绍了ios动态验证码的实现方法,代码简单易懂,非常不错,具有参考借鉴价值,需要的朋友参考下吧
收藏 0 赞 0 分享

iOS模块化开发浅析

本文给大家分析了IOS在模块发开发时候的相关注意点以及简单代码做了分享,有兴趣的朋友参考学习下。
收藏 0 赞 0 分享

iOS中封装.framework及使用的方法详解

这篇文章主要给大家介绍了关于iOS中封装.framework及使用的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧。
收藏 0 赞 0 分享

WKWebView、WebView和JS的交互方式详解

这篇文章主要给大家介绍了关于WKWebView、WebView和JS的交互方式,文中通过示例代码介绍的非常详细,对各位iOS开发者们具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
收藏 0 赞 0 分享

ios wkwebview离线化加载h5资源解决方案

本篇文章主要介绍了ios wkwebview离线化加载h5资源解决方案,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

iOS实现带指引线的饼状图效果(不会重叠)

饼状图对大家来说应该都不陌生,下面这篇文章主要给大家介绍了关于iOS实现带指引线的饼状图效果(不会重叠)的相关资料,文章通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧。
收藏 0 赞 0 分享
查看更多