2010-05-07 6 views
0

이것은 몇 시간 동안 나를 괴롭 히고 있으며 그것을 파악할 수 없었습니다.UITableView에서 행을 어떻게 삭제합니까?

코어 데이터와 NSMutableArray를 사용하여 데이터를 테이블 뷰로 가져옵니다. 아래 그림과 같이.

CORE 데이터 배열 I가 일정 거리 내에있는 결과를 표시하는 데에만 표를 얻기 위해 노력하고

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

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

표보기. 이 방법은 특정 거리에 대한 결과가 아직 테이블에 남아 있고 그래픽으로 표시되지 않는다는 점을 제외하고는 작동합니다.

필자는 테이블을 포맷하기 전에 필터링 프로세스를 수행해야하지만이 작업을 수행 할 수 없다고 생각하게되었습니다.

도와주세요. 내 xcode 기술은 훌륭하지 않으므로 코드 제안이 도움이 될 것입니다.

답변

2

cellForRowAtIndexPath:에 전달 된 인덱스와 entityArray의 항목간에 직접적인 통신이 없으므로 cellForRowAtIndexPath:의 결과를 필터링하려고하지 않으려합니다. 예를 들어 entityArray 전체에 10 개의 요소가 있지만 3 개가 원하는 거리에 있으면 entityArray에 대한 인덱스는 0..9에서 시작되지만 테이블 뷰의 셀 0..2 만 존재합니다.

결과를 가져올 때 별도의 필터링 된 NSMutableArray를 만들어야하며 해당 배열의 내용을 TableView에 표시해야합니다.

필터링을 미리 수행해야하는 또 다른 이유는 tableview 컨트롤러에 -numberOfRowsInSection : 메서드를 구현하여 테이블 뷰에 몇 개의 행이 있는지 알려야하기 때문입니다. 당신은 분명히 필터링을하지 않고 무엇을 할 수 없습니다.

편집 :

당신은 같은 일을하고 필터링 된 배열을 만들 수 있습니다

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

(그리고 -dealloc 방법에 filteredArray를 해제하는 것을 잊지 마세요) 이해

+0

이하지만 어떻게 내가 필터링 된 배열을 만드는 방법에 대해 가야합니까? – James

+0

나는 당신을 사랑한다! 꿈처럼 작동합니다. – James

+0

@James : 사랑의 표현은 훌륭하지만, 다른 사람들에게 그들의 감사를 표하는 표준 방법은 올바른 답을 수락하는 것입니다. :) –

관련 문제