UITableView 除了展示與刪除,還支援把整組 Section 或單個 Cell 從一處搬到另一處。關鍵觀念是:表格只是畫面,真正的數據來源是你自己的數組。因此任何移動操作都必須「兩邊同步」——先改數據,再叫表格跟著動。本文用一個三個 Section、每組三行的例子示範這套配對邏輯。
ZYViewController.h:
#import <UIKit/UIKit.h> @interface ZYViewController : UIViewController<UITableViewDelegate,UITableViewDataSource> @property(nonatomic,strong) UITableView *myTableView; @property(nonatomic,strong) NSMutableArray *arrayOfSections; // 數據源 @end
ZYViewController.m:
@synthesize myTableView;
@synthesize arrayOfSections;
// 建立一個 Section 的內容
-(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];
// 初始化數據源:三個 Section,各三行
arrayOfSections = [[NSMutableArray alloc] init];
[arrayOfSections addObject:[self newSectionWithIndex:1 withCellCount:3]];
[arrayOfSections addObject:[self newSectionWithIndex:2 withCellCount:3]];
[arrayOfSections addObject:[self newSectionWithIndex:3 withCellCount:3]];
myTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
self.myTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
myTableView.delegate = self;
myTableView.dataSource = self;
// 右側按鈕:移動 Section
self.navigationItem.rightBarButtonItem =
[[UIBarButtonItem alloc] initWithTitle:@"MoveSection"
style:UIBarButtonItemStylePlain
target:self
action:@selector(movewSection1ToSection3)];
// 左側按鈕:移動 Cell
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 的行數
-(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;
}
// 每行的內容
-(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;
}
接下來是兩個移動動作。把索引 0 的 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 不同:
-(void)moveCell2InSectionToCellInSection2{
NSMutableArray *section1 = [arrayOfSections objectAtIndex:0];
NSMutableArray *section2 = [arrayOfSections objectAtIndex:1];
NSString *cell2InSection1 = [section1 objectAtIndex:1];
[section1 removeObject:cell2InSection1]; // 從來源 Section 移除
[section2 insertObject:cell2InSection1 atIndex:0]; // 插入目標 Section 的開頭
NSIndexPath *sourceIndexPath = [NSIndexPath indexPathForRow:1 inSection:0];
NSIndexPath *destinationIndexPath = [NSIndexPath indexPathForRow:0 inSection:1];
[myTableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];
}
順帶提醒一個實務陷阱:cellForRowAtIndexPath: 裡每次都重新 alloc 一個新的 cell,示範可以,但正式專案應改用 dequeueReusableCellWithIdentifier: 的標準重用寫法,否則大量滾動時內存會快速堆積。
初始狀態:
點擊右側按鈕後,最後一個 Section 被移到最前面:
點擊左側按鈕後,Cell 完成跨 Section 搬移: