[英]How to delete cell with parse data in Xcode 5?
嗨,我正在使用parse
為我的應用程序存儲數據,並希望能夠在向左滑動單元格時將其刪除,盡管當我單擊Delete時,我必須手動下拉並刷新以使數據消失。
無論如何,只要在滑動單元格后單擊“刪除”,就可以使其動畫並消失嗎?
這是我用來刪除ViewController.m
單元數據的代碼:
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
[self.tableView setEditing:editing animated:animated];
}
- (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 *object = [self.objects objectAtIndex:indexPath.row];
[object deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
[self refreshControl];
[tableView reloadData];
}];
}
}
提前致謝!
您甚至在不知道數據是否已成功刪除之前就調用了該方法。
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//try this
PFObject *object = [self.objects objectAtIndex:indexPath.row];
[self.objects removeObjectAtIndex:indexPath.row];
//found the code for removing a row.
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
// Remove the row from data model
[object deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!succeeded){
//we had an error
// gives the error log and also how it relates to the user.
NSLog(ERROR: %@, %@", error, [error userInfo]);
}
}];
}
}
編輯:嘗試上面編輯過的解決方案。
要使單元正確設置動畫,您需要完成以下任務:
self.objects
) 嘗試這個:
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// 2. Update client model
[self.objects removeObjectAtIndex:indexPath.row];
// 3. Animate cells
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
// 1. Update backend model
PFObject *object = [self.objects objectAtIndex:indexPath.row];
[object deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (error) {
// If the backend reports an error, you'd need to readd the cell or
// display an error message to the user
}
}];
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.