UITableViewCell - 无法让 UIButton 子视图与 iOS 中的下一个单元格重叠(并可点击)9
UITableViewCell - can't get UIButton subview to overlap (and be tappable in) next cell in iOS 9
我正在尝试将 UIButton 子视图添加到 UITableViewCell 并使其与下面的单元格重叠。具体来说,我希望我的实现看起来像 iOS 联系人中的编辑联系人视图,左侧是联系人照片(按钮),右侧是名字和姓氏。
这曾经在 iOS 7 中有效,我很确定它在 iOS 8 中也有效。我已经尝试了所有我能想到的方法来使它在 iOS 9. 我可以让按钮重叠到下一个单元格中,但它不可点击。无论如何,当我点击按钮的下半部分(第二个单元格重叠部分的那一半)时,按钮无法识别(没有任何反应)。以下是我尝试过的所有方法,但均未成功。
当我创建 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;
}
通常在谷歌搜索四个小时并尝试我能找到的一切之后,我可以解决这样的问题。将不胜感激。
您需要创建一个包含多个文本字段和其中的肖像图像的单元格。从外部单元格重叠是非常棘手的,因为视图顺序可以通过用户滚动进出单元格来更改。
您可以在联系人应用程序中确认这一点,当您 select 一个 phone 数字单元格或下面的任何内容时,它会突出显示该单元格,但是当点击 first/last/company 单元格时,它没有突出显示。这是因为它本身就是一次细胞。
我正在尝试将 UIButton 子视图添加到 UITableViewCell 并使其与下面的单元格重叠。具体来说,我希望我的实现看起来像 iOS 联系人中的编辑联系人视图,左侧是联系人照片(按钮),右侧是名字和姓氏。
这曾经在 iOS 7 中有效,我很确定它在 iOS 8 中也有效。我已经尝试了所有我能想到的方法来使它在 iOS 9. 我可以让按钮重叠到下一个单元格中,但它不可点击。无论如何,当我点击按钮的下半部分(第二个单元格重叠部分的那一半)时,按钮无法识别(没有任何反应)。以下是我尝试过的所有方法,但均未成功。
当我创建 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;
}
通常在谷歌搜索四个小时并尝试我能找到的一切之后,我可以解决这样的问题。将不胜感激。
您需要创建一个包含多个文本字段和其中的肖像图像的单元格。从外部单元格重叠是非常棘手的,因为视图顺序可以通过用户滚动进出单元格来更改。
您可以在联系人应用程序中确认这一点,当您 select 一个 phone 数字单元格或下面的任何内容时,它会突出显示该单元格,但是当点击 first/last/company 单元格时,它没有突出显示。这是因为它本身就是一次细胞。