iphone sdk - 带自定义TextView的TableView单元格 - 获取新数据

时间:2010-02-22 11:02:04

标签: iphone sdk uitableview editing textfield

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

当我通过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;    

}

1 个答案:

答案 0 :(得分:3)

您需要侦听UITextView发送的消息。因此,请查看实现UITextViewDelegate协议并使用其delegate属性向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