6

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

How can I do that?

Andy Obusek
  • 12,614
  • 4
  • 41
  • 62
Ali
  • 1,975
  • 5
  • 36
  • 55

8 Answers8

12

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;

    (...)
}
joao
  • 4,067
  • 3
  • 33
  • 37
2

There should be a UITableView delegate method that populates the UITableView control:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

Find this method and change where the data for each cell is being pulled from. If you are currently using the indexPath to access an array of values to populate your tableView cells, you can programmatically switch to an array of empty values, or even just return empty cells from this method when the condition is right.

2

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

REALFREE
  • 4,378
  • 7
  • 40
  • 73
  • wouldn't that just remove the array and where as the objects will still exist ? if Yes, then it is not a good solution as it creates memory leaks... Instead you should try NSMutableArray *arrayThatYouCanRemoveObjects = [NSMutableArray arrayWithArray:your_array]; [arrayThatYouCanRemoveObjects removeObjectAtIndex:your_object_index]; [your_array release]; your_array = [[NSArray arrayWithArray: arrayThatYouCanRemoveObjects] retain]; copied from http://stackoverflow.com/questions/1728475/nsarray-remove-item-from-array – yunas Jan 03 '12 at 11:40
1

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];
Waqleh
  • 9,741
  • 8
  • 65
  • 103
1

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];
Basel
  • 2,368
  • 1
  • 16
  • 21
  • in for loop I increment and use: index = [NSIndexPath indexPathForRow:i inSection:0];;[uiTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:index] withRowAnimation:UITableViewRowAnimationFade]; but after the seccont iteration it crash – Ali Mar 14 '11 at 08:28
  • I think that you're looping from 0 incrementally. Try the opposite as follows: for (i = [uiTable numberOfRowsInSection:0] - 1; i >=0 ; i--){ NSIndexPath *index = [NSIndexPath indexPathForRow:i inSection:0]; [uiTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:index] withRowAnimation:UITableViewRowAnimationFade]; } – Basel Mar 14 '11 at 08:35
  • the same error , I tried your comment but the same error appeared – Ali Mar 14 '11 at 09:04
0

The rowcount = i should go before the call.

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

Otherwise, it will crash.

Dima Kozhevin
  • 3,602
  • 9
  • 39
  • 52
Jim D
  • 1
  • 2
0
tableView.dataSource = nil
tableView.reloadData()
Haiyuan
  • 103
  • 2
  • 5
0

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...

Ram G.
  • 3,045
  • 2
  • 25
  • 31