[tableView reloadData]的奇怪问题

时间:2011-06-24 14:20:38

标签: iphone uitableview invalidation reloaddata

让我们有两个可变数组,让我们有以下代码:

- (void)viewDidLoad
{
    NSMutableArray *A = [[NSMutableArray alloc] init];
    NSMutableArray *B = [[NSMutableArray alloc] init];
    // fill up A
    // B remains empty
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // ...
    if (...)
    {
        // fill up the cell with datas of an object from A
    }
    else
    {
        // fill up the cell with datas of an object from B
    }
    //...
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (...)
    {
        // remove an object from A and put it in B
    }
    else
    {
        // remove an object from B and put in A
    }
    [tableView reloadData];
}

如果[tableView reloadData]被注释掉(// [tableView reloadData]),那么在“点击”之后,表格将不会像预期的那样失效。并且如果当前单元格在屏幕上滚动并再次滚动,则调用“cellForRowAtIndexPath”并且单元格根据从哪个数组中获取其内容来“绘制”。 但是如果[tableView reloadData]处于活动状态,则“cellForRowAtIndexPath”根本不会被调用(!),并且该单元格将从屏幕上删除!

反应:WHUT?

可以帮助我任何人如何动态地使tableview无效(而不是通过滚动单元格和in:))!

提前致谢!

2 个答案:

答案 0 :(得分:0)

你需要使用UITableView method -deleteRowsAtIndexPath ...来执行你想要实现的删除。 Apple可能不会批准这种UI范例,因为它违反了HIG。

考虑使用编辑属性以及-commitEditingStyle... of the UITableViewDatasource进行删除。用户已经熟悉表视图的这种用法,并且不会被Apple拒绝。

答案 1 :(得分:0)

这是我的错......对我这件事来说,这是一次非常好的经历。 方法:

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

只包含 [A count] [B count] 而不是..实际上,对于reloadData,它更糟糕。无论如何它的工作原因是向上滚动 - 向下滚动,而 numberOfRowsInSection 不再被查询。

谢谢大家的支持!

相关问题