iphone sdk - 带有自定义 TextView 的 TableView Cell - 获取新数据

发布于 2024-08-22 06:13:02 字数 1204 浏览 5 评论 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;    

}

I have a tableview cell with a custom textview in it, I am now left wondering how do I possibly access the text in the textbox after text has been edited/added.

I would normally know how this is done when I draw the textfield through IB, but my textviewcell is dynamically drawn.

(i.e. I would like to capture the data that is updated in detailLabel.text)

Here is the related code to adapt to your answer.
Thanks again!

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;    

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情仇皆在手 2024-08-29 06:13:02

您需要监听 UITextView 发送的消息。因此,希望实现 UITextViewDelegate 协议并使用其 委托 属性。

当您的委托收到 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

You'll want to listen for messages sent by the UITextView. So look to implement the UITextViewDelegate protocol and register the implementing class with the detailLabel using its delegate property.

When your delegate is notified of a change in the UITextView you'll have to identify the cell which currently owns the UITextView. In doing so you must remember that cells can be reused.

I would start by looking at the following method in the UITableView class:

- (NSArray *)visibleCells

We know that if the user has made a change to the UITextView's contents it must currently be on screen and therefore be present in the pre-mentioned array. To find it we use the pointer to the UITextView which changed (it's a parameter in the textViewDidChange protocol method). So simply iterate over the visibleCells array and retrieve the UITextView and compare it against the UITextView which changed.

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

You'll now have a handle to a cell in the UITableView. To find the index use the following UITableView method:

- (NSIndexPath *)indexPathForCell:(UITableViewCell *)cell
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文