【问题标题】:iOS UITextView doesn't allow UITableViewCell to be selectediOS UITextView 不允许选择 UITableViewCell
【发布时间】:2014-05-06 14:20:43
【问题描述】:

我有一个 UITableViewCell,在其中,我有一个带有一些文本的 UITextView,它必须保持可滚动状态,因此我无法删除其中的用户交互。

但问题是,当我点击屏幕时,这个单元格也必须是可选择的。由于 UITextView 位于单元格触摸区域的前面,我无法做到这一点。

我尝试将 UITapGestureRecognizer 放入单元格类内的 UITextView 中,但我不知道调用哪个方法来选择单元格,以主类中的 UITableView 委托 (didSelectRowAtIndexPath) 结束。

有没有人可以帮我解决这个问题,或者有其他想法我可以如何解决这个问题?

提前非常感谢!

//CUSTOM CELL CODE

-(void)awakeFromNib
{
    self.backgroundColor = [UIColor clearColor];

    self.title.font = [UIFont fontWithName:FONT_MEDIUM size:14];
    self.title.textColor = [UIColor colorWithRed:181/255.0 green:181/255.0 blue:179/255.0 alpha:1];
    self.text.font = [UIFont fontWithName:FONT_LIGHT_ITA size:13];
    self.text.textColor = [UIColor colorWithRed:236/255.0 green:220/255.0 blue:0/255.0 alpha:1];

    UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(touchAction:)];
    [self.text addGestureRecognizer:tap];

}

-(void)touchAction:(UITapGestureRecognizer*)sender{
    NSLog(@"Touch");
}

-(void)setSelected:(BOOL)selected animated:(BOOL)animated
{

    [super setSelected:selected animated:animated];

}

【问题讨论】:

    标签: ios objective-c xcode uitableview


    【解决方案1】:

    你试过这种方式吗?

    在不禁用textView的用户交互设置UITextView的delegate的情况下,像这样实现UITextViewDelegate的textViewShouldBeginEditing方法

    - (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
        [self.tableView selectRowAtIndexPath:yourCellIndexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
        return NO;
    }
    

    documentation 表示“当用户执行通常会启动编辑会话的操作时,文本视图首先调用此方法以查看是否应该实际进行编辑。”

    【讨论】:

    • 也不使用 UITapGestureReconzzer
    • 到目前为止它正在工作,但问题是 selectRowAtIndexPath 方法不会触发 didSelectRowAtIndexPath 委托。我仍在试图弄清楚。一旦我发现我会回到这里。非常感谢您的回答!
    • 因为 selectRowAtIndexPath 不会触发委托方法,我称自己为 [self tableView:self.table didSelectRowAtIndexPath:indexPath]; 并且它有效!我不知道这是否是正确的方法,但已解决... 非常感谢您的帮助!
    • 是的,没错! selectRow 只选择行,自己调用没有错。我很乐意提供帮助)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-26
    • 1970-01-01
    • 2012-12-18
    • 1970-01-01
    • 1970-01-01
    • 2015-05-08
    相关资源
    最近更新 更多