ios - UITableViewCell - 无法让 UIButton subview 在 iOS 9 中重叠(并可点击)下一个单元格

标签 ios objective-c uitableview ios9 subview

我正在尝试将 UIButton subview 添加到 UITableViewCell 并将其重叠到下面的单元格上。具体来说,我希望我的实现看起来像 iOS 联系人中的编辑联系人 View ,左侧是联系人照片(按钮),右侧是名字和姓氏。

这曾经在 iOS 7 中工作,我很确定它也在 iOS 8 中工作。我已经尝试了所有我能想到的让它在 iOS 9 中工作。我可以让按钮重叠到下一个单元格中,但它不可点击。无论如何,当我单击按钮的下半部分(第二个单元格的重叠部分中的一半)时,按钮无法识别(没有任何 react )。这是我尝试过的所有事情,但均未成功。

当我创建我设置的 tableView

tableView.clipsToBounds = NO;


- (void)tableView:(UITableView *)tableView1 willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

    switch (indexPath.section) {
        case CONTACT_EDIT_SECTION: {
            switch (indexPath.row) {
                case NAME_FIRST_ROW: case NAME_LAST_ROW:
                    [cell setClipsToBounds:NO];
                    [cell.contentView setClipsToBounds:NO];
                    [cell.contentView.superview setClipsToBounds:NO];
                    [cell.contentView.superview.superview setClipsToBounds:NO];
                }
                    break;
            }
        }
            break;
    }
}

我读到当 tableView 处于编辑模式时,单元格被设置回 cell.contentView.clipsToBounds = YES。所以
- (void)setEditing:(BOOL)editing {

    [super setEditing:editing];
    UITableView *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:NAME_FIRST_ROW inSection:CONTACT_EDIT_SECTION]];
    cell.contentView.clipsToBounds = NO;

    UITableView *cell2 = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:NAME_LAST_ROW inSection:CONTACT_EDIT_SECTION]];
    cell2.contentView.clipsToBounds = NO;
}

显然,我尝试在 cellForRowAtIndexPath 方法中设置 clipsToBounds No:
- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    __ENTERING_METHOD__
    switch (indexPath.section) {
        case CONTACT_EDIT_SECTION: {
            switch (indexPath.row) {
                case NAME_FIRST_ROW: case NAME_LAST_ROW: {
                    UITableViewCell *cell;
                    if (indexPath.row == 0) {
                        cell = [tableView1 dequeueReusableCellWithIdentifier:@"Value1Cell" forIndexPath:indexPath];
                        UIButton *imageButton = [[UIButton alloc] initWithFrame:CGRectMake(LEFT_CELL_MARGIN, (2*DEFAULT_ROW_HEIGHT-IMAGE_DIAMETER)/2.0, IMAGE_DIAMETER, IMAGE_DIAMETER)];
                        [imageButton setBackgroundColor:[UIColor redColor]];
                        //[imageButton.layer setMasksToBounds:YES];
                        [imageButton.layer setCornerRadius:IMAGE_DIAMETER/2.0];
                        [imageButton.layer setBorderWidth:1];
                        [imageButton.layer setBorderColor:[UIColor colorWithRed:200 green:200 blue:200 alpha:1].CGColor];
                        [imageButton setImage:_person.thumbnailImage forState:UIControlStateNormal];
                        [imageButton addTarget:self action:@selector(photoActionSheet:) forControlEvents:UIControlEventTouchUpInside];
                        [cell.contentView.superview addSubview:imageButton];

                        [cell setClipsToBounds:NO];
                        [cell.contentView setClipsToBounds:NO];
                        [cell.contentView.superview setClipsToBounds:NO];
                        [cell.contentView.superview.superview setClipsToBounds:NO];

                    }
                    else {
                        cell = [tableView1 dequeueReusableCellWithIdentifier:@"Value1Cell" forIndexPath:indexPath];

                        [cell setClipsToBounds:NO];
                        [cell.contentView setClipsToBounds:NO];
                        [cell.contentView.superview setClipsToBounds:NO];
                        [cell.contentView.superview.superview setClipsToBounds:NO];
                    }
                    [cell layoutIfNeeded];
                    return cell;
                }
                    break;
            }
        }
            break;
    }
    return nil;
}

通常在谷歌搜索四个小时并尝试所有可能找到的东西后,我可以解决这样的问题。帮助将不胜感激。

最佳答案

您需要创建一个包含多个文本字段和肖像图像的单元格。从外部单元格重叠是非常棘手的,因为用户可以通过滚动单元格来改变 View 顺序。

您可以在联系人应用程序中确认这一点,当您选择电话号码单元格或以下任何内容时,它将突出显示该单元格,但是当点击第一个/最后一个/公司单元格时,它不会突出显示。这是因为它本身就是一个细胞。

关于ios - UITableViewCell - 无法让 UIButton subview 在 iOS 9 中重叠(并可点击)下一个单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34056475/

相关文章:

uitableview - 可能到 "reuse"AVPlayerLayer?从 superview 中删除单元重用会影响我的性能

ios - 添加交叉溶解效果 - iOS 视频创作

ios - 将 iOS 框架作为文件或 pod 分发

ios - 是否可以在节标题下方添加刷新控件

ios - 变量的类型不完整 'QPrinter'

ios - 在 UITableView 中显示一个表中的中型数据集

ios - iPhone : UITextView not expanding while editing

ios - UISegmentedControl 未接收到触摸

ios - objc_setAssociatedObject 函数错误在 64 位模式下不在 32 位

ios - NSUserDefault 常量未更新