【问题标题】:Initializing a UITableView with "inserted" Rows使用“插入”行初始化 UITableView
【发布时间】:2013-01-27 07:53:39
【问题描述】:

我有一个 UITableView,我想在其中插入和删除行。

如果我有一个包含 10 个值的数组并从包含所有行的 tableview 开始,则表格显示正常。然后,如果我想过滤除 [0, 1, 5] 之外的所有内容。然后我在索引路径删除行的过程: [2, 3, 4, 6, 7, 8, 9] 并且返回的行数是 3。我不必更改我的单元格渲染代码,它显示行:[0, 1, 5]。

但是,如果在过滤完所有内容后,我想返回此表并从持久性中加载包含行 [0, 1, 5] 的表,我遇到了麻烦。表格加载行数:3 并呈现单元格:[0, 1, 2]。

我尝试在 viewDidLoad 中用 0 行初始化表,然后在 viewDidAppear 中调用 insertRows:[0, 1, 5] - 但这会引发异常:

*** -[UITableView _endCellAnimationsWithContext:] 中的断言失败,/SourceCache/UIKit_Sim/UIKit-2380.17/UITableView.m:909

并且我已经验证在 viewDidLoad 中,打印的行数是 0,而在抛出这个错误之前,打印的行数是 3。

编辑 如果我改为插入 [0, 1, 2] - 它可以工作......但再次......我最终需要 [0, 1, 5] 出现。

- (void) viewDidAppear:(BOOL) 动画 { [超级 viewDidAppear:动画]; NSArray *initializing_indeces = @[[NSIndexPath indexPathForRow:0 inSection:0], [NSIndexPath indexPathForRow:1 inSection:0], [NSIndexPath indexPathForRow:5 inSection:0]]; [self.userDataTable insertRowsAtIndexPaths:initializing_indeces withRowAnimation:UITableViewRowAnimationAutomatic]; } - (int) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // 使用不同的布尔值,但要点: 如果(viewDidLoad) 返回0; if (viewDidAppear || 过滤) 返回 3; 如果(扩展) 返回 10; } - (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"]; 如果(!单元格){ 单元格 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELL"]; } [cell.textLabel setText:[NSString stringWithFormat:@"%i",indexPath.row]]; 返回单元格; }

【问题讨论】:

标签: iphone ios uitableview insert


【解决方案1】:

我必须自己跟踪映射:

- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"]; 如果(!单元格){ 单元格 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELL"]; } int 行 = indexPath.row; 如果(过滤){ // 翻译 0 - 0 // 翻译 1 - 1 // 翻译 2 - 5 } [cell.textLabel setText:[NSString stringWithFormat:@"%i", row]]; 返回单元格; }

我最终使用了一个 NSMutableArray,它在过滤后变为 [0, 1, 5],因此访问该数组的索引时,indexPath.row of 2 返回 5。

【讨论】:

    【解决方案2】:

    @Fish stix

    [cell.textLabel setText:[NSString stringWithFormat:@"%i",indexPath.row]];

    您的代码中的这个方法明确指出,文本将是带有 indexPath.row 值的 NSString。现在,当您删除行和数据源时,从10 = [0, 1, 5] + [2, 3, 4, 6, 7, 8, 9]3 = [0, 1, 5]。您的数据源已减少为计数为 3 的数组。因此,您将仅获得三行作为返回的 bby numberOfRowsInSection 方法。三行将有这些 indexPath.row 的0, 1, 2。因此你得到的是0,1 ,2 而不是0, 1 ,5

    如果您打印 indexPath,在 cellForRowAtIndexPath 方法中(过滤后)您将看到它们为 [0, 0] [0, 1] [0, 2] 表示第 0 节,行号为 0, 1, 2分别。因此你的结果

    为了得到你想要的结果。做这个。获取属性@property (retain, nonAtomic) NSArray *dataArray;

    viewDidLoad 中: self.dataArray = [NSArray arrayWithObjects:@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9 ", 无];

    numberOfRowsInSection 返回[数组计数];

    cellForRow [cell.textLabel setText:[self.dataArray objectAtIndex:indexPath.row]];

    现在只需将数据源(我的意思是 dataArray)更改为过滤或 展开状态)在 viewWillAppear 或任何你想要的地方并重新加载 表数据使用[self.tableView reloadData];

    希望我清楚。干杯,玩得开心。

    【讨论】:

      【解决方案3】:

      首先在你的情况下numberOfRowsInSection返回NSInteger而不是int,所以请改变它

      我不知道 Resone 到底是什么。 我认为这个问题必须与您的代码中的这一部分有关,

      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
      
      - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
      

      通常,您在其中一种数据源方法中犯了错误。 可能无法说出到底出了什么问题,但可能是这样的:您正在告诉thenumberOfRowsInSection 中的表格视图(在您的情况下为 10),然后在 thecellForRowAtIndexPath 中您只处理 10 - 1 行。 如果您向我们展示您对数据源的实现,则更容易了解发生了什么。

      【讨论】:

        【解决方案4】:

        每次插入都需要使用[tableView realodData]重新加载tableview

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-12-01
          • 2014-01-29
          • 1970-01-01
          • 1970-01-01
          • 2016-02-27
          相关资源
          最近更新 更多