how to clear all the rows of uitableview

被刻印的时光 ゝ 提交于 2019-12-30 04:51:06

问题


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

How can I do that?


回答1:


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;

    (...)
}



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




回答3:


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




回答4:


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];



回答5:


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];



回答6:


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




回答7:


The rowcount = i should go before the call.

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

Otherwise, it will crash.



来源:https://stackoverflow.com/questions/5291192/how-to-clear-all-the-rows-of-uitableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!