How to clear all uitableview lines

I added a button on the uinavigationbar that I want to use to clear all uitablview lines

How can i do this?

+6
objective-c iphone cocoa-touch
source share
7 answers

A bit late ... but try the following:

If you want to clear the table:

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

This function should look like this:

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if(clearTable) return 0; (...) } 
+12
source share

There must 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 extracted from. If you use indexPath to access an array of values ​​to populate the tableView cells, you can programmatically switch to an array of empty values ​​or even just return empty cells from this method when this condition is correct.

+2
source share

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

+2
source share

What do you mean by clearing a line? Do you still want them there, but without text? If yes, here is 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 remove them, you can use this code:

 [uiTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:index] withRowAnimation:UITableViewRowAnimationFade]; 
+1
source share

Before reloading the table, remove all objects from your tableView array (the array that populated your tableView) like this:

 [myArray removeAllObjects]; [tableView reloadData]; 
+1
source share

The accident happens because you need to reset the number if the number of lines in is -

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

In one case, a flag variable is used, as shown below:

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

You also need to change the delete code as shown 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
source share

rowcount = i must go before the call.

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

Otherwise, it will fail.

0
source share

All Articles