gpt4 book ai didi

objective-c - 将 subview 添加到 UITableViewCell 的内容 View 使单元格无法选择

转载 作者:行者123 更新时间:2023-12-01 18:01:57 25 4
gpt4 key购买 nike

我想在 UITextView 中向 UITableViewCell 添加一些静态文本。

UITextView *addressField = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 300, 75)];
[addressField setBackgroundColor:[UIColor clearColor]];
[addressField setFont:[UIFont fontWithName:@"HelveticaNeue" size:14]];
[addressField setContentInset:UIEdgeInsetsMake(0, 20, 0, 0)];
[addressField setEditable:NO];
[addressField setScrollEnabled:NO];

// change me later
[addressField setText:@"John Doe\n555 Some Street\nSan Francisco, CA, 00000"];

[cell.contentView addSubview:addressField];

[addressField release];

这很好用,但我这段代码使单元格无法选择,可能是因为 UITextView 覆盖了整个单元格。

我该如何解决这个问题,以便我可以同时拥有 UITextView 和可选择的单元格?

顺便说一句,我可以使 UITextView 的大小更小一些,但如果用户触摸 UITextView,他们仍然无法选择单元格。

最佳答案

我认为更好的方法是在整个 table 上创建一个点击手势识别器。 (例如在您的 viewDidLoad 中)

// gesture recognizer to make the entire cell a touch target
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(changeFocus:)];
[tableView addGestureRecognizer:tap];
[tap release];

然后创建一个选择器(在本例中为 changeFocus:)来进行实际选择。
- (void)changeFocus:(UITapGestureRecognizer *)tap
{
if (tap.state == UIGestureRecognizerStateEnded)
{
CGPoint tapLocation = [tap locationInView:self.tableView];
NSIndexPath* path = [self.tableView indexPathForRowAtPoint:tapLocation];
[self tableView:self.tableView didSelectRowAtIndexPath:path];
}
}

您可以使您的 changeFocus 方法更加精细,以防止选择或将焦点放在所选 indexPath 的特定 subview 上。

关于objective-c - 将 subview 添加到 UITableViewCell 的内容 View 使单元格无法选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8266350/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com