Objective - C UITableView学习笔记
2015-12-24 20:13
351 查看
UITableView具有两套协议,分别是
<UITableViewDataSource,UITableViewDelegate>
// 设置协议代理人
tableView.dataSource = self;
tableView.delegate = self;
// 方法
一 : UITableViewDataSource:有两个常用的方法:
// 指定一个分区有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
// 通过这个方法让tableview显示内容
// 这个方法只要有cell出现,就会触发
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// tableViewCell通过重用避免了多余的创建,一般来讲一个tableView显示的cell数是有限的,所以为了提高效率,避免重复的创建,利用重用解决问题.重用也是常见的tableView的面试问题
// 步骤一:先指定一个cell的重用标识
// 一般来讲,一个tableView对应一个重用标识,重用标识作用就是告诉系统,哪个cell对应哪个tableView
static NSString *reuse = @"reuse";
// 系统先会根据重用标识在重用池里找,有没有闲置的cell,如果有直接拿来用,如果没有,再创建
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
// 如果cell没找到,对应的cell是0x0
if (!cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
NSLog(@"创建了");
}
cell.textLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row];
// cell提供了三种视图,两个label,一个imageView
// tableView与数组关联
cell.textLabel.text = self.arr[indexPath.row];
cell.detailTextLabel.text = [NSString stringWithFormat:@"%ld",indexPath.section];
cell.imageView.image = [UIImage imageNamed:@"tu7.jpg"];
NSLog(@"%ld",indexPath.row);
return cell;
}
其他方法
#pragma mark 设置tableView里有多少个分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 3;
}
#pragma mark 设置分区的标题
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
NSString *str = [[NSString alloc] init];
str = @"0";
return str;
}
- (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView*)tableView{
return @[@"A",@"B",@"C"];
}
二: UITableViewDelegate:有一个常用的方法:
// 主要功能:实现点击
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
<UITableViewDataSource,UITableViewDelegate>
// 设置协议代理人
tableView.dataSource = self;
tableView.delegate = self;
// 方法
一 : UITableViewDataSource:有两个常用的方法:
// 指定一个分区有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
// 通过这个方法让tableview显示内容
// 这个方法只要有cell出现,就会触发
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// tableViewCell通过重用避免了多余的创建,一般来讲一个tableView显示的cell数是有限的,所以为了提高效率,避免重复的创建,利用重用解决问题.重用也是常见的tableView的面试问题
// 步骤一:先指定一个cell的重用标识
// 一般来讲,一个tableView对应一个重用标识,重用标识作用就是告诉系统,哪个cell对应哪个tableView
static NSString *reuse = @"reuse";
// 系统先会根据重用标识在重用池里找,有没有闲置的cell,如果有直接拿来用,如果没有,再创建
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
// 如果cell没找到,对应的cell是0x0
if (!cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
NSLog(@"创建了");
}
cell.textLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row];
// cell提供了三种视图,两个label,一个imageView
// tableView与数组关联
cell.textLabel.text = self.arr[indexPath.row];
cell.detailTextLabel.text = [NSString stringWithFormat:@"%ld",indexPath.section];
cell.imageView.image = [UIImage imageNamed:@"tu7.jpg"];
NSLog(@"%ld",indexPath.row);
return cell;
}
其他方法
#pragma mark 设置tableView里有多少个分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 3;
}
#pragma mark 设置分区的标题
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
NSString *str = [[NSString alloc] init];
str = @"0";
return str;
}
- (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView*)tableView{
return @[@"A",@"B",@"C"];
}
二: UITableViewDelegate:有一个常用的方法:
// 主要功能:实现点击
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
相关文章推荐
- 【转】Android自动化测试(UiAutomator)——UiObject
- java线程同步:使用Object的wait,notify,notifyAll做线程调度
- Objective--C 导航视图控制器
- Objective-C基础1-类目(Category)和延展(Extension)的区别
- Objective-C 集合之NSArray的常用方法
- Objective-C NSObject 的实现分析(2014-10-23更新)
- Objective-C Runtime 总结:消息机制 篇
- Object.ReferenceEquals
- 遍历系统的所有ObjectType和TypeIndex
- iOS开发系列--Objective-C之KVC、KVO
- Objective-c语言_内存管理2(代码)
- Objective-C 和 Core Foundation 对象相互转换的内存管理总结
- Ubuntu snmpwalk 执行报错 “Unknown Object Identifier”
- ActiveX控件之ActiveXObject is not defined
- JSONObject、JSONArray
- Objective-C:Foundation框架-常用类-NSObject
- Objective-C:Foundation框架-常用类-NSDate
- Objective-C:Foundation框架-常用类-NSNull
- Objective-C:Foundation框架-常用类-NSValue
- Objective-C:Foundation框架-常用类-NSNumber