UITableView 的简单介绍
2016-01-09 00:27
471 查看
UItableView的使用主要是其协议方法,其协议有两个<UITableViewDelegate,UITableViewDataSource>。
设置 UITableView的委托(两个委托属性);
tableView.dataSource =self;
tableView.delegate =self;
interface DemoViewController () <UITableViewDataSource, UITableViewDelegate>
@end
遵循协议;
<UITableViewDataSource, UITableViewDelegate>
@implementation DemoViewController
- (void)viewDidLoad {
[super viewDidLoad];
UITableView *tableView = [[UITableView alloc] initWithFrame:reck style:UITableViewStylePlain];
tableView.dataSource = self;
tableView.delegate = self;
[self.view addSubview:tableView];
}
#pragma mark - Table view协议方法
协议里的两个必须实现的方法:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSecti
1,初始化方法:
UITableView *tableView =[UITableView alloc]initWithFrame:(CGRect);
// style参数指定样式,有两种:普通样式
Plain(默认值)、分组样式
Grouped
UITableView *tableView =[UITableView alloc]initWithFrame:(CGRect) style:(UITableViewStyle)
//改变换行线颜色。
tableView.separatorColor = [UIColorpurpleColor];
//判断tableView是否能被编辑。
BOOL isEditing = tableView.isEditing;
//设置tableView的能否被编辑
[tableView setEditing:isEditinganimated:YES];
//⭐️重新加载cell的内容。
[tableViewreloadRowsAtIndexPaths:[NSArrayarrayWithObjects:indexPath,nil]withRowAnimation:UITableViewRowAnimationNone];
//同一个UIView的赋值给tv.tableHeaderView和tv.tableFooterView是,只会显示tv.tableHeaderView。
tv.tableHeaderView =UIView *
iv;
tv.tableFooterView =UIView *
iv;
//分隔线。
tv.separatorStyle =UITableViewCellSeparatorStyleSingleLine;
tv.separatorColor = [UIColorgreenColor];
//tableViewCellde附件种类
tableViewCell.accessoryType
//右侧添加一个索引表。
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;
//设置 UITableView中,有多少个
Section (组)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView ;
//设置 UITableView中,每个
Section中有多少个 Cell (确定矿场一个区域能停多少矿车)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
/* ========== Cell设置
Cell的样式 ========== */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath ;
// 注册cell,与下面的复用机制联用。
[tableView registerClass:[MyTableViewCell
class]forCellReuseIdentifier:@"MyTableViewCell"];
//(Cell的复用机制),先去可复用
Cell队列里面找一下,看看是否有可以复用的
Cell//
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:@"Cell"forIndexPath:indexPath];
/* ========== Cell点击事件
========== */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
*)indexPath;
//设置 Cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath
*)indexPath ;
//设置section的 header、footer的标题
(NSString *)
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
;
- (NSString *)tableView:(UITableView
*)tableView titleForFooterInSection:(NSInteger)section;
//自定义section的
header、footer (UIView *)
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
- (UIView *)tableView:(UITableView
*)tableView viewForHeaderInSection:(NSInteger)section
//设置 header、footer高度
(CGFloat)
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
- (CGFloat)tableView:(UITableView
*)tableView heightForFooterInSection:(NSInteger)section
//设置某行 Cell是否可以被编辑
(是否可以向左滑动,拖出编辑按钮)
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath
*)indexPath ;
//设置所编辑cell"删除"按钮的
Title
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath
*)indexPath ;
//当编辑按钮(默认叫"删除")被按下去的时候,执行的方法(类似
Button 点击事件)
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath;
//将要选择某一个cell时。
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath
*)indexPath{
if (indexPath.row ==0)
{
return
nil;
//设置不让选中每组的第一个。
}
return indexPath;
}
返回当前所选的cell。
NSIndexPath *ip = [NSIndexPath indexPathForRow:row inSection:section];
[tableView selectRowAtIndexPath:ip animated:YES scrollPosition:UITableViewScrollPositionNone];
//一个section刷新
NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:2];
[tableview reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
//一个cell刷新
NSIndexPath *indexPath=[NSIndexPath indexPathForRow:3 inSection:0];
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
设置 UITableView的委托(两个委托属性);
tableView.dataSource =self;
tableView.delegate =self;
interface DemoViewController () <UITableViewDataSource, UITableViewDelegate>
@end
遵循协议;
<UITableViewDataSource, UITableViewDelegate>
@implementation DemoViewController
- (void)viewDidLoad {
[super viewDidLoad];
UITableView *tableView = [[UITableView alloc] initWithFrame:reck style:UITableViewStylePlain];
tableView.dataSource = self;
tableView.delegate = self;
[self.view addSubview:tableView];
}
#pragma mark - Table view协议方法
协议里的两个必须实现的方法:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSecti
1,初始化方法:
UITableView *tableView =[UITableView alloc]initWithFrame:(CGRect);
// style参数指定样式,有两种:普通样式
Plain(默认值)、分组样式
Grouped
UITableView *tableView =[UITableView alloc]initWithFrame:(CGRect) style:(UITableViewStyle)
//改变换行线颜色。
tableView.separatorColor = [UIColorpurpleColor];
//判断tableView是否能被编辑。
BOOL isEditing = tableView.isEditing;
//设置tableView的能否被编辑
[tableView setEditing:isEditinganimated:YES];
//⭐️重新加载cell的内容。
[tableViewreloadRowsAtIndexPaths:[NSArrayarrayWithObjects:indexPath,nil]withRowAnimation:UITableViewRowAnimationNone];
//同一个UIView的赋值给tv.tableHeaderView和tv.tableFooterView是,只会显示tv.tableHeaderView。
tv.tableHeaderView =UIView *
iv;
tv.tableFooterView =UIView *
iv;
//分隔线。
tv.separatorStyle =UITableViewCellSeparatorStyleSingleLine;
tv.separatorColor = [UIColorgreenColor];
//tableViewCellde附件种类
tableViewCell.accessoryType
//右侧添加一个索引表。
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;
//设置 UITableView中,有多少个
Section (组)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView ;
//设置 UITableView中,每个
Section中有多少个 Cell (确定矿场一个区域能停多少矿车)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
/* ========== Cell设置
Cell的样式 ========== */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath ;
// 注册cell,与下面的复用机制联用。
[tableView registerClass:[MyTableViewCell
class]forCellReuseIdentifier:@"MyTableViewCell"];
//(Cell的复用机制),先去可复用
Cell队列里面找一下,看看是否有可以复用的
Cell//
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:@"Cell"forIndexPath:indexPath];
/* ========== Cell点击事件
========== */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
*)indexPath;
//设置 Cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath
*)indexPath ;
//设置section的 header、footer的标题
(NSString *)
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
;
- (NSString *)tableView:(UITableView
*)tableView titleForFooterInSection:(NSInteger)section;
//自定义section的
header、footer (UIView *)
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
- (UIView *)tableView:(UITableView
*)tableView viewForHeaderInSection:(NSInteger)section
//设置 header、footer高度
(CGFloat)
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
- (CGFloat)tableView:(UITableView
*)tableView heightForFooterInSection:(NSInteger)section
//设置某行 Cell是否可以被编辑
(是否可以向左滑动,拖出编辑按钮)
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath
*)indexPath ;
//设置所编辑cell"删除"按钮的
Title
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath
*)indexPath ;
//当编辑按钮(默认叫"删除")被按下去的时候,执行的方法(类似
Button 点击事件)
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath;
//将要选择某一个cell时。
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath
*)indexPath{
if (indexPath.row ==0)
{
return
nil;
//设置不让选中每组的第一个。
}
return indexPath;
}
返回当前所选的cell。
NSIndexPath *ip = [NSIndexPath indexPathForRow:row inSection:section];
[tableView selectRowAtIndexPath:ip animated:YES scrollPosition:UITableViewScrollPositionNone];
//一个section刷新
NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:2];
[tableview reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
//一个cell刷新
NSIndexPath *indexPath=[NSIndexPath indexPathForRow:3 inSection:0];
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 讲解iOS开发中基本的定位功能实现
- js判断客户端是iOS还是Android等移动终端的方法
- IOS开发环境windows化攻略
- 浅析iOS应用开发中线程间的通信与线程安全问题
- 检测iOS设备是否越狱的方法
- .net平台推送ios消息的实现方法
- 探讨Android与iOS,我们将何去何从?
- Android、iOS和Windows Phone中的推送技术详解
- IOS 改变键盘颜色代码
- 举例详解iOS开发过程中的沙盒机制与文件
- Android和IOS的浏览器中检测是否安装某个客户端的方法
- 分享一个iOS下实现基本绘画板功能的简单方法
- javascript实现阻止iOS APP中的链接打开Safari浏览器