如何清除uitableview的所有行

时间:2011-03-13 17:45:17

标签: iphone objective-c cocoa-touch

我在uinavigationbar上添加了一个按钮,我想用它来清除uitablview的所有行

我该怎么做?

8 个答案:

答案 0 :(得分:12)

有点晚......但试试这个:

您要清除表格的位置:

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

此功能应如下所示:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if(clearTable)
        return 0;

    (...)
}

答案 1 :(得分:2)

应该有一个UITableView委托方法来填充UITableView控件:

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

找到此方法并更改从中拉出每个单元格的数据的位置。如果您当前正在使用indexPath访问一个值数组来填充tableView单元格,则可以通过编程方式切换到空值数组,或者甚至在条件合适时从此方法返回空单元格。

答案 2 :(得分:2)

简单..将您的数据源(NSArray或NSDictionary)设置为nil和[self.tableView reloadData]

答案 3 :(得分:1)

清除行是什么意思?你还想让他们在那里,但没有文字吗?如果是,请输入以下代码:

   UITableViewCell *cell;

    NSIndexPath *index;

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

如果要删除它们,可以使用以下代码:

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

答案 4 :(得分:1)

在重新加载表之前,从tableView数组中移除所有对象(填充tableView的数组),如下所示:

[myArray removeAllObjects];
[tableView reloadData];

答案 5 :(得分:0)

发生崩溃是因为如果在< - p>中计算行数,则需要重置数字

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

一种方法是使用如下的标志变量:

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

您还需要修改删除代码,如下所示:

    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];
你的.h文件中的

BOOL deleting;
NSInteger  rowcount;

希望这会有所帮助......

答案 6 :(得分:0)

rowcount = i应该在通话前进行。

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

否则,它会崩溃。

答案 7 :(得分:0)

tableView.dataSource = nil
tableView.reloadData()