//tableView的创建
    //1.初始化 initWithFrame:style:(plian,gronp)
    //2.设置属性(行高, 分割线, 表头, 表尾)
    //3.添加到父视图
    //4.释放
    
    //tableView显示数据
    //1.设置dataSource
    //2.遵循协议<UITableViewDateSource>
    //3.实现两个必须要实现的方法(a. 返回某个分区行数, b.返回cell)
    
    //tableViewCell 创建
    //1.重用标识符(static初始化一次,节约内存)
    //2.通过标识符去复用池找cell
    //3.判断cell是否找到, 如果没有找到, 创建cell(initWithStyle:reuseIdentifier)要释放autorelease
    //4.判断括号外,对cell进行复制
    //5.return cell
//
  UITableViewDelegate, UITableViewDataSource一部分代理方法的使用
#import "TableViewController.h"

@interface TableViewController ()<UITableViewDelegate, UITableViewDataSource>//tableView想要实现显示内容必须遵守的协议

@end

@implementation TableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"Cell";
    self.view.backgroundColor = [UIColor yellowColor];
    
    //tableView的创建
    //1.初始化
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    //2.设置属性(行高, 分割线, 表头, 表尾)
    tableView.rowHeight = 60;
    //UITableViewCellSeparatorStyleSingleLine(默认)
    tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    tableView.separatorColor = [UIColor redColor];
    tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    tableView.backgroundColor = [UIColor grayColor];
    UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 375, 30)];
    headerLabel.text = @"IOS应用排行榜";
    headerLabel.textAlignment = NSTextAlignmentCenter;
    tableView.tableHeaderView = headerLabel;//表头,需要先创建一个Label,然后把创建好的Label赋给表头表尾
    [headerLabel release];
    
    //通过表尾可以清除多余分割线
    //tableView.tableFooterView = [[[UIView alloc] init] autorelease];
    //设置是否允许多选
    tableView.allowsMultipleSelection = YES;
    
    
    //tableView,想要显示数据,使用dataSource模式, dataSource实质还是代理模式
    //步骤a.设置dataSource
    //    b.遵循协议<UITableViewDateSource>
    //    c.实现两个必须要实现的方法(a.返回某个分区行数, b.返回cell)
    
    tableView.dataSource = self;
    tableView.delegate = self;
    //3. 添加父视图
    [self.view addSubview:tableView];
    //4. 释放
    [tableView release];
    
    // Do any additional setup after loading the view.
} #pragma mark -- UITableViewDataSource
//UITableViewDataSource中方法
//@required 必须要实现的两个方法1.和 2. //1.设置行数(如果有分区的话,可以通过if判断每个分区,然后给每个分区赋不同的行数)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;//此处返回设置10行,返回10;
}
//2.UITableViewCell, 单元格类, 继承于UIView, 用于在UITableView上显示内容
//注:会执行多次, 每走一次, 创建一个cell; 第一次只创建出一个屏幕能够显示的cell,如果滚动tableView, 会再走这个方法,再次创建cell -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //cell重用机制(复用机制),用于降低内存消耗
    //方法内部的实现
    //a.定义重用标示,static 静态变量,初始化一次, 降低内存的消耗
    static NSString *identifier = @"CELL";
    //b.去重用队列中根据标识符取可重用的 cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    //c. 判断是否获取到可重用的 cell( 最后要空间释放 )
    if (!cell) {//!cell 相当于 cell == nil
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
        cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;//辅助标示类型
        
    }
    //显示内容
    
    cell.imageView.image = [UIImage imageNamed:@"李四"];
    cell.textLabel.text = @"李四";
    cell.detailTextLabel.text = @"我是李四";
    return cell; }
/*
//辅助表示类型 用accessoryType 赋值         UITableViewCellAccessoryNone,                   // don't show any accessory view
        UITableViewCellAccessoryDisclosureIndicator,    // regular chevron. doesn't track
        UITableViewCellAccessoryDetailDisclosureButton, // info button w/ chevron. tracks
        UITableViewCellAccessoryCheckmark,              // checkmark. doesn't track
        UITableViewCellAccessoryDetailButton NS_ENUM_AVAILABLE_IOS(7_0) // info button. tracks
 
*/ //@optional  UITableViewDataSource 中不必实现但是经常用到的方法
//1.设置分组个数section
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;//返回多少就是多少个分区
}
//2.设置区头(section)
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSArray *array = @[@"A", @"B", @"C"];
    return array[section];
}
//3.设置区尾
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    NSArray *array = @[@"AAA", @"BBB", @"CCC"];
    return array[section];
}
//4.右侧添加一个索引表 -(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    NSArray *aa = @[@"A", @"B", @"C"];
    return aa;
}
#pragma mark - UITableViewDelegate //UITableViewDelegate中得一些常用方法
//1.将显示单元格
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%s", __FUNCTION__); }
//2.自定义区头视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *hearderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 375, 44)];
    hearderView.backgroundColor = [UIColor yellowColor];
