how to clear all the rows of uitableview

前端 未结 8 550
半阙折子戏
半阙折子戏 2021-01-02 23:32

I added a button on the uinavigationbar I want to use it to clear all the rows of uitablview

How can I do that?

相关标签:
8条回答
  • 2021-01-02 23:42

    A bit late... but try this:

    Where you want to clear the table:

    // clear table
    clearTable = YES;
    [table reloadData];
    clearTable = NO;
    

    This function should look like:

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
        if(clearTable)
            return 0;
    
        (...)
    }
    
    0 讨论(0)
  • 2021-01-02 23:46

    Before you are reloading the table remove all objects from your tableView array (The array which populated your tableView) like so:

    [myArray removeAllObjects];
    [tableView reloadData];
    
    0 讨论(0)
  • 2021-01-02 23:47

    The crash is occurring because you need to reset number if rows count in the -

    -(NSInteger)tableView:(UITableView *)tableView 
     numberOfRowsInSection:(NSInteger)section
    {
        return [tempArray count];
    }
    

    one way is use a flag variable as below:

    -(NSInteger)tableView:(UITableView *)tableView 
     numberOfRowsInSection:(NSInteger)section
    {
       if (deleting)
          return rowcount;
       else
           return [tempArray count];
    }
    

    Also you need to modify the deleting code as below:

        deleting = YES;
        for (i = [uiTable numberOfRowsInSection:0] - 1; i >=0 ; i--)
        { 
        NSIndexPath *index = [NSIndexPath indexPathForRow:i inSection:0]; 
        [uiTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:index] withRowAnimation:UITableViewRowAnimationFade]; 
    
         rowcount = i;
        } 
    
        deleting = NO;
        [uiTable reloadData];
    

    in your .h file

    BOOL deleting;
    NSInteger  rowcount;
    

    Hope this helps...

    0 讨论(0)
  • 2021-01-02 23:59

    Simple.. set your data source (NSArray or NSDictionary) to nil and [self.tableView reloadData]!

    0 讨论(0)
  • 2021-01-02 23:59

    What do you exactly mean by clearing the row? Do you still want them to be there, but without text? If yes, here's this code:

       UITableViewCell *cell;
    
        NSIndexPath *index;
    
            for (int i = 0 ; i < count; i++) {
                index = [NSIndexPath indexPathForRow:i inSection:0];
                cell = [tableView cellForRowAtIndexPath:index];
                cell.textLabel.text = @"";
            }
    

    If you want to delete them, you can use this code:

    [uiTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:index] withRowAnimation:UITableViewRowAnimationFade];
    
    0 讨论(0)
  • 2021-01-03 00:01

    The rowcount = i should go before the call.

    [uiTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:index]
    withRowAnimation:UITableViewRowAnimationFade];
    

    Otherwise, it will crash.

    0 讨论(0)
提交回复
热议问题