点击cell会置顶,其他的下移

第一,引入代理

@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>

第二,实现

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.itemList = [[NSMutableArray alloc] init];
    //存放置顶数据的数组
    NSMutableArray *topArray = [[NSMutableArray alloc] init];
    //存放不置顶数据的数组
    NSMutableArray *normalArray = [[NSMutableArray alloc] init];
    [self.itemList addObject:topArray];
    [self.itemList addObject:normalArray];
    
    [normalArray addObject:@"1"];
    [normalArray addObject:@"2"];
    [normalArray addObject:@"3"];
    [normalArray addObject:@"4"];
    [normalArray addObject:@"5"];
    [normalArray addObject:@"6"];
    [normalArray addObject:@"7"];
    [normalArray addObject:@"8"];
    [normalArray addObject:@"9"];
    [normalArray addObject:@"10"];
    [normalArray addObject:@"11"];
    [normalArray addObject:@"12"];
    
    
    CGRect frame = {0,20,320,460};
    UITableView *tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
}

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

//设置一共有2个分区,分别做为置顶区与正常区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //根据分区号,获取对应的存放容器
    NSArray *itemArray = [self.itemList objectAtIndex:section];
    return itemArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //根据分区号,获取对应的存放容器
    NSArray *itemArray = [self.itemList objectAtIndex:indexPath.section];
    //指定置顶区的唯一标识符..
    static NSString *topIdentifier = @"topIdentifier";
    //指定正常区的唯一标识符
    static NSString *normalIdentifier = @"normalIdentifier";
    
    //声明返回的变量名
    UITableViewCell *cell = nil;
    
    switch (indexPath.section) {
        case 0://置顶区域
        {
            cell = [tableView dequeueReusableCellWithIdentifier:topIdentifier];
            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:topIdentifier];
            }
            cell.textLabel.text = itemArray[indexPath.row];
            break;
        }
        case 1://正常区域
        {
            cell = [tableView dequeueReusableCellWithIdentifier:normalIdentifier];
            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:normalIdentifier];
            }
            cell.backgroundColor = [UIColor whiteColor];
            cell.textLabel.text = itemArray[indexPath.row];
        }
    }
    NSLog(@"22222222");
    //返回单元格
    return cell;
}

//通过点击单元格完成置顶操作...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //如果点击的是置顶分区则直接返回..
    if (indexPath.section == 0) {
        return;
    }
    //否则,则点击的是正常分区,开始整理数据...
    NSMutableArray *topArray = [self.itemList objectAtIndex:0];
    NSMutableArray *normalArray = [self.itemList objectAtIndex:1];
    
    //正常区域的点击数据
    NSString *item  = [normalArray objectAtIndex:indexPath.row];
    
    [tableView beginUpdates];//数据数据改变数据,tableview做相应的刷新
    //当置顶区域的置顶条数大于等于3时,将置顶区域的最后一个元素移除并且添加到正常区域的第一条
    if (topArray.count >= 3)
    {
        //置顶区域的最后一条数据
        NSString *lastTopItem  = [topArray lastObject];
        [normalArray insertObject:lastTopItem atIndex:0];
        [topArray removeObject:lastTopItem];
        //第二行
        NSIndexPath *lastTopIndexPath = [NSIndexPath indexPathForRow:2 inSection:0];
        NSIndexPath *headNormalIndexPath = [NSIndexPath indexPathForRow:0 inSection:1];
        [tableView moveRowAtIndexPath:lastTopIndexPath toIndexPath:headNormalIndexPath];
    }
    //当置顶区域的置顶条数小于3时,直接添加进置顶数组,并且从正常区域移除该对象...
    [topArray insertObject:item atIndex:0];
    [normalArray removeObject:item];
    
    NSIndexPath *topIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [tableView moveRowAtIndexPath:indexPath toIndexPath:topIndexPath];
    
    [tableView endUpdates];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString *title = nil;
    switch (section) {
        case 0:
            title = @"置顶的啊";
            break;
        case 1:
            title = @"普通的啊";
        default:
            break;
    }
    return title;
}

