This has been bugging me for hours now and i have not been able to figure it out.
这已经困扰了我几个小时了,我无法弄明白。
I am importing data into a tableview using core data and NSMutableArray. As shown below.
我使用核心数据和NSMutableArray将数据导入到tableview中。如下所示。
CORE DATA ARRAY
核心数据阵列
NSMutableArray *mutableFetchResults = [CoreDataHelper getObjectsFromContext:@"Spot" :@"Name" :YES :managedObjectContext];
self.entityArray = mutableFetchResults;
TABLE VIEW
表视图
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSManagedObject *object = (NSManagedObject *)[entityArray objectAtIndex:indexPath.row];
NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
NSString *lat1 = [object valueForKey:@"Email"];
//NSLog(@"Current Spot Latitude:%@",lat1);
float lat2 = [lat1 floatValue];
//NSLog(@"Current Spot Latitude Float:%g", lat2);
NSString *long1 = [object valueForKey:@"Description"];
//NSLog(@"Current Spot Longitude:%@",long1);
float long2 = [long1 floatValue];
//NSLog(@"Current Spot Longitude Float:%g", long2);
//Getting current location from NSDictionary
CoreDataTestAppDelegate *appDelegate = (CoreDataTestAppDelegate *) [[UIApplication sharedApplication] delegate];
NSString *locLat = [NSString stringWithFormat:appDelegate.latitude];
float locLat2 = [locLat floatValue];
//NSLog(@"Lat: %g",locLat2);
NSString *locLong = [NSString stringWithFormat:appDelegate.longitude];
float locLong2 = [locLong floatValue];
//NSLog(@"Long: %g",locLong2);
//Distance Shizzle
//Prime's Location
CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:lat2 longitude:long2];
//Home Location
CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:locLat2 longitude:locLong2];
double distance = [loc1 getDistanceFrom: loc2] / 1000;
int myInt = (int)(distance + (distance>0 ? 0.5 : -0.5));
//NSLog(@"INT VAL :%i", myInt);
NSMutableString* converted = [NSMutableString stringWithFormat:@"%.1f", distance];
[converted appendString: @" Km"];
//NSLog(@"Distance between Prime and home = %g", converted);
if (myInt < 11) {
cell.textLabel.text = [object valueForKey:@"Name"];
cell.detailTextLabel.text = [NSString stringWithFormat:converted];
}
else {
}
// Configure the cell...
return cell;
}
I am trying to get the table only to display results that are within a certain distance. This method here works apart from the fact that the results over a certain distance are still in the table, they are just not graphically visible.
我试图让表格只显示一定距离内的结果。这种方法除了在一定距离内的结果仍在表格中之外,它们只是图形上不可见。
I am led to believe that i have to carry out the filtering process before the formatting the table but i can not seem to do this.
我被引导相信我必须在格式化表之前执行过滤过程,但我似乎无法做到这一点。
Please help. My xcode skills are not brilliant so code suggestions would be helpfull.
请帮忙。我的xcode技能并不出色,所以代码建议会有所帮助。
1 个解决方案
#1
2
You don't want to be trying to filter the results in cellForRowAtIndexPath:
, because there isn't a direct correspondence between the index passed to cellForRowAtIndexPath:
and the entries in you entityArray. For example, if your entire entityArray has 10 elements, but only 3 are within the desired distance, indexes into entityArray will go from 0..9, but only cells 0..2 in your tableview will exist.
您不希望尝试过滤cellForRowAtIndexPath:中的结果,因为传递给cellForRowAtIndexPath的索引与您entityArray中的条目之间没有直接对应关系。例如,如果整个entityArray有10个元素,但只有3个元素在所需距离内,则entityArray的索引将从0..9开始,但只有tableview中的单元格0..2存在。
You should create a separate, filtered, NSMutableArray at the time you fetch your results, and display the contents of that array in your TableView.
您应该在获取结果时创建单独的,已过滤的NSMutableArray,并在TableView中显示该数组的内容。
The other reason you need to do your filtering in advance is that you need to implement the -numberOfRowsInSection: method in your tableview controller, to tell the tableview how many rows there are. You obviously can't do what without doing the filtering.
您需要提前进行过滤的另一个原因是您需要在tableview控制器中实现-numberOfRowsInSection:方法,以告诉tableview有多少行。如果不进行过滤,你显然无法做到。
Edit:
编辑:
You could create the filtered array doing something like:
您可以创建过滤后的数组,例如:
@property (nonatomic, retain) NSMutableArray *filteredArray;
self.filteredArray = [NSMutableArray array]; // start w/ empty array
for (MyObject *obj in self.entityArray) { // use fast enumeration to look at each element
if ([obj shouldBeAdded]) { // or whatever function you use to test distance
[self.filteredArray addObject:obj]; // add objects that match
}
}
(and don't forget to release filteredArray in your -dealloc method)
(并且不要忘记在-dealloc方法中释放filteredArray)
#1
2
You don't want to be trying to filter the results in cellForRowAtIndexPath:
, because there isn't a direct correspondence between the index passed to cellForRowAtIndexPath:
and the entries in you entityArray. For example, if your entire entityArray has 10 elements, but only 3 are within the desired distance, indexes into entityArray will go from 0..9, but only cells 0..2 in your tableview will exist.
您不希望尝试过滤cellForRowAtIndexPath:中的结果,因为传递给cellForRowAtIndexPath的索引与您entityArray中的条目之间没有直接对应关系。例如,如果整个entityArray有10个元素,但只有3个元素在所需距离内,则entityArray的索引将从0..9开始,但只有tableview中的单元格0..2存在。
You should create a separate, filtered, NSMutableArray at the time you fetch your results, and display the contents of that array in your TableView.
您应该在获取结果时创建单独的,已过滤的NSMutableArray,并在TableView中显示该数组的内容。
The other reason you need to do your filtering in advance is that you need to implement the -numberOfRowsInSection: method in your tableview controller, to tell the tableview how many rows there are. You obviously can't do what without doing the filtering.
您需要提前进行过滤的另一个原因是您需要在tableview控制器中实现-numberOfRowsInSection:方法,以告诉tableview有多少行。如果不进行过滤,你显然无法做到。
Edit:
编辑:
You could create the filtered array doing something like:
您可以创建过滤后的数组,例如:
@property (nonatomic, retain) NSMutableArray *filteredArray;
self.filteredArray = [NSMutableArray array]; // start w/ empty array
for (MyObject *obj in self.entityArray) { // use fast enumeration to look at each element
if ([obj shouldBeAdded]) { // or whatever function you use to test distance
[self.filteredArray addObject:obj]; // add objects that match
}
}
(and don't forget to release filteredArray in your -dealloc method)
(并且不要忘记在-dealloc方法中释放filteredArray)