0

我有一个带有自定义文本视图的表格视图单元格,我现在想知道如何在编辑/添加文本后访问文本框中的文本。

当我通过 IB 绘制文本字段时,我通常会知道这是如何完成的,但我的 textviewcell 是动态绘制的。

(即我想捕获在detailLabel.text中更新的数据)

这是适应您的答案的相关代码。再次感谢!

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];


    //Big Text Box
    UITextView *detailLabel = [[UITextView alloc] initWithFrame:CGRectMake(30, 80, 500, 150)];
    detailLabel.tag = 20;
    [cell.contentView addSubview:detailLabel];
    detailLabel.layer.borderWidth = 1;
    detailLabel.layer.borderColor = [[UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:0.9] CGColor];
    detailLabel.layer.cornerRadius = 10;
    detailLabel.font = [UIFont fontWithName:@"Helvetica" size:17];
    [detailLabel release];

} 

UITextView * detailLabel = (UITextView *) [cell.contentView viewWithTag:20];

switch (indexPath.row) {

case 0:
    detailLabel.text = @"no";
    break;
default:
    detailLabel.text = [NSString stringWithFormat:@"%d", indexPath.row];
    detailLabel.hidden = NO;    

}
4

1 回答 1

3

你会想要监听 UITextView 发送的消息。所以看看实现UITextViewDelegate协议并使用它的委托属性将实现类注册到 detailLabel 中。

当您的委托被通知 UITextView 发生更改时,您必须识别当前拥有 UITextView 的单元格。在这样做时,您必须记住单元格可以重复使用。

我将从查看 UITableView 类中的以下方法开始:

- (NSArray *)visibleCells

我们知道,如果用户对 UITextView 的内容进行了更改,它当前必须在屏幕上,因此出现在前面提到的数组中。为了找到它,我们使用指向发生变化的 UITextView 的指针(它是 textViewDidChange 协议方法中的参数)。因此,只需遍历 visibleCells 数组并检索 UITextView 并将其与更改的 UITextView 进行比较。

- (void)textViewDidChange:(UITextView *)textView {
....
cellsLabel = (UITextView *) [cell.contentView viewWithTag:20];
if (cellsLabel == textView)
...

现在,您将拥有 UITableView 中单元格的句柄。要查找索引,请使用以下 UITableView 方法:

- (NSIndexPath *)indexPathForCell:(UITableViewCell *)cell
于 2010-02-22T13:00:35.067 回答