iphone sdk - 带有自定义 TextView 的 TableView Cell - 获取新数据
我有一个带有自定义文本视图的表格视图单元格,我现在想知道在编辑/添加文本后如何访问文本框中的文本。
当我通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要监听 UITextView 发送的消息。因此,希望实现 UITextViewDelegate 协议并使用其 委托 属性。
当您的委托收到 UITextView 更改的通知时,您必须识别当前拥有 UITextView 的单元格。这样做时,您必须记住单元可以重复使用。
我首先查看 UITableView 类中的以下方法:
我们知道,如果用户对 UITextView 的内容进行了更改,则它当前必须位于屏幕上,因此存在于前面提到的数组中。为了找到它,我们使用指向发生更改的 UITextView 的指针(它是 textViewDidChange 协议方法中的一个参数)。因此,只需迭代visibleCells数组并检索UITextView并将其与已更改的UITextView进行比较即可。
您现在将拥有 UITableView 中单元格的句柄。要查找索引,请使用以下 UITableView 方法:
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:
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.
You'll now have a handle to a cell in the UITableView. To find the index use the following UITableView method: