数据源的加载
我们加载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];
}
}
效果如下