Problem数组
在表视图里面经过流畅而直观的动画来移动改变cell 和section 的位置动画
Solutionatom
使用 moveSection: toSection 表视图方法,将部分移动到一个新的位置。您还可使用moveRowAtIndexPath: toIndexPath: 方法移动表视图单元格从当前位置到另外一个新的地方spa
Discussion.net
移动表视图细胞和部分不一样于交换他们。让咱们来看一个例子,这将使这个更容易理解。比方说,你有三个部分中的表视图:第A, B和C。若是您移动部分A到C组,表视图会注意到这一举动,而后将B部分转移到A段以前的位置,并将移动C节第B的前面的位置可是,若是B段被移动到C组,表视图不会有移动A节可言,由于它是坐在上面,并不会与从新定位干扰B段和C在这种状况下, B段将被移动到C段和C段第B移动单元格时,一样的逻辑也将使用表格视图。code
为了证实这一点,让我么建立一个表视图,并拥有三个section,每一个section包含三个预先加载的三个cell。让咱们开始咱们的视图控制器的实现文件:orm
#import “ViewController.h”对象
static NSString *CellIdentifier = @“CellIdentifier”;索引
@interface ViewController () <UITableViewDelegate,UITableViewDataSource>three
@property (nonatomic,strong) UITableView *myTableView;
@property (nonatomic,strong) NSMutableArray *arrayOfSections;
咱们的视图控制器将成为本表视图的数据源。该表视图有部分,每一个部分都有行。咱们将继续数组的数组,数组的第一个是咱们的阵列部分,这将自己包含包含咱们的cell其余数组。在咱们的视图控制器的实现文件的顶部定义的arrayOfSections将承担这一责任。让咱们继续前进,填充这个数组:
- (NSMutableArray *) newSectionWithIndex:(NSUInteger)paramIndex cellCount:(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;
}
- (NSMutableArray *) arrayOfSections{
if (_arrayOfSections == nil){
NSMutableArray *section1 = [self newSectionWithIndex:1 cellCount:3];
NSMutableArray *section2 = [self newSectionWithIndex:2 cellCount:3];
NSMutableArray *section3 = [self newSectionWithIndex:3 cellCount:3];
_arrayOfSections = [[NSMutableArray alloc] initWithArray:0[section1,section2,section3]];
}
return _arrayOfSections;
}
而后咱们将实例化表格视图和实现UITableViewDataSource协议必要的方法来填充咱们的数据表视图:
- (NSInteger) numberOfSectionInTableView: (UITableView *)tableView{
return self.arrayOfSections.count;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSMutableArray *sectionArray = self.arrayOfSections[section];
return sectionArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSMutableArray *sectionArray = self.arrayOfSections[indexPath.section];
cell.textLabel.text = sectionArray[indexPath.row];
return cell;
}
- (void)viewDidLoad{
[super viewDidLoad];
self.myTableView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStyleGrouped];
[self.myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:CellIdentifier];
self.myTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.myTableView.delegate = self;
self.myTableView.dataSource = self;
[self.view addSubView:self.myTableView];
}
展现时间!!!我们先来看看部分如何被移动到新的位置?让咱们来写一个将移动第1至第3节的方法
- (void) moveSection1ToSection3{
NSMutableArray *section1 = self.arrayOfSection[0];
[self.arrayOfSections removeObject:section1];
[self.arrayOfSections removeObject:section1];
[self.myTableView moveSection:0 toSection:2];
}
我会留给你来决定,当你想调用这个方法,由于咱们没能在那一刻对咱们的UI中的按钮。你能够简单地建立一个导航控制器,其上放置一个导航按钮,而后调用该方法。
一旦正常运行的应用程序,你会看到部分列队从1到3 ,如图4-12
Figure 4-12. A table view with three sections, each containing three cells
当你调用moveSection1ToSection3方法,你会看到,第1被移动到第3节,第3节移动到第2节的前一个位置,最后第2移动到第1节的前一个位置(图4-13 )
Figure 4-13. Section 1 is moved to Section 3, and other sections are subsequently moved as well
移动单元是很是类似的运动部分。要移动单元格,咱们所要作的就是使用moveRowAtIndexPath : toIndexPath :方法。请记住,你能够移动一个单元格从一个部分到同一区段,或到一个新的章节。让咱们能够很容易和移动单元1中第1节电池2在同一节,看看会发生什么:
- (void) moveCell1InSection1ToCell2InSection1{
NSMutableArray *section1 = self.arrayOfSection[0];
NSString *cell1InSection1 = section1[0];
[section1 removeObject:cell1InSection1];
[section1 insertObject:cell1InSection1 atIndex:1];
NSIndexPath *sourceIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
NSIndexPath *destinationIndexPath = [NSIndexPath indexPathForRow:1 inSection:0];
[self.myTableView moveAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];
}
那么究竟是怎么回事这个代码?好了,咱们须要确保咱们的数据源保持正确的数据须要通过咱们四处移动单元格中要显示咱们的表视图,因此咱们会删除单元格1中第1节第一个。该移动单元2单元1和单元3到单元2 ,共2单元的阵列中。而后,咱们将插入电池1到数组的索引1 (第二个对象) 。这将使咱们的数组包含cell2,cell1 ,而后cell3 。以后作到这一点,其实咱们已提出了细胞在咱们的表视图。
让咱们把这个有点难度。如何移动单元格2在第1单元1第2节?
- (void) moveCell2InSection1ToCell1InSection2{ NSMutableArray *section1 = self.arrayOfSections[0];
NSMutableArray *section2 = self.arrayOfSections[1]; NSString *cell2InSection1 = section1[1];
[section1 removeObject:cell2InSection1]; [section2 insertObject:cell2InSection1
atIndex:0];
NSIndexPath *sourceIndexPath = [NSIndexPath indexPathForRow:1 inSection:0];
NSIndexPath *destinationIndexPath = [NSIndexPath indexPathForRow:0 inSection:1];
[self.myTableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];
}