IOS 作业项目 TableView两个section中cell置顶功能实现
点击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置顶功能实现的更多相关文章
- 【代码笔记】iOS-一个tableView,两个section
一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController ...
- IOS 作业项目(4)步步完成 画图 程序(中续)
一,程序布局整理 前言://1,程序启动//2,程序流程框架//3,程序界面一致//4,程序界面功能, //这里只做页面的固定功能, //在首次创建界面时,我们会指定好固定事件触发前的固定方法 //至 ...
- IOS 作业项目(4)步步完成 画图 程序(中)
一,承接上文,继续本文 [UIButton buttonWithType:UIButtonTypeRoundedRect]; 如此声明的按钮才会有点击闪动的效果!如果直接frame方式声明就不会有. ...
- IOS 作业项目(2) 画图(保存,撤销,笔粗细设定功能)
先上效果图
- [IOS 开发]TableView如何刷新指定的cell 或section
//一个section刷新 NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:]; [tableview reloadSections:ind ...
- IOS 作业项目(4)步步完成 画图 程序(剧终)
// // CHViewController.m // SuperDrawingSample // // Created by JaikenLI on 13-11-21. // Copyrig ...
- IOS 作业项目(4)步步完成 画图 程序(上)
先上流程图
- [iOS微博项目 - 2.2] - 在app中获取授权
github: https://github.com/hellovoidworld/HVWWeibo A.发送授权请求 1.使用UIWebView加载请求页面 自定义一个继承UIViewContr ...
- IOS 作业项目(1) 关灯游戏 (百行代码搞定)
1,准备工作,既然要开关灯,就需要确定灯的灯的颜色状态 首先想到的是扩展UIColor
随机推荐
- hdu--(1247)Hat’s Words(trie树)
Hat’s Words Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- iOS控制器瘦身-面向超类编程
今天写这篇文章的目的,是提供一种思路,来帮助大家解决控制器非常臃肿的问题,对控制器瘦身. 滴滴 老司机要开车了 如果手边有项目,不妨打开工程看一下你的控制器代码有多少行,是不是非常多?再看一下tabl ...
- FireFox 一键清理缓存
https://addons.mozilla.org/zh-CN/firefox/addon/empty-cache-button/
- hadoop2.0 和1.0的区别
1. Hadoop 1.0中的资源管理方案Hadoop 1.0指的是版本为Apache Hadoop 0.20.x.1.x或者CDH3系列的Hadoop,内核主要由HDFS和MapReduce两个系统 ...
- Java中的String与常量池[转帖]
string是java中的字符串.String类是不可变的,对String类的任何改变,都是返回一个新的String类对象.下面介绍java中的String与常量池. 1. 首先String不属于8种 ...
- Hive的Transform功能
Hive的TRANSFORM关键字提供了在SQL中调用自写脚本的功能,适合实现Hive中没有的功能又不想写UDF的情况.例如,按日期统计每天出现的uid数,通常用如下的SQL SELECT date, ...
- js基础之动画(三)
一.链式运动 首先,要改进运动框架 function getStyle(obj,attr){ if(obj.currentStyle){ return obj.currentStyle[attr]; ...
- 微软WTL模板库完整版安装(VS2010+windows7X64位环境下)分享
一:简介 想必大家对于微软的MFC应该都比较熟悉.但是WTL可能很多人比较陌生吧.下面我就简单的说说这个库. 首先对这个库的做个简单的介绍吧. WTL 是 Windows Template Libra ...
- 常州培训 day6 解题报告
第一题: 题目大意: 给出一个N*N的矩阵,矩阵元素均为0或1.定义矩阵权值为sum(F[i][j]*F[j][i]); 给出K个操作: 询问矩阵的权值mod 2. 将矩阵的某一行元素取反(0变成1, ...
- ubnutu安装sougou 输入法
先安百度经验安装fcitx 1.首先下载sogoupinyin_2.0.0.0068_amd64.deb,点击安装后,会通过ubuntu软件中心安装,安装玩成后,任然是无法使用.然后: 2.然后执行下 ...