//    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
//    label.text = @"zhao";
//    [hearderView addSubview:label];
//    [label release];
    return [hearderView autorelease];
} //3.自定义区尾视图
//- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
//{
//
//} //4.设置每行间隔的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{     return 80;
}
//5.选择哪一section的哪一行
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"section: %ld, row: %ld", indexPath.section, indexPath.row);
} //6.设置选中的行所执行的动作
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //NSLog(@"%s",__FUNCTION__);
    //NSUInteger row = [indexPath row];
    return indexPath; } //设置让UITableView行缩进
-(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%s",__FUNCTION__);
    NSUInteger row = [indexPath row];
    return row;
} @end
 
 
 

tableView的使用(一)的更多相关文章

  1. iOS有关横向TableView的东西

    之前看到Apple store里面有横向的tableview,当然也有可能是collectionview啦. 尤其是项目中只有一条那么需要横向滑动的东西,就没有必要使用庞大的collectionvie ...

  2. tableView显示第一个cell有偏移问题

    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 0 ...

  3. [tableView reloadData] 和 runloop

    需要[tableView reloadData]后需要立即获取tableview的cell.高度,或者需要滚动tableview,那么,直接在reloadData后执行代码是会有问题的. 断点调试感觉 ...

  4. 【代码笔记】iOS-一个tableView,两个section

    一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController ...

  5. 【Swift】Alamofile网络请求数据更新TableView的坑

    写这篇BLOG前,有些话不得不提一下,就仅当发发恼骚吧... 今天下午为了一个Alamofire取得数据而更新TableView的问题,查了一下午的百度(360也是见鬼的一样),竟然没有一个简单明了的 ...

  6. TableView 滑动收起键盘

    self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag; 拖拽tableView就会收起键盘

  7. 关于TableView上有一段留白的解决方法

    当cell的类型是plaint类型时 直接设置self.automaticallyAdjustsScrollViewInsets=NO; 还有要注意检查你自己设置的frame是否正确     当cel ...

  8. iOS监听tableView组头切换事件

    - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSIntege ...

  9. 【原】iOS学习之tableView的常见BUG

    1.TableView头视图不随视图移动,头视图出现错位 错误原因:tableView的 UITableViewStyle 没有明确的声明 解决方法:在tableView声明的时候明确为 UITabl ...

  10. 两种让tableview返回顶部的方法

    1. [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:_currentRow inSection:0] animat ...

随机推荐

  1. 点滴积累【JS】---JS小功能(JS实现多物体缓冲运动)

    效果: 思路: 利用setInterval计时器进行运动,offsetWidth实现宽度的变动,在用onmouseover将终点和所选中的DIV放入参数再进行缓冲运动. 代码: <head ru ...

  2. The Way to Go读书笔记_第4章_基本结构和基本数据类型

    “_”标识符 _ 本身就是一个特殊的标识符,被称为空白标识符.它可以像其他标识符那样用于变量的声明或赋值(任何类型都可以赋值给它),但任何赋给这个标识符的值都将被抛弃,因此这些值不能在后续的代码中使用 ...

  3. python学习之多行字符串

    多行字符串的写法 ("..." "..." "...") 例子 >>> err = ("a" ... ...

  4. Oracle之函数concat、lpad

    一.引言 程序测试需要生成大量的测试数据,且测试数据有主键,主键自增,于是决定用存储过程来实现,经过半天的查资料终于完成了,记录之,学习之 二.存储过程 格式: CREATE PROCEDURE re ...

  5. lseek,fcntl,ioctl函数

    函数说明:每一个已打开的文件都有一个读写位置, 当打开文件时通常其读写位置是指向文件开头, 若是以附加的方式打开文件(如O_APPEND), 则读写位置会指向文件尾. 当read()或write()时 ...

  6. WebService 页面重定向错误

    “/”应用程序中的服务器错误. 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败. xxx.xxx.xxx.xxx:xx 说明: 执行当前 Web 请求期间,出现未经处理的异常.请 ...

  7. nginx+keepalived构建高可用服务

    1.整体环境规划 虚拟IP:10.0.4.248 主Nginx:10.0.4.249 备用Nginx:10.0.4.250 2.keepalived安装 #cd /usr/local/src #wge ...

  8. libxl库的介绍,对Excel操作封装得很好的一个库,兼容2007版和多字节字符(最后有破解版下载)

    前段时间忙着毕业论文,终于有时间写博客了. 早些时候老大给我的一个任务需要对excel进行读表操作,研究了一下c++对excel的操作. 对Excel的操作基本有com,ODBC,AD等,其中ODBC ...

  9. Easyui data方法扩展finder

    finder: function(jq, conditions){ if(!$(jq).data("OriginalData")){ $(jq).data("Origin ...

  10. 一个共通的viewModel搞定所有的分页查询一览及数据导出(easyui + knockoutjs + mvc4.0)

    前言 大家看标题就明白了我想写什么了,在做企业信息化系统中可能大家写的最多的一种页面就是查询页面了.其实每个查询页面,除了条件不太一样,数据不太一样,其它的其实都差不多.所以我就想提取一些共通的东西出 ...