コア データから入力さUITableView
れる配列 ( ) を介して入力される があります。tableArray
それぞれUITableViewCell
に作成時に番号が割り当てられ、番号は配列に格納されます。( numberArray
)
ユーザーが行を並べ替えると、数字は配列内で移動します(もちろん、と組み合わせてtableView
)
したがって、ここでは 2 つの Mutable 配列が使用されています。
はのnumberArray
番号 (または順序) を保持しますTableViewCells
。UITableViewCell
のテキスト ( ) を保持する配列をソートして、 が保持するtableArray
のと同じ順序を反映する必要がありnumberArray
ます。
また、これは重要です。前に述べたように、各セルには番号が割り当てられます。この番号はnumberArray
、
同じ場所に同じ値を保持するには、両方の配列を並べ替える必要があります。
たとえば、次のようになります。
tableArray
いくつかのオブジェクトを保持します:
1) hi
2) whats Up
3) this
4) is cool!
ご覧のとおり、ここの各オブジェクトには 1 ~ 4 の番号が割り当てられています。そして、これらの各番号が に追加されますnumberArray
。
ユーザーはセルを移動できるので、明らかに数字の順序が変わります。
したがって、ビューが読み込まれると、numberArray
それが正しいかどうかの正確な順序を取得する必要があります
1,2,3,4 or 2,4,3,1
tableArray
と同じ順序を反映するように並べ替える必要があるnumberArray
ため、ビューがロードされたときに、numberArray's
順序が 2,3,4,1の場合、順序tableArray's
を次のように設定する必要があります。
2「どういたしまして」、3「これ」、4「かっこいい!」、1「こんにちは」。
私はこれを介してこれを行うことができると信じていますNSPredicate
。
どんな助けでも大歓迎です!
編集
cellForRow:
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString * identifier = @"identifier";
self.myCell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (self.myCell == nil) {
self.myCell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
HandgunAmmo *handgunAmmo = [self.tableArray objectAtIndex:indexPath.row];
self.myCell.brandLabel.text = handgunAmmo.brand;
self.myCell.caliberLabel.text = handgunAmmo.caliber;
self.myCell.numberOfRoundsLabel.text = handgunAmmo.numberOfRounds;
return self.myCell;
}
そして私のviewWIllAppear
方法では:
-(void)viewWillAppear:(BOOL)アニメーション{
if (self.context == nil)
{
self.context = [(RootAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
}
NSFetchRequest *request = [[NSFetchRequest alloc]init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"HandgunAmmo" inManagedObjectContext:self.context];
[request setEntity:entity];
NSError *error;
NSMutableArray *array = [[self.context executeFetchRequest:request error:&error] mutableCopy];
[self setTableArray:array];
[self.ammoTable reloadData];
[super viewWillAppear:YES];
}
したがって、変更されたときに配列が永続的にならない理由は、コアデータからデータをロードし、コアデータから[self setTableArray:array];
すべてのデータを配列に再ロードする呼び出しを行ってから、テーブルビューに配列を設定するためです。array
したがって、に等しく設定する前に、を並べ替えることができる必要がありますself.tableArray
。
お手伝いありがとう!