单元格显示在节标题的顶部 [英] Cell shows on top of section header

查看:77
本文介绍了单元格显示在节标题的顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有自定义单元格和自定义标题的 UITableView 。当我在编辑时移动一个单元格时,它会弹出标题视图。
如何将标题视图保留在所有单元格的顶部?

I have an UITableView with custom cells and custom headers. When I move one cell upon editing, it pops up on to of the header view. How can I keep the header view on top of all the cells?

该应用程序使用故事板,以防万一。

The app uses storyboard, in case that makes a difference.

这是它的样子吗? https://www.dropbox.com/s/wg8oiar0d9oytux/iOS%20SimulatorScreenSnapz003.mov

这是我的代码:

[...]
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ListCell";
    ListCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    int handleSection = [self sectionToHandle:indexPath.section];

    switch (handleSection)
    {
        case PrivateLists:
        {
            if (tableView.isEditing && (indexPath.row == self.privateLists.count))
            {
                cell.textField.text = NSLocalizedString(@"Lägg till ny lista", nil);
                cell.textField.enabled = NO;
                cell.textField.userInteractionEnabled = NO;
                cell.accessoryType = UITableViewCellAccessoryNone;
                cell.editingAccessoryView.hidden = YES;
            }
            else
            {
                List *list = [self.privateLists objectAtIndex:indexPath.row];
                cell.textField.text = list.name;
                cell.textField.enabled = YES;
                cell.textField.userInteractionEnabled =YES;
                cell.backgroundColor = [UIColor clearColor];

                cell.onTextEntered = ^(NSString* enteredString){
                    list.name = enteredString;
                    UpdateListService *service = [[UpdateListService alloc]initServiceWithList:list];
                    [service updatelistOnCompletion:
                     ^(BOOL success){
                         DLog(@"Updated list");

                         NSIndexPath *newPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];
                         [self.tableView moveRowAtIndexPath:indexPath toIndexPath:newPath];

                         [self moveListToTop:list.ListId newIndexPath:newPath];
                         justMovedWithoutSectionUpdate = YES;
                     }
                                                    onError:
                     ^(NSError *error){
                         [[ActivityIndicator sharedInstance] hide];
                         [[ErrorHandler sharedInstance]handleError:error fromSender:self];
                     }];
                };
            }
        }
            break;
        default:
            return 0;
            break;
    }

    return cell;
}

-(UIView *)tableView:(UITableView *)aTableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 22)];

    UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 300, 21)];
    [textLabel setFont:[[AXThemeManager sharedTheme]headerFontWithSize:15.0]];
    [textLabel setTextColor:[[AXThemeManager sharedTheme]highlightColor]];
    [textLabel setText:@"SECTION TITLE"];
    [textLabel setBackgroundColor:[UIColor clearColor]];

    UIImageView *backgroundView = [[UIImageView alloc]initWithImage:[AXThemeManager sharedTheme].tableviewSectionHeaderBackgroundImage];
    [backgroundView setFrame:view.frame];
    [view addSubview:backgroundView];
    [view sendSubviewToBack:backgroundView];
    [view addSubview:textLabel];

    return view;
}

- (float)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 22;
}

- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}
[...]


推荐答案

好消息!我能够以两种不同的方式修复/解决您的问题(见下文)。

Good news! I was able to fix/workaround your problem in two different ways (see below).

我想说这肯定是一个操作系统错误。你正在做什么导致你移动的单元格(使用 moveRowAtIndexPath:)放在z顺序中的标题单元格的上方(前面)。

I would say this is certainly an OS bug. What you are doing causes the cell you have moved (using moveRowAtIndexPath:) to be placed above (in front of) the header cell in the z-order.

我能够在操作系统5和6中重现问题,其中包含已经和没有UITextFields的单元格,以及tableView进出编辑模式(在你的视频中)处于编辑模式,我注意到了)。即使您使用的是标准版块标题,也会发生这种情况。

I was able to repro the problem in OS 5 and 6, with cells that did and didn't have UITextFields, and with the tableView in and out of edit mode (in your video it is in edit mode, I noticed). It also happens even if you are using standard section headers.

保罗,您在其中一条评论中说道:

Paul, you say in one of your comments:


我使用加载器严重解决了这个问题并锁定了表格,同时
预先形成了reloadData

I solved it badly using a loader and "locking" the table while preforming a reloadData

我不确定使用加载程序并锁定表是什么意思,但我确定在<之后调用 reloadData code> moveRowAtIndexPath:确实解决了问题。这不是你想要做的吗?

I am not sure what you mean by "using a loader and locking the table", but I did determine that calling reloadData after moveRowAtIndexPath: does fix the problem. Is that not something you want to do?

[self.tableView moveRowAtIndexPath:indexPath toIndexPath:newPath];
//[self.tableView reloadData];
// per reply by Umka, below, reloadSections works and is lighter than reloadData:
[self reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationNone];

如果你不想这样做,这是另一个让我觉得有点hacky的解决方案,但是似乎运作良好(iOS 5 +):

If you dont want to do that, here is another solution that feels a little hacky to me, but seems to work well (iOS 5+):

__weak UITableViewCell* blockCell = cell;  // so we can refer to cell in the block below without a retain loop warning.
...
cell.onTextEntered = ^(NSString* sText)
{
    // move item in my model
    NSIndexPath *newPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];
    [self.itemNames removeObjectAtIndex:indexPath.row];
    [self.itemNames insertObject:sText atIndex:0];

    // Then you can move cell to back
    [self.tableView moveRowAtIndexPath:indexPath toIndexPath:newPath];
    [self.tableView sendSubviewToBack:blockCell];  // a little hacky

    // OR per @Lombax, move header to front
    UIView *sectionView = [self.tableView headerViewForSection:indexPath.section];
    [self.tableView bringSubviewToFront:sectionView];

这篇关于单元格显示在节标题的顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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