2017-09-22 160 views
0

我是編程新手。下面的代碼使用滑動刪除一行,但在刷新所有行列表後出現。 我有以下代碼:UItableview滑動刪除iOS 11不刷新/重新加載數據

- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UIContextualAction *delete = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive 
                     title:@"DELETE" 
                     handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) { 
                      NSLog(@"index path of delete: %@", indexPath); 
                      completionHandler(YES); 


                     }]; 
    delete.backgroundColor = [UIColor purpleColor]; //arbitrary color 

    UISwipeActionsConfiguration *swipeActionConfig = [UISwipeActionsConfiguration configurationWithActions:@[delete]]; 
    swipeActionConfig.performsFirstActionWithFullSwipe = NO; 

    return swipeActionConfig; 

} 

我使用如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    FCell = (favouriteCell *)[tableView dequeueReusableCellWithIdentifier:@"favouriteCell"]; 
    FCell.selectionStyle=UITableViewCellSelectionStyleNone; 
    if (FCell == nil) 
    { 
     FCell = [[[NSBundle mainBundle]loadNibNamed:@"favouriteCell" owner:nil options:nil] objectAtIndex:0]; 
    } 

    FCell.nameLBL.text=[[favDetails objectAtIndex:indexPath.row] valueForKey:@"name"]; 
    FCell.poetLBL.text=[[favDetails objectAtIndex:indexPath.row] valueForKey:@"poet"]; 
    return FCell; 

} 
+0

它似乎沒有從任何數據源中刪除該行。 在您正在記錄刪除的索引路徑的completionHandler中,您需要從數據源(數組等)中刪除該行。否則,當你的表從數據源重新加載時,它會重新出現。 – Stephen

+0

謝謝,最好的辦法是做什麼? – Simi

+0

你用什麼結構作爲數據源?你能分享你的cellForRowAtIndexPath的一些代碼嗎? – Stephen

回答

0

在completionHandler你在哪裏記錄刪除的索引路徑,則需要從數據源中刪除該行(數組等)。否則,當你的表從數據源重新加載時,它會重新出現。

trailingSwipeActionsConfigurationForRowAtIndexPath代碼應該是這樣的:

- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath { 
     UIContextualAction *delete = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive 
                      title:@"DELETE" 
                      handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) { 
                       NSLog(@"index path of delete: %@", indexPath); 
[favDetails removeObjectAtIndex:indexPath.row];             
completionHandler(YES); 


                      }]; 
     delete.backgroundColor = [UIColor purpleColor]; //arbitrary color 

     UISwipeActionsConfiguration *swipeActionConfig = [UISwipeActionsConfiguration configurationWithActions:@[delete]]; 
     swipeActionConfig.performsFirstActionWithFullSwipe = NO; 

     return swipeActionConfig; 

    } 

作爲行從表中刪除視覺這將會從您的數據源中刪除的對象。

+0

非常感謝,但它顯示一個錯誤。我在我的問題中添加了NSlog代碼。 – Simi

+0

我不確定您的問題中的新代碼如何與原始代碼相關。我相信我的回答可以解決你原來的問題。 你可能應該用你的錯誤開一個新的問題。 – Stephen

+0

爲了清晰起見,我編輯了我的答案。 – Stephen