目前,我可以删除要滑动删除的表格视图单元格。但是,只有刷新UITableView后,才会对单元格删除进行动画处理。我希望删除单元格,因为我在刷卡删除后按下红色删除按钮,而不是每次删除单元格时都要刷新它。这是我的参考代码:
- (id)initWithCoder:(NSCoder *)aCoder
{
self = [super initWithCoder:aCoder];
if (self) {
// Custom the table
// The className to query on
self.parseClassName = @"fgrep";
// The key of the PFObject to display in the label of the default cell style
self.textKey = @"name";
// Whether the built-in pull-to-refresh is enabled
self.pullToRefreshEnabled = YES;
// Whether the built-in pagination is enabled
self.paginationEnabled = NO;
// The number of objects to show per page
//self.objectsPerPage = 10;
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(refreshTable:)
name:@"refreshTable"
object:nil];
}
- (void)refreshTable:(NSNotification *) notification
{
// Reload the recipes
[self loadObjects];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"refreshTable" object:nil];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (PFQuery *)queryForTable
{
PFQuery *fgquery = [PFQuery queryWithClassName:@"fgrep"];
[fgquery whereKey:@"user" equalTo:[PFUser currentUser]];
// If no objects are loaded in memory, we look to the cache first to fill the table
// and then subsequently do a query against the network.
/* if ([self.objects count] == 0) {
query.cachePolicy = kPFCachePolicyCacheThenNetwork;
}*/
// [query orderByAscending:@"name"];
return fgquery;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
{
static NSString *simpleTableIdentifier = @"FGRepCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
// Configure the cell
UILabel *nameLabel = (UILabel*) [cell viewWithTag:101];
nameLabel.text = [object objectForKey:@"name" ];
UILabel *operationtime = (UILabel*) [cell viewWithTag:102];
operationtime.text = [object objectForKey:@"fgoperation"];
UILabel *yards = (UILabel*) [cell viewWithTag:103];
yards.text = [object objectForKey:@"fgyards"];
UILabel *hashe = (UILabel*) [cell viewWithTag:104];
hashe.text = [object objectForKey:@"fghash"];
UILabel *makeormiss = (UILabel*) [cell viewWithTag:105];
makeormiss.text = [object objectForKey:@"fgmakeormiss"];
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{ if (editingStyle == UITableViewCellEditingStyleDelete) {
// Remove the row from data model
PFObject *objectToDel = [self.objects objectAtIndex:indexPath.row];
[objectToDel deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
[self loadView];
}
];
}}
- (void) objectsDidLoad:(NSError *)error
{
[super objectsDidLoad:error];
NSLog(@"error: %@", [error localizedDescription]);
}
@end
我认为我的主要问题与此代码段有关:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{ if (editingStyle == UITableViewCellEditingStyleDelete) {
// Remove the row from data model
PFObject *objectToDel = [self.objects objectAtIndex:indexPath.row];
[objectToDel deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
[self loadView];
}
];
}}
再一次,我可以在滑动并按下红色删除按钮后从表格视图中删除单元格,但只有在刷新表格视图后才能删除单元格。一旦按下刷过单元格后显示的红色删除按钮,我想删除单元格。
答案 0 :(得分:1)
您需要:
而不是重新加载tableviewbeginUpdates
,deleteRowsAtIndexPaths:withRowAnimation:
和endUpdates
醇>