IOS学习笔记(11)TableView中移动cell和section
2013-01-25 11:15
381 查看
在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 = [super
initWithNibName:nibNameOrNil
bundle:nibBundleOrNil];
if (self !=
nil) {
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];
}
return
self;
}
- (void)viewDidLoad
{
[super
viewDidLoad];
self.title =
@"Third";
self.view.backgroundColor = [UIColor
grayColor];
myTableView = [[UITableView
alloc]initWithFrame:self.view.bounds
style:UITableViewStyleGrouped];
myTableView.delegate =
self;
myTableView.dataSource =
self;
[self.view
addSubview:myTableView];
//[self moveSection1ToSection3]; //移动section
//[self moveCellInSection1ToCell2InSection1]; //在一个section里面移动cell
[self
moveCell2InSection1ToCell1InSection2];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSUInteger result =
0;
if([tableView isEqual:myTableView]){
if ([arrayOfSections
count]>section) {
NSMutableArray *sectionArray = [arrayOfSections
objectAtIndex:section];
result = (NSInteger)[sectionArray
count];
}
}
return result;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
NSUInteger result =
0;
if([tableView isEqual:myTableView]){
result = (NSUInteger)[self.arrayOfSections
count];
}
return result;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath{
UITableViewCell *result =
nil;
if([tableView isEqual:myTableView]){
static NSString *CellIdentifier =
@"CellIdentifier";
if(result == nil){
result = [[UITableViewCell
alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
NSMutableArray *sectionArray = [arrayOfSections
objectAtIndex:indexPath.section];
result.textLabel.text = [sectionArray
objectAtIndex:indexPath.row];
}
return result;
}
-(NSMutableArray *)newSectionWithIndex:(NSUInteger)paramIndex withCellCount:(NSUInteger)paramCellCount{
NSMutableArray *result = [[NSMutableArray
alloc]init];
NSUInteger counter =
0;
for (counter; counter<paramCellCount; counter++) {
[result
addObject:[[NSString
alloc]initWithFormat:@"Section %lu Cell %lu",(unsigned
long)paramIndex,(unsigned
long)counter + 1]];
}
return result;
}
-(void)moveSection1ToSection3{
NSMutableArray *section1 = [arrayOfSections
objectAtIndex:0];
[arrayOfSections
removeObject:section1];
[arrayOfSections
addObject:section1];
[myTableView
moveSection:2
toSection:1];
}
-(void)moveCellInSection1ToCell2InSection1{
NSMutableArray *section1 = [arrayOfSections
objectAtIndex:0];
NSString *cell1InSection1 = [section1
objectAtIndex:0];
[section1
removeObject:cell1InSection1];
[section1
insertObject:cell1InSection1
atIndex:1];
NSIndexPath *sourceIndexPath = [NSIndexPath
indexPathForRow:0
inSection:0];
NSIndexPath *destinationIndexPath = [NSIndexPath
indexPathForRow:1
inSection:0];
[myTableView
moveRowAtIndexPath:sourceIndexPath
toIndexPath:destinationIndexPath];
}
-(void)moveCell2InSection1ToCell1InSection2{
NSMutableArray *section1 = [arrayOfSections
objectAtIndex:0];
NSMutableArray *section2 = [arrayOfSections
objectAtIndex:1];
NSString *cell2InSection1 = [section1
objectAtIndex:1];
[section1
removeObject:cell2InSection1];
//NSString *cell1InSection2 = [section2 objectAtIndex:0];
//[section2 removeObject:cell1InSection2];
[section2
insertObject:cell2InSection1
atIndex:0];
NSIndexPath *sourceIndexPath = [NSIndexPath
indexPathForRow:1
inSection:0];
NSIndexPath *destinationIndexPath = [NSIndexPath
indexPathForRow:0
inSection:1];
[myTableView
moveRowAtIndexPath:sourceIndexPath
toIndexPath:destinationIndexPath];
}
- (void)didReceiveMemoryWarning
{
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
用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 = [super
initWithNibName:nibNameOrNil
bundle:nibBundleOrNil];
if (self !=
nil) {
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];
}
return
self;
}
- (void)viewDidLoad
{
[super
viewDidLoad];
self.title =
@"Third";
self.view.backgroundColor = [UIColor
grayColor];
myTableView = [[UITableView
alloc]initWithFrame:self.view.bounds
style:UITableViewStyleGrouped];
myTableView.delegate =
self;
myTableView.dataSource =
self;
[self.view
addSubview:myTableView];
//[self moveSection1ToSection3]; //移动section
//[self moveCellInSection1ToCell2InSection1]; //在一个section里面移动cell
[self
moveCell2InSection1ToCell1InSection2];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSUInteger result =
0;
if([tableView isEqual:myTableView]){
if ([arrayOfSections
count]>section) {
NSMutableArray *sectionArray = [arrayOfSections
objectAtIndex:section];
result = (NSInteger)[sectionArray
count];
}
}
return result;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
NSUInteger result =
0;
if([tableView isEqual:myTableView]){
result = (NSUInteger)[self.arrayOfSections
count];
}
return result;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath{
UITableViewCell *result =
nil;
if([tableView isEqual:myTableView]){
static NSString *CellIdentifier =
@"CellIdentifier";
if(result == nil){
result = [[UITableViewCell
alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
NSMutableArray *sectionArray = [arrayOfSections
objectAtIndex:indexPath.section];
result.textLabel.text = [sectionArray
objectAtIndex:indexPath.row];
}
return result;
}
-(NSMutableArray *)newSectionWithIndex:(NSUInteger)paramIndex withCellCount:(NSUInteger)paramCellCount{
NSMutableArray *result = [[NSMutableArray
alloc]init];
NSUInteger counter =
0;
for (counter; counter<paramCellCount; counter++) {
[result
addObject:[[NSString
alloc]initWithFormat:@"Section %lu Cell %lu",(unsigned
long)paramIndex,(unsigned
long)counter + 1]];
}
return result;
}
-(void)moveSection1ToSection3{
NSMutableArray *section1 = [arrayOfSections
objectAtIndex:0];
[arrayOfSections
removeObject:section1];
[arrayOfSections
addObject:section1];
[myTableView
moveSection:2
toSection:1];
}
-(void)moveCellInSection1ToCell2InSection1{
NSMutableArray *section1 = [arrayOfSections
objectAtIndex:0];
NSString *cell1InSection1 = [section1
objectAtIndex:0];
[section1
removeObject:cell1InSection1];
[section1
insertObject:cell1InSection1
atIndex:1];
NSIndexPath *sourceIndexPath = [NSIndexPath
indexPathForRow:0
inSection:0];
NSIndexPath *destinationIndexPath = [NSIndexPath
indexPathForRow:1
inSection:0];
[myTableView
moveRowAtIndexPath:sourceIndexPath
toIndexPath:destinationIndexPath];
}
-(void)moveCell2InSection1ToCell1InSection2{
NSMutableArray *section1 = [arrayOfSections
objectAtIndex:0];
NSMutableArray *section2 = [arrayOfSections
objectAtIndex:1];
NSString *cell2InSection1 = [section1
objectAtIndex:1];
[section1
removeObject:cell2InSection1];
//NSString *cell1InSection2 = [section2 objectAtIndex:0];
//[section2 removeObject:cell1InSection2];
[section2
insertObject:cell2InSection1
atIndex:0];
NSIndexPath *sourceIndexPath = [NSIndexPath
indexPathForRow:1
inSection:0];
NSIndexPath *destinationIndexPath = [NSIndexPath
indexPathForRow:0
inSection:1];
[myTableView
moveRowAtIndexPath:sourceIndexPath
toIndexPath:destinationIndexPath];
}
- (void)didReceiveMemoryWarning
{
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
相关文章推荐
- tableView section随cell移动 ,不在顶到屏幕顶部
- UITableView (4): 在TableView中移动cell和Section 从TableView中删除cell和section 添加系统默认刷新控件
- [11]UITableView编辑 删除 添加 移动 单例 及其去掉TableView没内容的Cell
- IOS 构造和使用TableView(基于storyboard) (七)移动cell和section
- SWTableView(CCTableView)移动列表项到指定索引对应的cell
- SWTableView(CCTableView)移动列表项到指定索引对应的cell
- ios 11 tableView穿透导航栏 同时让section悬浮在导航栏下面
- tableview 自定义cell 不显示 self 与 sel.contentview 的区别 多选右移cell不移动的问题
- tableView中cell的删除、插入、移动、复制粘贴问题详解代码分析
- iOS TableView如何刷新指定的cell 或section
- iOS 之 TableView如何刷新指定的cell 或section
- 关于TableView中不止一个section的情况下首个cell的y轴不为0的会造成的问题和解决办法
- 利用长按手势移动TableView中的Cell
- iOS刷新tableView某个cell或section
- iOS: TableView如何刷新指定的cell 或section
- iOS: TableView如何刷新指定的cell 或section
- iOS中获取用户所点击的位置信息,并通过这个位置信息得到tableView的中的cell的indexPath.section
- Cell特效之图片随着TableView的滑动上下移动
- iOS: TableView如何刷新指定的cell 或section
- tableview cell的移动