学英语。所以用英文来记录笔记。
 
Define the dataSource:
 
@implementation ViewController
{
    NSMutableArray *dataSourse;
    UITableView *myTableView;
}
 
Define the dataSource array:
 
    dataSourse = [NSMutableArray array];
    for (int i=0; i<50; ++i) {
        NSString *str = [NSString stringWithFormat:@"the %d row", i];
        [dataSourse addObject:str];
    }
 
 
DataSource Two-dimensional Array:
 
    dataSource = [NSMutableArray array];
    for (int s=0; s<5; ++s) {
        NSMutableArray *sectionArray=[NSMutableArray array];
        for (int r=0; r<10; ++r) {
            NSString *str=[NSString stringWithFormat:@"the %d group--the %d row", s, r];
            [sectionArray addObject:str];
        }
        [dataSource addObject:sectionArray];
    }
 
 
 
Create a UITABLEVIEW
 
    UITableView *myTableVie = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 320, 480) style:UITableViewStylePlain];
    
    myTableVie.delegate = self;
    myTableVie.dataSource = self;
    
    [self.view addSubview:myTableVie];
 
 
Implement delegate:
 
#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource , UITableViewDelegate]]>

@end

 
 
The UITableViewDataSource  have two function which must be implemented:
 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"xxxx"];
    
    NSString *str = dataSourse[indexPath.row];
    cell.textLabel.text=[NSString stringWithFormat:@"I am a cell %@", str];
    return cell;
}
 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dataSourse.count;
}
 
 
The Reusable Mechanism:
 
 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuseID = @"myCell";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:reuseID];
    if (cell == nil) {
        NSLog(@"alloc new cell");
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseID];
        cell.detailTextLabel.text = [NSString stringWithFormat:@"previous %d", indexPath.row];
    }
    NSString *str=dataSourse[indexPath.row];
    cell.textLabel.text=[NSString stringWithFormat:@"I am a cell %@", str];
    return cell;
}
 
 
Add the group (all code):
 
- (void)viewDidLoad
{
    
    [super viewDidLoad];
    
    dataSource = [NSMutableArray array];
    for (int s=0; s<5; ++s) {
        NSMutableArray *sectionArray=[NSMutableArray array];
        for (int r=0; r<10; ++r) {
            NSString *str=[NSString stringWithFormat:@"the %d group--the %d row", s, r];
            [sectionArray addObject:str];
        }
        [dataSource addObject:sectionArray];
    }
    
    UITableView *myTableVie = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 320, self.view.frame.size.height) style:UITableViewStylePlain];
    
    myTableVie.delegate = self;
    myTableVie.dataSource = self;
    
    
    UIView *headView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 200)];
    headView.backgroundColor=[UIColor redColor];
    myTableVie.tableHeaderView=headView;
    [self.view addSubview:myTableVie];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return [NSString stringWithFormat:@"The %d group", section];
}

//DIY group header , but if overwrite the function, the groupheader function is unvaliable..

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
 UIView *headView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
 headView.backgroundColor = [UIColor blackColor];
 
 UILabel *label=[[UILabel alloc] initWithFrame:headView.frame];
 label.text=[NSString stringWithFormat:@"The %d group", section];
 label.textColor=[UIColor redColor];
 label.textAlignment = NSTextAlignmentCenter;
 label.font=[UIFont boldSystemFontOfSize:15];
 [headView addSubview:label];
 return headView;
 }

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return dataSource.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *sectionArray=dataSource[section];
    return sectionArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuseID = @"myCell";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:reuseID];
    if (cell == nil) {
        NSLog(@"alloc new cell");
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseID];
        cell.detailTextLabel.text = [NSString stringWithFormat:@"previous %d", indexPath.row];
    }
    NSString *str=dataSource[indexPath.section][indexPath.row];
    cell.textLabel.text=str;
    return cell;
}

 
 
Delete the tableView in one row :
 
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"tableview ");
    //delete the data in a row
    NSMutableArray *sectionArray = dataSource[indexPath.section];
    [sectionArray removeObjectAtIndex:indexPath.row];
    //delete a row in tableview
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
 
 
Ps : The difference between the self and sender , 
for example : 
 
-(void)onLeftButtonClicked:(id)sender{
    NSLog(@"d1 :%@", sender);
    NSLog(@"d2 :%@", self);
    if (myTableView.isEditing) {
        myTableView.editing=NO;
    }else{
        myTableView.editing=YES;
    }
}
 
