我正在尝试将数组从核心数据加载到表视图中。有些值是重复的。因此,在我用来获取数据的数组中,我试图告诉数组删除所有重复项,然后将其显示在表格视图中。但由于某种原因,它没有删除重复项。这是代码:
更新
- (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;
}
谢谢!