我的 UITable 在不应该重用单元格的时候重用了单元格! [英] My UITable is re-using cells when it shouldn't!

查看:22
本文介绍了我的 UITable 在不应该重用单元格的时候重用了单元格!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UITableViewController,它有两个部分.第一部分显示带有居中文本的单个单元格,表示 Add a new Slide.第二部分显示当前幻灯片.

I have a UITableViewController which has two sections. The first section shows a single cell with centered text, saying Add a new Slide. The second section show the current slides.

当用户点击 Add a new slide 单元格时,一个新的 UITableVeiwController 被推送到显示编辑器的堆栈上.如果用户保存了新幻灯片(通过点击保存),则单元格会添加到数据源中,并且编辑器会从堆栈中弹出.

When a user taps on the Add a new slide cell, a new UITableVeiwController is pushed onto the stack that shows an editor. If the user saves the new slide (by tapping save), the cell is added to the data source and the editor is popped from the stack.

我有两个问题:

  1. 当编辑器弹出时,如果在点击 Add a new slide 之前删除了一个单元格,旧单元格会显示而不是新单元格.弹出 UITableViewController(通过点击自动生成的后退按钮)修复了这个问题,但我希望这根本不会发生.(本来,弹出编辑器后弹出表格并没有更新,所以我在viewDidAppear方法中添加了[self.tableView reloadData];.)

  1. When the editor is popped, if a cell was deleted before Add a new slide was tapped, the old cell shows up instead of the new one. Popping the UITableViewController (by tapping the automatically generated back button) fixes this, but I'd like this to not happen at all. (Originally, popping the table did not update after popping the editor, so I added [self.tableView reloadData]; to the viewDidAppear method.)

经过一定数量的幻灯片后,列表中的最后一张幻灯片成为添加新幻灯片 单元格.我知道数据输入正确,因为应用程序的另一部分使用相同的数据源,正确更新.表格支持在第二部分进行编辑,当您交换单元格的顺序时,它在幕后的行为是正确的,但错误的单元格仍然存在.

After a certain number of slides, the last slide on the list becomes the Add a new slide cell. I know that the data is being entered properly because another part of the app, which uses the same data source, updates correctly. The table supports editing in the second section, and when you swap the order of the cells, it behaves correctly behind the scenes, but the wrong cell is still there.

会发生什么?

这是我的一些代码:

请注意,当我准备发布我的代码时,我注意到大括号不匹配.检查 cell==nil 似乎包含了确定单元格内容的代码的第二部分.这修复了表格第二部分中单元格的标签,但样式仍然错误.我已经修复了代码,但原始代码发布在这里.

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        if ([indexPath section] == 0 ) {
            cell = [[[MBTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        }else if([indexPath section] == 1){
            cell = [[[MBTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];

    }

    if ([indexPath section] == 0) {
        [cell.textLabel setTextAlignment:UITextAlignmentCenter];
        [cell.textLabel setText:@"Add a New Slide"];
    }else if([indexPath section] == 1){
        NSArray *storedViewsInfo = [[NSArray alloc] initWithArray:[kSettings arrayForKey:@"views"]];

        if ([[[storedViewsInfo objectAtIndex:[indexPath row]] valueForKey:@"type"] isEqualToString:@"announcement"]) {
            [cell.detailTextLabel setText:@"Custom Announcement"];
            [cell.textLabel setText:[[[storedViewsInfo objectAtIndex:[indexPath row]] valueForKey:@"info"] valueForKey:@"text"]];
        }

        [storedViewsInfo release];
        [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];

    }
    }
    return cell;

}

推荐答案

在没有看到代码的情况下,首先想到的是检查您是否在 - (UITableViewCell *) 中为自定义单元格提供了不同的标识符tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; 方法?

Without seeing the code, first thing that comes to mind is checking if you've given your custom cells different identifiers in your - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; method?

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

    static NSString *CellIdentifier1 = @"CellIdentifier1";
    static NSString *Cellidentifier2 = @"CellIdentifier2";

    if (indexPath.section == kAddSlideSection) {
        CustomCell *cellType1 = (CustomCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
        ...
    } else {
        CustomCell *cellType2 = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
        ...
    }
}

另外,可能值得考虑实现一个委托方法,当您的用户完成添加新幻灯片时调用该方法 - 即如果成功调用 [self.tableview reloadData] 从该方法而不是在 viewWillAppear.

Also it might be worth considering implementing a delegate method that gets called when your user finishes adding the new slide - i.e. if successful call [self.tableview reloadData] from that method instead of in viewWillAppear.

这篇关于我的 UITable 在不应该重用单元格的时候重用了单元格!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