我正在使用此代码删除UITableViewCell,但是当我滑动删除时,它首先显示右侧的减号,然后删除。
我拍了一段视频来帮助说明我的观点:Video on YouTube
- (void)setEditing:(BOOL)editing animated:(BOOL)animate
{
[self.tableView setEditing: !self.tableView.editing animated:YES];
if (self.tableView.editing)
[self.navigationItem.leftBarButtonItem setTitle:@"Done"];
else
[self.navigationItem.leftBarButtonItem setTitle:@"Edit"];
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
PFObject *routine= [self.routineArray objectAtIndex:indexPath.row];
[routine deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error) {
[self.routineArray removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
// [MKInfoPanel showPanelInView:self.view type:MKInfoPanelTypeError title:@"Routine Deleted" subtitle:@"You have succesfully deleted the routine!" hideAfter:2];
} else {
NSLog(@"%@", error);
}
}];
}
}
编辑:
- (void)loadData
{
PFQuery *query = [PFQuery queryWithClassName:@"Routine"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
self.routineArray = [objects mutableCopy];
[self.tableView reloadData];
} else {
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];
}
-(void)addRoutine
{
PFObject *routine = [[PFObject alloc] initWithClassName:@"Routine"];
[routine setObject:self.entered forKey:@"name"];
[routine saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error) {
[self loadData];
} else {
// There was an error saving the routine.
}
}];
}
答案 0 :(得分:2)
看起来有两个问题。第一个看起来像-deleteInBackgroundWithBlock:在按下删除按钮后执行它的块需要相当长的时间。如果您没有使用NSFetchedResultsController
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
PFObject *routine = [self.routineArray objectAtIndex:indexPath.row];
[self.routineArray removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[routine deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error) {
//[MKInfoPanel showPanelInView:self.view type:MKInfoPanelTypeError title:@"Routine Deleted" subtitle:@"You have succesfully deleted the routine!" hideAfter:2];
} else {
NSLog(@"%@", error);
}
}];
}
}
如果您喜欢淡出行的不透明度以外的其他内容,也可以使用不同的动画。如果您仅定位iOS 5.0,则可以使用UITableViewRowAnimationAutomatic
让UIKit根据具体情况尝试选择效果最佳的动画。
另一个问题看起来在按下删除后重新打开了编辑模式。您不应该覆盖-setEditing:animated:
,因此请尝试完全删除该方法。
在-viewDidLoad:
中,您可以执行以下操作以免费获得编辑行为:
self.navigationItem.leftBarButtonItem = self.editButtonItem;
见:
还应注意,当您检查编辑状态时,您应该使用isEditing访问器。
要避免调用-reloadData
,只需将单个新对象添加到dataSource数组,然后添加tableView行,然后将其保存到核心数据存储中。这与从表视图中删除行时必须执行的操作完全相反。如果您需要将routine
对象附加到tableView的末尾并且没有自定义排序顺序,这将起作用。否则,您必须在所需索引处将routine
对象插入self.routineArray
,然后创建正确的indexPath以将tableView行插入tableView中的所需位置。
- (void)addRoutine
{
PFObject *routine = [[PFObject alloc] initWithClassName:@"Routine"];
[routine setObject:self.entered forKey:@"name"];
[self.routineArray addObject:routine];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:([self.routineArray count]-1) inSection:0];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath withRowAnimation:UITableViewRowAnimationAutomatic
[routine saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error) {
[self loadData];
} else {
// There was an error saving the routine.
}
}];
}