【问题标题】:How do you delete rows from UITableView?如何从 UITableView 中删除行?
【发布时间】:2010-05-07 00:39:06
【问题描述】:

这已经困扰了我好几个小时了,我一直无法弄清楚。

我正在使用核心数据和 NSMutableArray 将数据导入表视图。如下图。

核心数据阵列

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 技能并不出色,因此代码建议会有所帮助。

【问题讨论】:

    标签: objective-c iphone core-data


    【解决方案1】:

    您不想尝试过滤 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
        }
    }
    

    (别忘了在你的-dealloc方法中释放filteredArray)

    【讨论】:

    • 我明白这一点,但我该如何创建过滤后的数组?
    • 我爱你!像梦一样工作
    • @James:虽然表达爱意很棒,但感谢某人输入的标准方式是接受他们的回答是正确的。 :)