In the Button action , 
self is the object: <QFViewController: 0x8fab460> sender is the object: <UIBarButtonItem: 0x8fba3b0> 
sender is GUI widget self. 
self is the class self.
 
 
The IOS 7 own particular feature :
 
self.automaticallyAdjustsScrollViewInsets=NO;
 
It make the layout automatic adapt the padding.
 
 
//How many group
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return dataSource.count;
}
//Set group header height
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}
//Set group header text
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return [NSString stringWithFormat:@"The %d group",section];
}
 
//User-defined group-header, if overwrite this function, the function which set header text is invalid.
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
    //headView’s width must be the same with the return variable of function which named tableView:heightForHeaderInSection:
 
    headView.backgroundColor=[UIColor colorWithRed:(CGFloat)random()/RAND_MAX green:(CGFloat)random()/RAND_MAX blue:(CGFloat)random()/RAND_MAX alpha:1];
    UILabel *label=[[UILabel alloc]initWithFrame:headView.frame];
    label.text=[NSString stringWithFormat:@"The %d group ",section];
    label.font=[UIFont boldSystemFontOfSize:15];
    label.textColor=[UIColor redColor];
    label.textAlignment=NSTextAlignmentCenter;
    [headView addSubview:label];
    
    return headView;
}
 
//How many rows every group own
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *sectionArray=dataSource[section];
    return sectionArray.count;
}
 
 
//return this row cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *reuseID=@"myCell";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:reuseID];
    if (cell==nil) {
        NSLog(@"new cell");
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseID];
        cell.detailTextLabel.text=[NSString stringWithFormat:@"privious %d",indexPath.row];
    }
    NSString *str=dataSource[indexPath.section][indexPath.row];
    cell.textLabel.text=str;
    return cell;
}

 
//Edit tableview
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    /*
     UITableViewCellEditingStyleNone,
     UITableViewCellEditingStyleDelete,
     UITableViewCellEditingStyleInsert
     */
    if (editingStyle==UITableViewCellEditingStyleInsert) {
        //1、insert a data
        NSString *str=[NSString stringWithFormat:@“new the line %d",indexPath.row];
        NSMutableArray *sectionArray=dataSource[indexPath.section];
        [sectionArray insertObject:str atIndex:indexPath.row];
        //2、insert a row in tableview
        [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
    }else if (editingStyle==UITableViewCellEditingStyleDelete){
        //1、remove a row in datasource.
        NSMutableArray *sectionArray=dataSource[indexPath.section];
        [sectionArray removeObjectAtIndex:indexPath.row];
        //2、remove a row in tableview.
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}
//ontrol the edit state in Row
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section%2==0) {
        return UITableViewCellEditingStyleDelete;
    }
    return UITableViewCellEditingStyleInsert;
}
//Move the row-data in tableview
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
    NSMutableArray *sourceSectionArray=dataSource[sourceIndexPath.section];
    id sourceData=sourceSectionArray[sourceIndexPath.row];
    [sourceSectionArray removeObject:sourceData];
    //remove it in previous location
    NSMutableArray *destinationSectionArray=dataSource[destinationIndexPath.section];
    [destinationSectionArray insertObject:sourceData atIndex:destinationIndexPath.row];
    //Insert the row in new location
    [tableView reloadRowsAtIndexPaths:@[destinationIndexPath] withRowAnimation:UITableViewRowAnimationNone];
    //refresh a row, but have some error in the application.
    [tableView reloadData];
}
 
The exercise picture :
 
 
 
 
 

