IOS 构造和使用TableView(基于storyboard) (七)移动cell和section
来源:互联网 发布:搜狗大数据 编辑:程序博客网 时间:2024/05/08 15:11
数据源的加载
我们加载3个Section,每个section有3个Cell
从视图头文件开始
#import <UIKit/UIKit.h>@interface myTableViewController : UITableViewController<UITableViewDelegate,UITableViewDataSource>@property (strong, nonatomic) IBOutlet UITableView *myTable;@property (nonatomic, strong) NSMutableArray *arrayOfSections;@end定义一个数据填充方法
- (NSMutableArray *) newSectionWithIndex:(NSUInteger)paramIndex withCellCount:(NSUInteger)paramCellCount{ NSMutableArray *result = [[NSMutableArray alloc] init]; NSUInteger counter = 0; for (counter = 0; counter < paramCellCount; counter++) { [result addObject:[[NSString alloc]initWithFormat:@"Section %lu Cell %lu",(unsigned long)paramIndex,(unsigned long)counter+1]]; } return result;}初始化
- (void)viewDidLoad{ [super viewDidLoad]; arrayOfSections = [[NSMutableArray alloc]init]; NSMutableArray *section0 = [self newSectionWithIndex:1 withCellCount:3]; NSMutableArray *section1 = [self newSectionWithIndex:2 withCellCount:3]; NSMutableArray *section2 = [self newSectionWithIndex:3 withCellCount:3]; [arrayOfSections addObject:section0]; [arrayOfSections addObject:section1]; [arrayOfSections addObject:section2];}定义Section数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ NSInteger result = 0; if ([tableView isEqual:self.myTable]) { result = (NSInteger)[self.arrayOfSections count]; } return result;}定义row数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ NSInteger result = 0; if ([tableView isEqual:self.myTable]) { if ([self.arrayOfSections count]>section) { NSMutableArray *sectionArray = [self.arrayOfSections objectAtIndex:section]; result = (NSInteger)[sectionArray count]; } } return result;}填充数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *result = nil; if ([tableView isEqual:self.myTable]) { static NSString *TableViewCellIdentifier = @"MyCells"; result = [tableView dequeueReusableCellWithIdentifier:TableViewCellIdentifier]; if (result == nil) { result = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableViewCellIdentifier]; } NSMutableArray *sectionArray = [self.arrayOfSections objectAtIndex:indexPath.section]; result.textLabel.text = [sectionArray objectAtIndex:indexPath.row]; } return result;}数据填充完毕
移动Section主要是通过调用moveSection:toSection:方法
- (void) moveSection:(NSInteger)S1 ToSection:(NSInteger)S2{ NSMutableArray *section = [self.arrayOfSections objectAtIndex:0]; [self.arrayOfSections removeObject:section]; [self.arrayOfSections insertObject:section atIndex:S2]; [self.myTable moveSection:S1 toSection:S2];}在选中某个Cell时执行移动
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{ if ([tableView isEqual:self.myTable]) { [self moveSection:1 ToSection:0]; }}效果如下
移动Cell
- (void)moveCell:(NSInteger)C1 InSection:(NSInteger)S1 ToCell:(NSInteger)C2 InSection:(NSInteger)S2{ NSMutableArray *section1 = [self.arrayOfSections objectAtIndex:S1]; NSMutableArray *section2 = [self.arrayOfSections objectAtIndex:S2]; NSString *cell2InSection1 = [section1 objectAtIndex:C2]; [section1 removeObject:cell2InSection1]; [section2 insertObject:cell2InSection1 atIndex:C1]; NSIndexPath *sourceIndexPath = [NSIndexPath indexPathForRow:C1 inSection:S1]; NSIndexPath *destinationIndexPath = [NSIndexPath indexPathForRow:C2 inSection:S2]; [self.myTable moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];}点击Cell时触发
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{ if ([tableView isEqual:self.myTable]) { [self moveCell:0 InSection:0 ToCell:0 InSection:1]; }}效果如下
0 0
- IOS 构造和使用TableView(基于storyboard) (七)移动cell和section
- IOS 构造和使用TableView(基于storyboard) (二)接收和处理Table View事件
- IOS 构造和使用TableView(基于storyboard) (五)构建页眉和页脚
- IOS 构造和使用TableView(基于storyboard) (三)附件的使用
- IOS 构造和使用TableView(基于storyboard) (一)基本的数据显示
- IOS 构造和使用TableView(基于storyboard) (四)启用Table Viewcell的滑动删除
- IOS 构造和使用TableView(基于storyboard) (六)显示上下文菜单
- IOS学习笔记(11)TableView中移动cell和section
- IOS开发(28)UITableView之移动Section和Cell
- iOS tableView section 和cell 一同滚动的方法
- IOS使用视图控制器和视图(七) NavigationController 的使用(基于storyboard)
- iOS刷新tableView某个cell或section
- iOS tableView背景图和cell背景图
- 使用storyboard tableView的cell自适应高度
- 刷新section和cell
- storyboard tableview section
- 我使用的Tableview 和cell
- tableview cell in section
- Delphi 7下使用Log4Delphi 0.8日志组件
- Boost智能指针——weak_ptr
- ios--OpenCV--图像像素颜色反转
- 开源日志库<log4cplus+VS2008使用>整理
- 用NDK调用第三方动态库--个人总结
- IOS 构造和使用TableView(基于storyboard) (七)移动cell和section
- C#--struct结构
- 1、direct X 的简介
- ArrayList和LinkedList
- CloudStack 4.3 发布,支持微软Hyper-V
- c++ new 用法 总结
- 高阻态分析
- jquery serialize介绍
- Ext window的大小与屏幕匹配