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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 牙套保持器坏了怎么办 牙齿磕掉了一小块怎么办 牙齿裂掉了一半怎么办 大牙缺了一小块怎么办 牙根碎了一小块怎么办 门牙碎了一小块怎么办 孩子门牙长歪了怎么办 儿童门牙长歪了怎么办 大门牙长歪了怎么办 下面牙齿长歪了怎么办 只有一颗牙齿歪怎么办 牙齿突然长歪了怎么办 有个牙齿长歪了怎么办 一颗大牙长歪了怎么办 一颗牙齿长偏了怎么办 一个门牙长歪了怎么办 儿童牙齿长歪了怎么办 孩子牙齿长歪了怎么办 宝宝门牙长歪了怎么办 一颗牙齿挤歪了怎么办 后槽牙掉了一块怎么办 最里面的牙掉了怎么办 成年了牙齿掉了怎么办 我的门牙豁牙子怎么办 小孩牙齿长得稀怎么办 后槽牙掉了一颗怎么办 前门牙掉了一颗怎么办 牙齿黄怎么办小苏打美白牙齿 1岁宝宝牙齿发黄怎么办 宝宝一岁牙齿黄怎么办 宝宝出的牙黄怎么办 我的牙齿很黄怎么办 小孩换的牙发黄怎么办 换牙后牙齿变黄怎么办 抽烟让牙齿变黄怎么办 抽烟把牙齿抽黄了怎么办 牙齿抽烟有黑色污渍怎么办 抽烟抽的牙黄了怎么办 宝宝换牙长歪了怎么办 30岁地包天怎么办 2岁宝宝地包天怎么办