IOS UI 第九篇: UITABLEVIEW的更多相关文章

  1. iOS开发——UI_swift篇&UITableView实现单元格展开与隐藏

    UITableView实现单元格展开与隐藏  关于UITableView的展开的收缩在前面的文章我已经结束,就是使用代理,通知,block传值的时候实现的,当时是使用一个Bool值来实现,最后使用着三 ...

  2. iOS开发——UI_swift篇&UITableView实现索引功能

    UITableView实现索引功能     关于UItableView的索引在平时项目中所见不多,最多的就是跟联系人有关的界面,虽然如此,但是作为一个swift开发的程序必须知道的一个技术点,所以今天 ...

  3. IOS UI 第二篇:基本UI

    1.UI书写:   一个小练习图片如下:     代码如下:     @implementation AppDelegate{    UIControl *control;    UILabel *l ...

  4. iOS开发——UI_swift篇&UItableView实现移动单元格

    UItableView实现移动单元格   1,下面的样例是给表格UITableView添加单元格移动功能: (1)给表格添加长按功能,长按后表格进入编辑状态 (2)在编辑状态下,可以看到单元格后面出现 ...

  5. IOS UI 第一篇:基本UI

    1. UI 书写 最基本创建一个label 标签 写一个first rate :      UILabel *label = [[UILabel alloc] initWithFrame:CGRect ...

  6. IOS设计模式第九篇之备忘录模式

    版权声明:原创作品,谢绝转载!否则将追究法律责任. 备忘录模式捕获和具体化对象的内部状态.换句话说,它可以节省你的东西后来,这种外部状态可以恢复在不违反封装; 也就是说,私人数据是私有的. 怎么用备忘 ...

  7. iOS开发UI篇—UITableview控件简单介绍

    iOS开发UI篇—UITableview控件简单介绍 一.基本介绍 在众多移动应⽤用中,能看到各式各样的表格数据 . 在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView,UIT ...

  8. iOS开发UI篇—UITableview控件基本使用

    iOS开发UI篇—UITableview控件基本使用 一.一个简单的英雄展示程序 NJHero.h文件代码(字典转模型) #import <Foundation/Foundation.h> ...

  9. iOS开发UI篇—UITableview控件使用小结

    iOS开发UI篇—UITableview控件使用小结 一.UITableview的使用步骤 UITableview的使用就只有简单的三个步骤: 1.告诉一共有多少组数据 方法:- (NSInteger ...

随机推荐

  1. 如何构建高性能web网站:分布式缓存

    一.数据库前端缓冲区 要清除数据库前缓冲区,首先必须明确什么是文件系统的内核缓冲区(Buffer Area):它位于内核的物理内存地址空间,除了使用O_DIRECT比其他标签中打开文件,所有的磁盘的读 ...

  2. 杭州电acm理工大舞台版

    我要参加全国软件设计大赛C/C++学生语言组,前一个假设<C训练和演习,并总结手>没看完,请阅读上述并根据所作的训练,然后做下面的练习. 门户:http://blog.csdn.net/l ...

  3. hdu Rescue (bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1242 简单优先队列搜索,自己好久不敲,,,,,手残啊,,,,orz 代码: #include < ...

  4. 浅谈数据结构之KMP(串中的模式匹配算法)

    KMP算法是一种模式匹配算法的改进版,其通过减少匹配的次数以及使主串不回朔来减少字符串匹配的次数,从而较少算法的相应代价,但是,事件万物是普遍归中的,KMP算法的有效性也是有一定的局限的,我将在本文的 ...

  5. HDU 4946 Area of Mushroom 凸包 第八次多校

    题目链接:hdu 4946 题意:一大神有N个学生,各个都是小神,大神有个二次元空间,每一个小神都有一个初始坐标,如今大神把这些空间分给徒弟们,规则是假设这个地方有一个人比谁都先到这,那么这个地方就是 ...

  6. 自定义View视图

    自定义View视图文件查找逻辑 之前MVC5和之前的版本中,我们要想对View文件的路径进行控制的话,则必须要对IViewEngine接口的FindPartialView或FindView方法进行重写 ...

  7. document.domain跨域

    原文:[转载]document.domain跨域 document.domain 用来得到当前网页的域名. 比如在地址栏里输入: javascript:alert(document.domain); ...

  8. kubernetes多节点部署的决心

    注:以下操作均基于centos7系统. 安装ansible ansilbe能够通过yum或者pip安装,因为kubernetes-ansible用到了密码.故而还须要安装sshpass: pip in ...

  9. C#操作Xml:通过XmlDocument读写Xml文档

    什么是Xml? Xml是扩展标记语言的简写,是一种开发的文本格式.关于它的更多情况可以通过w3组织了解http://www.w3.org/TR/1998/REC-xml-19980210.如果你不知道 ...

  10. ASP.NET MVC上传文件----uploadify的使用

    课程设计需要实现上传文件模块,本来ASP.NET是有内置的控件,但是ASP.NET MVC没有,所以就有两种方法:自定义和采用第三方插件.由于时间的关系,故采用第三方插件:uploadify. upl ...