IOS 作业项目 TableView两个section中cell置顶功能实现的更多相关文章

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

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

  2. IOS 作业项目(4)步步完成 画图 程序(中续)

    一,程序布局整理 前言://1,程序启动//2,程序流程框架//3,程序界面一致//4,程序界面功能, //这里只做页面的固定功能, //在首次创建界面时,我们会指定好固定事件触发前的固定方法 //至 ...

  3. IOS 作业项目(4)步步完成 画图 程序(中)

    一,承接上文,继续本文  [UIButton buttonWithType:UIButtonTypeRoundedRect]; 如此声明的按钮才会有点击闪动的效果!如果直接frame方式声明就不会有. ...

  4. IOS 作业项目(2) 画图(保存,撤销,笔粗细设定功能)

    先上效果图

  5. [IOS 开发]TableView如何刷新指定的cell 或section

    //一个section刷新 NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:]; [tableview reloadSections:ind ...

  6. IOS 作业项目(4)步步完成 画图 程序(剧终)

    // //  CHViewController.m //  SuperDrawingSample // //  Created by JaikenLI on 13-11-21. //  Copyrig ...

  7. IOS 作业项目(4)步步完成 画图 程序(上)

    先上流程图

  8. [iOS微博项目 - 2.2] - 在app中获取授权

    github: https://github.com/hellovoidworld/HVWWeibo   A.发送授权请求 1.使用UIWebView加载请求页面 自定义一个继承UIViewContr ...

  9. IOS 作业项目(1) 关灯游戏 (百行代码搞定)

    1,准备工作,既然要开关灯,就需要确定灯的灯的颜色状态 首先想到的是扩展UIColor

随机推荐

  1. 获取txt文件指定行内容

    #!/usr/bin/python num=0; ni=open("C:\Python34\ceshi.txt") for line in ni: num=num+1;  #表示行 ...

  2. backbonejs中的集合篇(一)

    一:集合概念 集合是多个模型,如果把模型model理解为表结构中的行,那么集合collection就是一张表,由多个行组成.我们经常需要用集合来组织和管理多个模型. 二:创建集合 1:扩展Backbo ...

  3. uva 242

    242 - Stamps and Envelope Size Time limit: 3.000 seconds  Stamps and Envelope Size  Philatelists hav ...

  4. POC测试——原型验证,降低风险,IT系统销售工作之一

    POC测试,即Proof of Concept,是业界流行的针对客户具体应用的验证性测试,根据用户对采用系统提出的性能要求和扩展需求的指标,在选用服务器上进行真实数据的运行,对承载用户数据量和运行时间 ...

  5. mysql启动报错

    查看报错日志: 131023 15:02:59 [ERROR] Can't start server: Bind on TCP/IP port: No such file or directory13 ...

  6. 使用WebView视图显示网页-----迷你浏览器

    Android提供了WebView组件,表面上来看,这个组件与普通ImageView差不多,但实际上,这个组件的功能要强大得多,WebView组件本身就是一个浏览器实现,它的内核基于开源WebKit引 ...

  7. 浏览器渲染原理--reflow

    Web页面运行在各种各样的浏览器当中,浏览器载入.渲染页面的速度直接影响着用户体验简单地说,页面渲染就是浏览器将html代码根据CSS定义的规则显示在浏览器窗口中的这个过程.先来大致了解一下浏览器都是 ...

  8. Redis系列-存储篇sorted set主要操作函数小结

    redis支持有序集合,即sorted set.sorted set在set的基础上,增加了排序属性,是set的升级版.这里简要谈谈sorted set的常用函数: 1)insert a)  zadd ...

  9. C#操作Access数据库(创建&修改结构)

    本文转自:http://www.cnblogs.com/liyugang/archive/2012/11/17/2775393.html 想要在程序中控制Access,不是数据,而是Access数据库 ...

  10. 建议入门-用ArcMap进行空间查询与空间连接

    1.打开arcmap并导入数据(如本图导入美国地图(usa.mxd)): 2.空间查询操作,在地图上的某片区域点击右键,得到下图,点击identify,此时我在阿拉斯加上面点击的 地图会闪现一下被查询 ...