如何从UITableView删除行? [英] How do you delete rows from UITableView?

查看:168
本文介绍了如何从UITableView删除行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



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



CORE DATA ARRAY

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

表VIEW

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

//从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);

//距离Shizzle
// Prime的位置
CLLocation * loc1 = [[CLLocation alloc] initWithLatitude:lat2 longitude:long2];
//首页位置
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];

[convert appendString:@Km];

// NSLog(@Prime和home之间的距离=%g,转换);

if(myInt< 11){
cell.textLabel.text = [object valueForKey:@Name];
cell.detailTextLabel.text = [NSString stringWithFormat:converted];
}
else {


}

//配置单元格...

返回单元格;
}



我试图获取表格只显示某一距离。这种方法在这里工作,除了一定距离的结果仍然在表中,它们只是不可见的。



我导致相信,在格式化表之前必须执行过滤过程,但我似乎不能这样做。



请帮助。我的xcode技能不是很出色,所以代码建议会很有帮助。

解决方案

你不想尝试过滤结果在 cellForRowAtIndexPath:中,因为传递到 cellForRowAtIndexPath:的索引与您中的条目之间没有直接对应entityArray。例如,如果你的整个entityArray有10个元素,但只有3个在所需的距离内,索引到entityArray将从0..9,但只有单元格0..2在你的tableview将存在。



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



你需要事先进行过滤的另一个原因是你需要在你的tableview控制器中实现-numberOfRowsInSection:方法,告诉tableview有多少行。



编辑:



您可以创建过滤的数组做类似于:

  @property(nonatomic,retain)NSMutableArray * filteredArray; 

self.filteredArray = [NSMutableArray array]; //开始w /空数组

for(MyObject * obj in self.entityArray){//使用快速枚举查看每个元素
if([obj shouldBeAdded]){或任何用于测试距离的函数
[self.filteredArray addObject:obj]; //添加与
匹配的对象}
}

忘记在您的-dealloc方法中释放filteredArray)


This has been bugging me for hours now and i have not been able to figure it out.

I am importing data into a tableview using core data and NSMutableArray. As shown below.

CORE DATA ARRAY

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

TABLE VIEW

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

I am trying to get the table only to display results that are within a certain distance. This method here works apart from the fact that the results over a certain distance are still in the table, they are just not graphically visible.

I am led to believe that i have to carry out the filtering process before the formatting the table but i can not seem to do this.

Please help. My xcode skills are not brilliant so code suggestions would be helpfull.

解决方案

You don't want to be trying to filter the results in cellForRowAtIndexPath:, because there isn't a direct correspondence between the index passed to cellForRowAtIndexPath: and the entries in you entityArray. For example, if your entire entityArray has 10 elements, but only 3 are within the desired distance, indexes into entityArray will go from 0..9, but only cells 0..2 in your tableview will exist.

You should create a separate, filtered, NSMutableArray at the time you fetch your results, and display the contents of that array in your TableView.

The other reason you need to do your filtering in advance is that you need to implement the -numberOfRowsInSection: method in your tableview controller, to tell the tableview how many rows there are. You obviously can't do what without doing the filtering.

Edit:

You could create the filtered array doing something like:

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

(and don't forget to release filteredArray in your -dealloc method)

这篇关于如何从UITableView删除行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