IOS学习笔记(11)TableView中移动cell和section
在TableView中移动cell和section
用tableView的moveSection:toSection:方法把一个Section移动到新位置。也可以使用moveRowAtIndexIndexPath:toIndexPath:方法把一个TableViewCell从当前位置移到一个新位置。
#import <UIKit/UIKit.h>
@interface ThirdViewController :UIViewController<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView *myTableView;
@property(nonatomic,strong)NSMutableArray *arrayOfSections;
@end
#import "ThirdViewController.h"
@interface ThirdViewController ()
@end
@implementation ThirdViewController
@synthesize myTableView,arrayOfSections;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [superinitWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self !=nil) {
arrayOfSections = [[NSMutableArrayalloc]init];
NSMutableArray *section1 = [selfnewSectionWithIndex:1withCellCount:3];
NSMutableArray *section2 = [selfnewSectionWithIndex:2withCellCount:3];
NSMutableArray *section3 = [selfnewSectionWithIndex:3withCellCount:3];
[arrayOfSectionsaddObject:section1];
[arrayOfSectionsaddObject:section2];
[arrayOfSectionsaddObject:section3];
}
return self;
}
- (void)viewDidLoad
{
[superviewDidLoad];
self.title =@"Third";
self.view.backgroundColor = [UIColorgrayColor];
myTableView = [[UITableViewalloc]initWithFrame:self.view.boundsstyle:UITableViewStyleGrouped];
myTableView.delegate =self;
myTableView.dataSource =self;
[self.viewaddSubview:myTableView];
//[self moveSection1ToSection3]; //移动section
//[self moveCellInSection1ToCell2InSection1]; //在一个section里面移动cell
[selfmoveCell2InSection1ToCell1InSection2];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSUInteger result = 0;
if([tableView isEqual:myTableView]){
if ([arrayOfSectionscount]>section) {
NSMutableArray *sectionArray = [arrayOfSectionsobjectAtIndex:section];
result = (NSInteger)[sectionArraycount];
}
}
return result;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
NSUInteger result = 0;
if([tableView isEqual:myTableView]){
result = (NSUInteger)[self.arrayOfSectionscount];
}
return result;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *result = nil;
if([tableView isEqual:myTableView]){
static NSString *CellIdentifier =@"CellIdentifier";
if(result == nil){
result = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier];
}
NSMutableArray *sectionArray = [arrayOfSectionsobjectAtIndex:indexPath.section];
result.textLabel.text = [sectionArrayobjectAtIndex:indexPath.row];
}
return result;
}
-(NSMutableArray *)newSectionWithIndex:(NSUInteger)paramIndex withCellCount:(NSUInteger)paramCellCount{
NSMutableArray *result = [[NSMutableArrayalloc]init];
NSUInteger counter = 0;
for (counter; counter<paramCellCount; counter++) {
[resultaddObject:[[NSStringalloc]initWithFormat:@"Section %lu Cell %lu",(unsignedlong)paramIndex,(unsignedlong)counter + 1]];
}
return result;
}
-(void)moveSection1ToSection3{
NSMutableArray *section1 = [arrayOfSectionsobjectAtIndex:0];
[arrayOfSectionsremoveObject:section1];
[arrayOfSectionsaddObject:section1];
[myTableViewmoveSection:2toSection:1];
}
-(void)moveCellInSection1ToCell2InSection1{
NSMutableArray *section1 = [arrayOfSectionsobjectAtIndex:0];
NSString *cell1InSection1 = [section1 objectAtIndex:0];
[section1removeObject:cell1InSection1];
[section1insertObject:cell1InSection1 atIndex:1];
NSIndexPath *sourceIndexPath = [NSIndexPathindexPathForRow:0inSection:0];
NSIndexPath *destinationIndexPath = [NSIndexPathindexPathForRow:1inSection:0];
[myTableViewmoveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];
}
-(void)moveCell2InSection1ToCell1InSection2{
NSMutableArray *section1 = [arrayOfSectionsobjectAtIndex:0];
NSMutableArray *section2 = [arrayOfSectionsobjectAtIndex:1];
NSString *cell2InSection1 = [section1 objectAtIndex:1];
[section1removeObject:cell2InSection1];
//NSString *cell1InSection2 = [section2 objectAtIndex:0];
//[section2 removeObject:cell1InSection2];
[section2insertObject:cell2InSection1 atIndex:0];
NSIndexPath *sourceIndexPath = [NSIndexPathindexPathForRow:1inSection:0];
NSIndexPath *destinationIndexPath = [NSIndexPathindexPathForRow:0inSection:1];
[myTableViewmoveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];
}
- (void)didReceiveMemoryWarning
{
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end