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
随机推荐
- 学习manacher(最长公共回文串算法)
给定一个字符串求出其中最长个公共回文串. 举列子: abab -->回文串长度为2 以前的算法诸如: 扩展kmp求法过于麻烦,看到有一篇博文(http://leetcode.com/2011 ...
- Same Tree [LeetCode]
Problem Description: http://oj.leetcode.com/problems/same-tree/ class Solution { public: bool isSame ...
- 233. Number of Digit One *HARD* -- 从1到n的整数中数字1出现的次数
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...
- 【转】Session ID/session token 及和cookie区别
Session + Cookie 知识收集! cookie机制采用的是在客户端保持状态的方案.它是在用户端的会话状态的存贮机制,他需要用户打开客户端的cookie支持.cookie的作用就是为了解决 ...
- group_concat函数与find_in_set()函数相结合
一:group_concat函数详解 1.语法如下: group_concat([DISTINCT] 要连接的字段 [Order BY ASC/DESC 排序字段] [Separator '分隔符'] ...
- 读取DBF文件的部分代码
private void BtnOpenInitial_Click(object sender, EventArgs e) { OpenFileDialog file = new OpenFileDi ...
- JSP 客户端请求
当浏览器请求一个网页时,它会向网络服务器发送一系列不能被直接读取的信息,因为这些信息是作为HTTP信息头的一部分来传送的.您可以查阅HTTP协议来获得更多的信息. 下表列出了浏览器端信息头的一些重要内 ...
- DML数据操作语言练习
--创建表T_HQ_BM2 --create table t_hq_bm2 as select * from t_hq_bm; commit; --添加行内容 --insert into t_hq_b ...
- uboot启动 及命令分析(3)
u-boot命令 先贴一个重要结构,位于uboot/include/command.h,这个结构代表每个uboot命令 struct cmd_tbl_s { char *name; /* ...
- 从python中copy与deepcopy的区别看python引用
讨论copy与deepcopy的区别这个问题要先搞清楚python中的引用.python的内存管理. python中的一切事物皆为对象,并且规定参数的传递都是对象的引用.可能这样说听起来比较难懂,对比 ...