[英]How do you delete rows from UITableView?
这已经困扰了我好几个小时,而我却无法弄清楚。
我正在使用核心数据和NSMutableArray将数据导入到tableview中。 如下所示。
核心数据阵列
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技能不是很出色,因此代码建议会有所帮助。
您不想尝试过滤cellForRowAtIndexPath:
的结果,因为传递给cellForRowAtIndexPath:
的索引与您的EntityArray中的条目之间没有直接对应关系。 例如,如果整个entityArray有10个元素,但在所需距离内只有3个元素,则entityArray的索引将从0..9开始,但表视图中仅存在单元格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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.