一、基本知识点

UITableView表格视图,是一个可以滚动的界面(理解为垂直滚动的UIScrollView),可展示多行数据,没有行数的限制,只能有一列。
使用UITableView:
1、展示信息,显示大量的列表数据
2、导航
通常选择单元格导航至另一界面。主界面是tableview,通常是导航界面(nav)的根视图,用户轻击表格中的内容,从而进入改选项的详细界面(完整信息)
 
一、创建UITableView有两种样式
1、 UITableView *tableview = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];//设置样式
2、 UITableView *tableview1 = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];//设置样式
3、设置相关属性
tableview.rowHeight = 80;//行高
    tableview.separatorStyle = UITableViewCellSeparatorStyleSingleLine;//分割线样式
    tableview.separatorColor = [UIColor greenColor];//分割线颜色
UIImageView *imageview = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1.jpg"]];
    tableview.backgroundView = imageview;//设置背景图
//[tableview addSubview:imageview];//也可以这样添加
UIView *header = [[UIView alloc]initWithFrame:CGRectMake(10, 10, 0, 20)];
    tableview.tableHeaderView = header;//tableView顶部视图,其frame只有高有效
tableview.dataSource = self;//设置数据源
    tableview.delegate = self;
    tableview.tableFooterView = [[UIView alloc]init];//去掉线
    [self.view addSubview:tableview];
4、代理方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;//设置table组数,默认是1,因此当只有一个分组时可省略
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//    if (section == 0) {
//        return 2;
//    }else if (section == 1) {
//        return 6;
//    }
    return 5;//设置每行的行数
}
//设置每行对应的cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{//indexPath包含了第几组:indexPath.section 和第几行:indexPath.row
   
    static NSString *identifier = @"cell";//重用机制标识
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];//根据重用标识,找到重用池着对应的cell
    if (cell == nil) {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];//创建一个cell,设置其样式和标识
 
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];//此处更改UITableViewCellStyleSubtitle样式,可显示不同的风格
NSArray *arr = @[@"王",@"杨",@"韩"];
        cell.detailTextLabel.text = arr[indexPath.row];//添加字符串
 
        cell.textLabel.text = [NSString stringWithFormat:@"第%zi行,第%zi组",indexPath.row,indexPath.section];//设置cell的文本信息
        cell.imageView.image = [UIImage imageNamed:@"2.jpg"];
    }
    return cell;
}
 
二、UITableView代理方法及应用
#import "ViewController.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    UITableView *_tableview = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    //_tableview.rowHeight = 80;//设置行高
    //_tableview.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [self.view addSubview:_tableview];
 
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 8;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 4;
    }else if (section == 2) {
        return 2;
    }
    return 40;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //static NSString *ident = @"cell";//会导致重用机制有bug
   NSString *ident = [NSString stringWithFormat:@"%zi,%zi",indexPath.section,indexPath.row];//解决重用机制bug可用这种方法//给每行设置不同的标识
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ident];
//    static int num;
//    NSLog(@"%d",num++);
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ident];
        //cell.textLabel.text = nil;//先清理,再使用
    }
    cell.textLabel.text = [NSString stringWithFormat:@"%zi组,%zi行",indexPath.section,indexPath.row];
    cell.detailTextLabel.text = @"详细信息";
   
    if (indexPath.section == 0 && indexPath.row == 0) {
        cell.imageView.image = [UIImage imageNamed:@"1.jpg"];
    }
    if (indexPath.section == 2 && indexPath.row == 0) {
        cell.imageView.image = [UIImage imageNamed:@"2.jpg"];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;//提示用户可点,点击之后跳至下级界面

//cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;//提示用户可点,点击按钮(叹号)会有相关提示弹出,点击cell之后跳至下级界面

//cell.accessoryType = UITableViewCellAccessoryCheckmark;//对勾
    //cell.accessoryType = UITableViewCellAccessoryDetailButton;//提示用户可点,点击按钮(叹号)会有相关提示弹出
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    //每组顶部视图的高度
    return 20;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    //自定义每组头视图
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 40 )];
    view.backgroundColor = [UIColor redColor];
    return view;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    //自定义尾部视图
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 80)];
    view.backgroundColor = [UIColor greenColor];
    return view;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 30;
}

//-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    //设置每行的高度
//    if (indexPath.row = 0) {
//        return 80;
//    }
//    return 40;

//}
 

