概要
下記のようなSQLiteのテーブルからデータを表示しているUITableViewに
セルの並び替えを実装し、その状態を保持する方法を考えました。
TableViewController.m
#import "TableViewController.h"
#import "FMDatabase.h"
@interface TableViewController () {
NSMutableArray *subject;
NSMutableArray *subjectID;
NSMutableArray *sequence;
NSArray *paths;
NSString *dir;
FMDatabase *db;
}
@end
@implementation TableViewController
- (void)viewDidLoad {
[super viewDidLoad];
// データベース,テーブルを作成
paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
dir = [paths objectAtIndex:0];
db = [FMDatabase databaseWithPath:[dir stringByAppendingPathComponent:@"app.db"]];
NSString *create = @"CREATE TABLE IF NOT EXISTS tr_sample (subject_id INTEGER PRIMARY KEY AUTOINCREMENT, subject TEXT);";
[db open];
[db executeUpdate:create];
[db close];
// インサート
NSString *insert = @"INSERT INTO tr_sample (subject) VALUES (?)";
[db open];
[db executeUpdate:insert, @"あ"];
[db executeUpdate:insert, @"い"];
[db executeUpdate:insert, @"う"];
[db executeUpdate:insert, @"え"];
[db executeUpdate:insert, @"お"];
[db close];
// セレクト
subject = [[NSMutableArray alloc] init];
subjectID = [[NSMutableArray alloc] init];
sequence = [[NSMutableArray alloc] init];
NSString *select = [[NSString alloc] initWithFormat:@"SELECT subject_id, subject FROM tr_sample ORDER BY subject_id"];
[db open];
FMResultSet *results = [db executeQuery:select];
while ([results next]) {
NSDictionary *dic = [results resultDictionary];
[subject addObject:dic[@"subject"]];
[subjectID addObject:dic[@"subject_id"]];
[sequence addObject:dic[@"sequence"]];
}
[results close];
[db close];
}
// TableViewの行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return subject.count;
}
// セルに表示
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier];
}
// ラベルにデータを表示する
cell.textLabel.text = subject[indexPath.row];
return cell;
}
@end
セルの並び替えを実装する
viewDidLoadなどに編集ボタンを実装する。
TableViewController.m
self.navigationItem.rightBarButtonItem = [self editButtonItem];
編集モード時に並び替えを行えるようにする。
TableViewController.m
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
TableViewController.m
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
これで編集ボタンを押すとセルの並び替えができるようになります。
セルを移動させた時にfromIndexPath
とtoIndexPath
でそれぞれ移動前、移動後のIndexPathが取得できます。
並び替えた順番をデータベースに保持する
テーブル作成時に並び順を保存しておくためのカラムを作る。
TableViewController.m
NSString *create = @"CREATE TABLE IF NOT EXISTS tr_sample (subject_id INTEGER PRIMARY KEY AUTOINCREMENT, subject TEXT, sequence INTEGER);";
セレクト文のORDER BYで参照するカラムを変更する。
TableViewController.m
NSString *select = [[NSString alloc] initWithFormat:@"SELECT subject_id, subject, sequence FROM tr_sample ORDER BY sequence"];
セルの並び替え時に並び順を更新する。
並び替えを実装した時に書いたメソッドにさらに書き加えていきます。
TableViewController.m
// セルの並び替え時に実行される
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
NSInteger fromIndex = fromIndexPath.row;
NSInteger toIndex = toIndexPath.row;
// 移動先と移動元の間にあるセルの並び順を1つずつスライドさせる
// 下に移動させた場合
if(fromIndex < toIndex){
while (fromIndex < toIndex) {
NSString *update = @"UPDATE tr_sample SET sequence = ? WHERE subject_id = ?";
[db open];
[db executeUpdate:update,[NSNumber numberWithInteger:fromIndex],subjectID[fromIndex+1]];
[db close];
fromIndex++;
}
// 上に移動させた場合
}else if (fromIndex > toIndex){
while (fromIndex > toIndex) {
NSString *update = @"UPDATE tr_sample SET sequence = ? WHERE subject_id = ?";
[db open];
[db executeUpdate:update,[NSNumber numberWithInteger:fromIndex],subjectID[fromIndex-1]];
[db close];
fromIndex--;
}
}
// 移動させたセルの並び順を更新
NSString *update = @"UPDATE tr_sample SET sequence = ? WHERE subject_id = ?";
[db open];
[db executeUpdate:update,[NSNumber numberWithInteger:toIndex],subjectID[fromIndexPath.row]];
[db close];
NSString *select = [[NSString alloc] initWithFormat:@"SELECT subject_id, subject, sequence FROM tr_sample ORDER BY sequence"];
[db open];
FMResultSet *results = [db executeQuery:select];
// 更新したデータをセレクト
subject = [[NSMutableArray alloc] init];
subjectID = [[NSMutableArray alloc] init];
sequence = [[NSMutableArray alloc] init];
while ([results next]) {
NSDictionary *dic = [results resultDictionary];
[subject addObject:dic[@"subject"]];
[subjectID addObject:dic[@"subject_id"]];
[sequence addObject:dic[@"sequence"]];
}
[results close];
[db close];
}
まとめ
いろいろツッコミどころはあると思いますが、
とりあえずこんな感じで目的は果たせたと思います。
他にもっといい方法がありそうな気がしていますが……