2010-05-07 88 views
0

这一直困扰着我几个小时,我一直无法弄清楚。如何从UITableView中删除行?

我正在使用核心数据和NSMutableArray将数据导入tableview。如下所示。

核心数据ARRAY

NSMutableArray *mutableFetchResults = [CoreDataHelper getObjectsFromContext:@"Spot" :@"Name" :YES :managedObjectContext]; 
self.entityArray = mutableFetchResults; 

表视图

- (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; 
} 

我想只得到表中显示的结果是在一定距离内。这种方法在这里工作,除了一定距离的结果仍然在表格中,它们只是不可见的。

我被引导认为我必须在格式化表格之前执行过滤过程,但我似乎无法执行此操作。

请帮忙。我的xcode技巧并不精彩,所以代码建议会很有帮助。

回答

2

您不想过滤cellForRowAtIndexPath:中的结果,因为传递给cellForRowAtIndexPath:的索引与entityArray中的条目之间没有直接对应关系。例如,如果您的整个entityArray具有10个元素,但只有3个元素位于期望距离内,则指向entityArray的索引将从0到9,但只有您的tableview中的单元格0..2存在。

您应该在获取结果时创建一个单独的过滤NSMutableArray,并在TableView中显示该数组的内容。

您需要事先进行过滤的另一个原因是您需要在tableview控制器中实现-numberOfRowsInSection:方法,以告诉tableview有多少行。你显然不能做什么没有做过滤。

编辑:

您可以创建过滤数组做这样的事情:

@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 
    } 
} 

(不要忘记释放filteredArray在-dealloc法)

+0

我明白这一点,但如何我去创建过滤的数组? – James 2010-05-07 11:06:20

+0

我爱你!像梦一样工作 – James 2010-05-07 13:47:22

+0

@詹姆斯:虽然爱的表达很棒,但感谢某人投入的标准方式是接受他们的回答是正确的。 :) – 2010-05-07 18:57:54