从表格单元格访问数据
我正在创建一个表单,想知道是否有人知道如何从表格单元格中检索 UITextField 值。
- (IBAction)saveForm:(id)sender {
NSLog(@"TextField Value => %@", titleField.text);
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
switch(indexPath.section)
{
case 0:
if (row == 0) {
cell.selectionStyle = UITableViewCellSelectionStyleNone;
CGRect titleFieldFrame = CGRectMake(20.0, 80.0, 280.0, 25.0);
UITextField *titleField = [[UITextField alloc] initWithFrame:titleFieldFrame];
[titleField setBorderStyle:UITextBorderStyleRoundedRect];
[titleField setTextColor:[UIColor blackColor]];
[titleField setFont:[UIFont systemFontOfSize:16]];
[titleField setPlaceholder:@"Title"];
[titleField setBackgroundColor:[UIColor whiteColor]];
titleField.keyboardType = UIKeyboardTypeDefault;
titleField.returnKeyType = UIReturnKeyDone;
[cell addSubview:titleField];
}
break;
return cell;
}
I'm creating a form and was wondering if anyone knows how to retrieve a UITextField value from a table cell.
- (IBAction)saveForm:(id)sender {
NSLog(@"TextField Value => %@", titleField.text);
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
switch(indexPath.section)
{
case 0:
if (row == 0) {
cell.selectionStyle = UITableViewCellSelectionStyleNone;
CGRect titleFieldFrame = CGRectMake(20.0, 80.0, 280.0, 25.0);
UITextField *titleField = [[UITextField alloc] initWithFrame:titleFieldFrame];
[titleField setBorderStyle:UITextBorderStyleRoundedRect];
[titleField setTextColor:[UIColor blackColor]];
[titleField setFont:[UIFont systemFontOfSize:16]];
[titleField setPlaceholder:@"Title"];
[titleField setBackgroundColor:[UIColor whiteColor]];
titleField.keyboardType = UIKeyboardTypeDefault;
titleField.returnKeyType = UIReturnKeyDone;
[cell addSubview:titleField];
}
break;
return cell;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以简单地创建一个实例变量并将 titleField 分配给它。或者,您可以使用
[titleField setTag:123]
分配标签,然后通过[yourTableView viewWithTag:123]
检索它。You could simply create an instance variable and assign titleField to that. Or you could assign a tag with
[titleField setTag:123]
and then later retrieve it via[yourTableView viewWithTag:123]
.给 titleField 一个特殊的标签,例如
,然后使用
-viewWithTag:
来获取该视图。Give a special tag to the titleField, e.g.
and then use
-viewWithTag:
to get that view.