2010-05-07 86 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