1

配列がアイテムをコアデータからテーブルビューにロードする場所にしようとしています。ただし、一部の値は重複しています。したがって、データを取得するために使用している配列内で、配列に重複を削除してからテーブル ビューに表示するように指示しようとしています。しかし、何らかの理由で重複を削除していません。コードは次のとおりです。

更新しました

- (void)viewDidLoad
{
    fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *weightEntity = [NSEntityDescription entityForName:@"Tracking" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:weightEntity];
    result = [self.managedObjectContext executeFetchRequest:fetchRequest error:nil];

    NSMutableArray *cleaningArray= [NSMutableArray new];
    NSSet *duplicatesRemover = [NSSet setWithArray:result];
    [duplicatesRemover enumerateObjectsUsingBlock: ^(id obj, BOOL* stop)
    {
        if(![cleaningArray containsObject: obj])
        {
            [cleaningArray addObject: obj];
        }
    }];

    cleanArray = [cleaningArray copy];

    [super viewDidLoad];

    // Do any additional setup after loading the view.
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{    
    static NSString *CellIdentifier = @"Cell";

    mainCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (mainCell == nil) {
        mainCell = [[dictionaryTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    Entity *person = [cleanArray objectAtIndex:indexPath.row];
    mainCell.nameLabel.text = person.date;

    return mainCell;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"%@", [cleanArray objectAtIndex:0]);
    return   cleanArray.count;
}

ありがとう!

4

2 に答える 2

2

-containsObject は ([string1 isEqual:string2] のように) 文字列を比較するので、これを行うことができます

NSArray* result = @[@"test",@"string",@"test",@"line"];
    NSMutableArray* cleanArray = [[NSMutableArray alloc] init];


   for (id object in result) {
        if (![cleanArray containsObject:object])
            [cleanArray addObject:object];
    }

NSLog (@"cleanArray %@",cleanArray);

ログ:

cleanArray (
    test,
    string,
    line
)


@dreamlax を更新し、@Zack とチャットしています。NSSet / isEqual の問題はニシンだったようです。「結果」配列には NSStrings は含まれず、Core Data のフェッチ要求が含まれます。返されるデータが一意でなくても、もちろんそれぞれが一意です。この配列は、要求に応じてフェッチ要求を実行するテーブル ビューと密接に結合されています。

そのため、Zack が行う必要があったのは、コア データをテーブル ビューから分離し、一意性を比較する文字列をプリフェッチし、その一意化された配列をテーブル ビューにフィードすることでした。NSSet は、一意の結果セットを取得するために正常に機能します。

fetchRequest = [[NSFetchRequest alloc] init]; 
NSEntityDescription *weightEntity = 
        [NSEntityDescription entityForName:@"Entity" 
                                 inManagedObjectContext:self.managedObjectContext]; 
[fetchRequest setEntity:weightEntity]; 
result = [self.managedObjectContext executeFetchRequest:fetchRequest 
                                                      error:nil]; 
NSMutableSet *dateSet = [NSMutableSet alloc] init]; 

for (id object in result) { 
    Entity *person = object; 
    NSString* dateString = person.date; 
    [dateSet addObject:dateString]; 
} 

self.dateArray = [dateSet allObjects]; 

次に、彼のtableViewで:

mainCell.nameLabel.text = [cleanArray objectAtIndex:indexPath.row];

于 2013-01-10T01:50:37.113 に答える
1

dreamlax の功績によると、 isEqual: メソッドをオーバーライドするだけの問題です。そのNSSetため、重複したオブジェクトは削除されません。

エンティティで、 isEqual メソッドをオーバーライドし、すべてのフィールドが等しい場合は true を返します。次に例を示します。

- (BOOL)isEqual:(id)anObject
{
    return self.attribute_1 == anObject,attribute_1 && ... && self.attribute_N== anObject.attribute_N; // Primitive types comparison in this example
}
于 2013-01-10T02:23:56.810 に答える