1 前言
今天我們來學習一下如何移動UITableView控件中的Section和Cell
2 代碼實例
ZYViewController.h
[plain] #import <UIKit/UIKit.h>
@interface ZYViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *myTableView;
@property(nonatomic,strong)NSMutableArray *arrayOfSections;//數據源
@end
#import <UIKit/UIKit.h>
@interface ZYViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *myTableView;
@property(nonatomic,strong)NSMutableArray *arrayOfSections;//數據源
@end
ZYViewController.m
[plain] @synthesize myTableView;
@synthesize arrayOfSections;
//初始化數據源每個Cell的內容
-(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];
// Do any additional setup after loading the view, typically from a nib.
//初始化數據源
arrayOfSections = [[NSMutableArray alloc] init];
NSMutableArray *section1 = [self newSectionWithIndex:1 withCellCount:3];
NSMutableArray *section2 = [self newSectionWithIndex:2 withCellCount:3];
NSMutableArray *section3 = [self newSectionWithIndex:3 withCellCount:3];
[arrayOfSections addObject:section1];
[arrayOfSections addObject:section2];
[arrayOfSections addObject:section3];
myTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
self.myTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
//添加代理
myTableView.delegate = self;
myTableView.dataSource = self;
//添加右按鈕
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"MoveSection" style:UIBarButtonItemStylePlain target:self action:@selector(movewSection1ToSection3)];
//添加左按鈕
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"MoveCell" style:UIBarButtonItemStylePlain target:self action:@selector(moveCell2InSectionToCellInSection2)];
[self.view addSubview:myTableView];
}
//Section行數
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
NSInteger result = 0;
if ([tableView isEqual:myTableView]) {
result = (NSInteger)[self.arrayOfSections count];
}
return result;
}
//每個Section中的Cell行數
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSInteger result = 0;
if ([tableView isEqual:myTableView]) {
if ([arrayOfSections count]>section) {
NSMutableArray *sectionArray = [self.arrayOfSections objectAtIndex:section];
result = (NSInteger)[sectionArray count];
}
}
return result;
}
//設置每行Cell的內容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *result = nil;
if ([tableView isEqual:myTableView]) {
static NSString *CellIdentifier = @"CellIdentifier";
result = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSMutableArray *sectionArray = [self.arrayOfSections objectAtIndex:indexPath.section];
result.textLabel.text = [sectionArray objectAtIndex:indexPath.row];
return result;
}
//將第一個索引位置的Section移動放到最後一個位置
-(void)movewSection1ToSection3{
NSMutableArray *section1 = [self.arrayOfSections objectAtIndex:0];
[self.arrayOfSections removeObject:section1];
[self.arrayOfSections addObject:section1];
[myTableView moveSection:0 toSection:2];
}
//將第一個Section中的第一個Cell和第二個互換
-(void)moveCell1InSectionToCell2InSection1{
NSMutableArray *section1 = [self.arrayOfSections objectAtIndex:0];
NSString *cell1InSection1 = [section1 objectAtIndex:0];
[section1 removeObject:cell1InSection1];
[section1 insertObject:cell1InSection1 atIndex:1];
NSIndexPath *sourceIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
NSIndexPath *destinationIdexPath = [NSIndexPath indexPathForRow:1 inSection:0];
[self.myTableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIdexPath];
}
//將第一個Section的第二個Cell移動到第二個Section的第一個Cell之前的位置
-(void)moveCell2InSectionToCellInSection2{
NSMutableArray *section1 = [arrayOfSections objectAtIndex:0];
NSMutableArray *section2 = [arrayOfSections objectAtIndex:1];
NSString *cell2InSection1 = [section1 objectAtIndex:1];
[section1 removeObject:cell2InSection1];
[section2 insertObject:cell2InSection1 atIndex:0];
NSIndexPath *sourceIndexPath = [NSIndexPath indexPathForRow:1 inSection:0];
NSIndexPath *destinationIndexPath = [NSIndexPath indexPathForRow:0 inSection:1];
[myTableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];
}
@synthesize myTableView;
@synthesize arrayOfSections;
//初始化數據源每個Cell的內容
-(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];
// Do any additional setup after loading the view, typically from a nib.
//初始化數據源
arrayOfSections = [[NSMutableArray alloc] init];
NSMutableArray *section1 = [self newSectionWithIndex:1 withCellCount:3];
NSMutableArray *section2 = [self newSectionWithIndex:2 withCellCount:3];
NSMutableArray *section3 = [self newSectionWithIndex:3 withCellCount:3];
[arrayOfSections addObject:section1];
[arrayOfSections addObject:section2];
[arrayOfSections addObject:section3];
myTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
self.myTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
//添加代理
myTableView.delegate = self;
myTableView.dataSource = self;
//添加右按鈕
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"MoveSection" style:UIBarButtonItemStylePlain target:self action:@selector(movewSection1ToSection3)];
//添加左按鈕
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"MoveCell" style:UIBarButtonItemStylePlain target:self action:@selector(moveCell2InSectionToCellInSection2)];
[self.view addSubview:myTableView];
}
//Section行數
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
NSInteger result = 0;
if ([tableView isEqual:myTableView]) {
result = (NSInteger)[self.arrayOfSections count];
}
return result;
}
//每個Section中的Cell行數
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSInteger result = 0;
if ([tableView isEqual:myTableView]) {
if ([arrayOfSections count]>section) {
NSMutableArray *sectionArray = [self.arrayOfSections objectAtIndex:section];
result = (NSInteger)[sectionArray count];
}
}
return result;
}
//設置每行Cell的內容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *result = nil;
if ([tableView isEqual:myTableView]) {
static NSString *CellIdentifier = @"CellIdentifier";
result = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSMutableArray *sectionArray = [self.arrayOfSections objectAtIndex:indexPath.section];
result.textLabel.text = [sectionArray objectAtIndex:indexPath.row];
return result;
}
//將第一個索引位置的Section移動放到最後一個位置
-(void)movewSection1ToSection3{
NSMutableArray *section1 = [self.arrayOfSections objectAtIndex:0];
[self.arrayOfSections removeObject:section1];
[self.arrayOfSections addObject:section1];
[myTableView moveSection:0 toSection:2];
}
//將第一個Section中的第一個Cell和第二個互換
-(void)moveCell1InSectionToCell2InSection1{
NSMutableArray *section1 = [self.arrayOfSections objectAtIndex:0];
NSString *cell1InSection1 = [section1 objectAtIndex:0];
[section1 removeObject:cell1InSection1];
[section1 insertObject:cell1InSection1 atIndex:1];
NSIndexPath *sourceIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
NSIndexPath *destinationIdexPath = [NSIndexPath indexPathForRow:1 inSection:0];
[self.myTableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIdexPath];
}
//將第一個Section的第二個Cell移動到第二個Section的第一個Cell之前的位置
-(void)moveCell2InSectionToCellInSection2{
NSMutableArray *section1 = [arrayOfSections objectAtIndex:0];
NSMutableArray *section2 = [arrayOfSections objectAtIndex:1];
NSString *cell2InSection1 = [section1 objectAtIndex:1];
[section1 removeObject:cell2InSection1];
[section2 insertObject:cell2InSection1 atIndex:0];
NSIndexPath *sourceIndexPath = [NSIndexPath indexPathForRow:1 inSection:0];
NSIndexPath *destinationIndexPath = [NSIndexPath indexPathForRow:0 inSection:1];
[myTableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];
}
運行結果:
點擊右側按鈕後效果:
點擊左側按鈕後效果: