Objective c 如何引用驻留在自定义UITableViewCell中的UITextView

Objective c 如何引用驻留在自定义UITableViewCell中的UITextView,objective-c,cocoa-touch,uitableview,swift,Objective C,Cocoa Touch,Uitableview,Swift,我现在要做的是将对textView的引用存储在cellForRowAtIndexPath方法的变量中: func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("photoCell") as EditProfileTa

我现在要做的是将对textView的引用存储在cellForRowAtIndexPath方法的变量中:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("photoCell") as EditProfileTableViewCell

    cell.imageCaption.tag = indexPath.row
    imageCaptionTextView = cell.imageCaption

    imageCaptionTextView.delegate = self

    return cell

}
然后,当我点击键盘上的“完成”按钮时,我会尝试访问文本视图,当我在其中单击时会显示该按钮

func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {

    checkMaxLength(textView, maxLength: 60)

    if text == "\n" {

        imageCaptionTextView.resignFirstResponder()
        return false
    }
    return true
}
我有一些其他代码没有包含在这篇文章中,它只是在textView周围添加了一个边框,以向用户指示textView已被点击。这似乎只适用于第一个文本视图。我还向textView添加了一个标记,并打印到控制台,但该标记返回相同的数字

很明显,我似乎只提到了第一个单元格。我无法使用didSelectRow等,因为此单元格包含一张照片,然后是一个文本视图,用于添加该图像的标题

有什么简单的方法可以引用我的单元格吗?我已经看过了关于堆栈溢出的例子,但它们没有包括足够广泛的例子作为答案

如果能帮上忙,我将不胜感激

谢谢您的时间。

假设您的UITextView作为子视图添加到单元格的contentView中,则您可以通过执行以下操作来访问封闭的UITableViewCell:

UITableViewCell* cell = (id)textField.superview.superview;
或:


这取决于contentView是UITableViewCell的子视图这一假设,在iOS 8之前(包括iOS 8)都是如此,但将来可能不再如此。因此,您可以在那里添加一些检查,例如,使用isKindOfClass以确保获得正确的对象类型。

我认为您可以做的是首先获得选定的单元格。 然后,您可以使用标签访问它

NSIndexPath *selectedCellIndex = [self.tableView indexPathForSelectedRow];
UITableViewCell *selectedCell = [self.tableView cellForRowAtIndexPath:selectedCellIndex];
然后,你得到你的目标细胞。因此,您可以使用标记获取UITextView

以下是有关UIResponder链的链接:

UITextViewDelegate具有textViewDidBeginEditing和TextViewDiEndediting的函数


这不起作用,因为我必须实际选择单元格。点击我的UITextView不能做到这一点,你能试着调试这个问题吗?检查从selectedCellIndex获得的内容,因为UITextView也是从UIResponder继承的。触摸将被传递。我相信UITableView能够检测到这一点。我认为这不是一个新问题。
NSIndexPath *selectedCellIndex = [self.tableView indexPathForSelectedRow];
UITableViewCell *selectedCell = [self.tableView cellForRowAtIndexPath:selectedCellIndex];
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("photoCell") as EditProfileTableViewCell

    cell.imageCaption.tag = indexPath.row
    cell.imageCaption.delegate = self

    return cell
}

func textViewDidBeginEditing(_ textView: UITextView)
{
    textView. // add border here
}

func textViewDidEndEditing(_ textView: UITextView)
{
   textView.text // will give you final value
   textView.tag will give you the row of the cell.
}

func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {

    checkMaxLength(textView, maxLength: 60)

    if text == "\n" {

        textView.resignFirstResponder()
        return false
    }
    return true
}