[tableView 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?

可以帮助我的任何人如何在运行中使表视图无效(而不是通过滚动单元格:)!

提前感谢!

您需要使用UITableView方法-deleteRowsAtIndexPath。。。以执行您试图实现的删除。不过,苹果可能不会批准这种UI模式,因为它违反了HIG。

请考虑使用editing属性以及-commitEditingStyle。。。用于执行删除的UITableViewDatasource的。用户已经熟悉表格视图的这种用法,它不会有被苹果拒绝的风险。

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

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

仅包含[A计数][B计数]不。。事实上,有了reloadData,情况会更糟。无论如何,它的工作原理是向上滚动-向下滚动,而numberOfRowsInSection不会再次被查询。

感谢大家的支持!

最新更新