UITableView表格视图、UITableView代理方法及应用的更多相关文章

  1. iOS:UITableView表格视图控件

    UITableView:表格视图控件,继承滚动视图控件UIScrollView,(类似于UIPickerView选择器,它主要通过设置数据源代理和行为代理实现协议来设置单元格)    对表格的操作主要 ...

  2. iOS:分组的表格视图UITableView,可以折叠和展开

    虽然表格视图可以分组,但是如果分组后,每一行的内容太多,往后翻看起来比较的麻烦.为了解决这个麻烦,可以将分组的行折叠和展开.折叠时,行内容就会隐藏起来:展开时,行内容就会显示出来. 折叠时: 展开后: ...

  3. iOS:带主标题、副标题、图像类型的表格视图UITableView

    制作一个通讯录,包括姓名.电话.头像,将表格视图类型设置为UITableViewCellStyleSubtitle 效果图: //创建一个联系人的类,初始化数据 在视图控制器中实现表格内容的显示 #i ...

  4. [Xcode 实际操作]五、使用表格-(7)UITableView单元格间隔背景色

    目录:[Swift]Xcode实际操作 本文将演示如何给表格设置间隔的背景颜色. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit //首先 ...

  5. [Xcode 实际操作]五、使用表格-(6)UITableView滑动到指定单元格

    目录:[Swift]Xcode实际操作 本文将演示如何使表格滑动到指定的索引路径. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit //首 ...

  6. UITableView表视图以及重建机制

    表视图UITableView   表视图UITableView,是IOS中最重要的视图,随处可见 表视图通常用来管理一组具有相同数据结构的数据 UITableView继承自UIScrollView,所 ...

  7. UITableView的全部属性、方法以及代理方法执行顺序,看过之后肯定有收获---董鑫

    UITableView-------表视图--继承UIScrollView并遵守NSCoding协议 属性 frame-------------设置控件的位置和大小 backgroundColor-- ...

  8. UITableView的常用属性和代理方法

    以下是近期总结的关于tableView的一些属性和代理方法,以及一些常见的问题,现汇总如下,今后还会持续更新,请继续关注:   tableView 的头部和尾部视图属性: UISwitch *foot ...

  9. IOS UITableView的代理方法详解

    一.UITableViewDataSourc(数据源代理) 1.必须实现的回调方法 返回每个分区的行数 - (NSInteger)tableView:(UITableView *)tableView ...

随机推荐

  1. 浅谈js的键值对key和value

    > 昨晚无意中看到类似下面结构的一段代码的取值问题,引起我的兴趣,花了点时间写了个demo给大家分享一下... var obj = [ {"2011":{"name ...

  2. string和vector

    一.String对象 1.string s;      s.size(); //返回的是s中字符的个数,也是s的长度: //string对象最后没有加空字符 //size()返回的是string::s ...

  3. java中Thread的 interrupt异常处理

    http://blog.csdn.net/srzhz/article/details/6804756

  4. javascript-test1

    var AAA = function(name, age) { this.name = name ; this.age = age; } AAA.prototype.getName = functio ...

  5. Python list方法总结

    1. 向列表的尾部添加一个新的元素 append(...) L.append(object) -- append object to end 1 2 3 4 >>> a = ['sa ...

  6. Linux与Windows API对比

    对象 操作 Linux API Windows API 线程 创建 pthread_create() CreateThread() 退出 pthread_exit() ThreadExit() 等待 ...

  7. ca des key crt scr

    openssl genrsa -des3 -out domain.key 1024 openssl req -new -key domain.key -out domain.csr openssl r ...

  8. TCP/IP、Http的区别

    TPC/IP协议是传输层协议,主要解决数据如何在网络中传输,而HTTP是应用层协议,主要解决如何包装数据.关于TCP/IP和HTTP协议的关系,网络有一段比较容易理解的介绍:“我们在传输数据时,可以只 ...

  9. 使用soureTree删除分支

    使用心得 1:使用pull拉取时,自己本地的更改不会覆盖掉 2:删除分枝时,直接选中,可以直接删除远程的分枝

  10. 同一web系统,不同端口的跨域问题

    部署web系统的时候,发现了跨域问题,子系统是用Iframe嵌入到系统里面的,导致父窗口获取子系统的webService和图片时发现跨域问题,如下图所示:问题1:父窗口获取子系统的对象跨域 原因:用这 ...