UITableView的创建及其一些常用方法
UITableView,它的数据源继承于UITableViewDataSource,它的委托UITableViewDelegate。
一、UITableView的创建
1.代码方式:
UITableView *tableView=[[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
tableView.backgroundColor=[UIColor grayColor];
[self.view addSubview:tableView];
2.Main.storyboard方式:

二、TableView的实现
#import <UIKit/UIKit.h> @interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate> @end
#import "ViewController.h" @interface ViewController ()
//为tableView提供数据
@property(nonatomic,strong)NSDictionary *dic;
@property(nonatomic,retain)NSArray *arr1;
@property(nonatomic,retain)NSArray *arr2;
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
UITableView *tableView=[[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
// 设置tableView的委托
tableView.delegate=self;
// 设置tableView的数据源
tableView.dataSource=self;
// 设置tableView的背景颜色
tableView.backgroundColor=[UIColor greenColor];
[self.view addSubview:tableView];
_arr1=@[@"张三",@"李四"];
_arr2=@[@"刘老师",@"张老师"];
//为tableView提供数据
_dic=@{@"老师":_arr2,@"学生":_arr1}; }
//为UITableView划分分区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [_dic count] ;
}
//设置每块分区的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{ NSArray *arrKey=[_dic allKeys];
NSString *key=[arrKey objectAtIndex:section];
NSArray *arrObject=[_dic objectForKey:key];
return [arrObject count];
}
//为tableView的每个单元格提供数据
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellId=@"cellId";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellId];
if(!cell){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
NSArray *arrKey=[_dic allKeys];
NSString *key=[arrKey objectAtIndex:indexPath.section];
NSArray *arrObject=[_dic objectForKey:key];
cell.textLabel.text=[arrObject objectAtIndex:indexPath.row];
return cell;
}
//隔行换色
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([indexPath row] % == ) {
cell.backgroundColor = [UIColor blueColor];
} else {
cell.backgroundColor = [UIColor grayColor];
}
}
//为每块分区设置头高
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return ;
}
//为每块分区设置头标题
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSArray *arr=[_dic allKeys];
return [arr objectAtIndex:section];
}
三、UITableViewDataSource的一些属性与方法
@protocol UITableViewDataSource<NSObject> @required - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; // Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls) - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; @optional - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; // Default is 1 if not implemented - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section; // fixed font style. use custom view (UILabel) if you want something different
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section; // Editing // Individual rows can opt out of having the -editing property set for them. If not implemented, all rows are assumed to be editable.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath; // Moving/reordering // Allows the reorder accessory view to optionally be shown for a particular row. By default, the reorder control will be shown only if the datasource implements -tableView:moveRowAtIndexPath:toIndexPath:
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath; // Index - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView; // return list of section titles to display in section index view (e.g. "ABCD...Z#")
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index; // tell table which section corresponds to section title/index (e.g. "B",1)) // Data manipulation - insert and delete support // After a row has the minus or plus button invoked (based on the UITableViewCellEditingStyle for the cell), the dataSource must commit the change
// Not called for edit actions using UITableViewRowAction - the action's handler will be invoked instead
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath; // Data manipulation - reorder / moving support - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath; @end
四、UITableViewDelegate的一些属性与方法
@protocol UITableViewDelegate<NSObject, UIScrollViewDelegate> @optional // Display customization - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath NS_AVAILABLE_IOS(6_0);
- (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
- (void)tableView:(UITableView *)tableView didEndDisplayingFooterView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0); // Variable height support - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section; // Use the estimatedHeight methods to quickly calcuate guessed values which will allow for fast load times of the table.
// If these methods are implemented, the above -tableView:heightForXXX calls will be deferred until views are ready to be displayed, so more expensive logic can be placed there.
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(7_0);
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section NS_AVAILABLE_IOS(7_0);
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section NS_AVAILABLE_IOS(7_0); // Section header & footer information. Views are preferred over title should you decide to provide both - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section; // custom view for header. will be adjusted to default or specified header height
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section; // custom view for footer. will be adjusted to default or specified footer height // Accessories (disclosures). - (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath NS_DEPRECATED_IOS(2_0, 3_0);
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath; // Selection // -tableView:shouldHighlightRowAtIndexPath: is called when a touch comes down on a row.
// Returning NO to that message halts the selection process and does not cause the currently selected row to lose its selected look while the touch is down.
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);
- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);
- (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0); // Called before the user changes the selection. Return a new indexPath, or nil, to change the proposed selection.
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath;
- (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0);
// Called after the user changes the selection.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0); // Editing // Allows customization of the editingStyle for a particular cell located at 'indexPath'. If not implemented, all editable cells will have UITableViewCellEditingStyleDelete set for them when the table has editing property set to YES.
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0);
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(8_0); // supercedes -tableView:titleForDeleteConfirmationButtonForRowAtIndexPath: if return value is non-nil // Controls whether the background is indented while editing. If not implemented, the default is YES. This is unrelated to the indentation level below. This method only applies to grouped style table views.
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath; // The willBegin/didEnd methods are called whenever the 'editing' property is automatically changed by the table (allowing insert/delete/move). This is done by a swipe activating a single row
- (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)tableView:(UITableView*)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath; // Moving/reordering // Allows customization of the target row for a particular row as it is being moved/reordered
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath; // Indentation - (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath; // return 'depth' of row for hierarchies // Copy/Paste. All three methods must be implemented by the delegate. - (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(5_0);
- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender NS_AVAILABLE_IOS(5_0);
- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender NS_AVAILABLE_IOS(5_0); @end
UITableView的创建及其一些常用方法的更多相关文章
- 继承自UITableView的类自带tableView属性,不需要在创建该属性,因为父类UITableView已经创建.
继承自UITableView的类自带tableView属性,不需要在创建该属性,因为父类UITableView已经创建. https://www.evernote.com/shard/s227 ...
- NSString和NSMutableString的创建及其一些常用方法
NSString和NSMutableString都是对象类型,是NSObject的子类.NSString是不可变字符串,NSMutableString是可变字符串 一.NSString的创建 1.创建 ...
- XML创建与解析常用方法介绍
XML解析方式介绍 1.DOM4J(Document Object Model for Java) 虽然DOM4J代表了完全独立的开发结果,但最初,它是JDOM的一种智能分支.它合并了许多超 ...
- 轻量级应用开发之(08)UITableView
一 UITableView基本介绍 在众多移动应⽤用中,能看到各式各样的表格数据 . 在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView,UITableView继承自UISc ...
- 使用代码创建AutoLayout约束
使用代码创建AutoLayout约束 1.代码创建约束的步骤 2.代码创建约束的常用方法 3.代码创建约束的原则 4.禁用Autoresizing的原因 5. 设置相对状态栏的约束,使用self.to ...
- iOS学习之UITableView
一.UITableView的概念 UITabelView继承于UIScrollView,可以滚动. @interface UITableView : UIScrollView <NSCoding ...
- 关于直接创建视图UITableViewController显示(初学)
今天渣渣想直接创建一个UITableView视图作为根视图来用结果发现有警告,才明白TableView和view是不能直接作为根视图的,需要放在ViewController上.做个笔记详细了解下. 参 ...
- Executors常用的创建ExecutorService的几个方法说明
一.线程池的创建 我们可以通过ThreadPoolExecutor来创建一个线程池. new ThreadPoolExecutor(corePoolSize, maximumPoolSize, kee ...
- ##DAY10 UITableView基础
##DAY10 UITableView基础 UITableView继承于UIScrollView,可以滚动. UITableView的每⼀条数据对应的单元格叫做Cell,是UITableViewCel ...
随机推荐
- Python之路,day9-Python基础
回顾:抽象方法@staticmethod 不能访问类的任何属性@classmethod 类方法 只能访问公有属性@property 属性方法 , 把一个方法变成一个静态属性def sayhi() pa ...
- Razor 视图引擎 – ASP.NET MVC 4 系列
Razor 视图引擎是 ASP.NET MVC 3 开始扩展的内容,并且也是默认视图引擎. Razor 通过理解标记的结构来实现代码和标记之间尽可能顺畅的转换.下面的例子演 ...
- halcon运行版设置
- mysql之高可靠
6.mysql主-从备份 主-主备份,首先AB机要保持数据一致,即先手工备份A机数据,然后在B机上恢复,之后就很简单了,只需要在B机上创建一个同步账号,并在A机上输入B机的那些参数然后启动slave ...
- 经验分享:Linux 双网卡 不同网段 网络互通
环境如下: 现状:一台linux主机上有两个网卡eth0 和eth1 ,机器能访问192网的服务资源,但不能访问10网段的资源. 要求:linux能通过eth1访问10网段的资源 路由: 网卡: 操作 ...
- 崽崽帮www.zaizaibang.com精选3
[景山远洋美国交换生随笔]异国他乡的感触 [成都亲子活动]可能是成都最全最好的亲子活动了! 黄平-儿科 @体育活动 下雪天乐翻天之穿越封锁线 北京育翔小学的前世今生 武汉儿科类中医口碑榜 南宁周边农家 ...
- <<Vector Calculus>>笔记
现在流行用Exterior Caculus, 所以个人觉得Matthews这本书有点过时了. 想学Vector Calculus的话,推荐<Vector Calculus, Linear Alg ...
- haproxy +keepalived 原创
Haproxy+keepalived 原理: HAProxy介绍及其定位 HAProxy提供高可用性.负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费.快速并且可靠的一种解决方案.根 ...
- ios启动载入启动图片
版本判断: 1.首先你要知道这个键值对的key:id key = (id)kCFBundleVersionKey; 2.同过本地的NSBundle取得当前的版本号. 3.在沙盒中取得对应的版本号. ...
- js多文件上传
一.HTML 选择文件的时候可以选择多个文件,这个需要我们在input file 里面加入一个属性multiple="multiple" 这样就可以框选文件了 <!DOCTY ...