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];
}