简体   繁体   English

使用UITableView中的文本字段

[英]Working with text fields in a UITableView

I am making a search form with a number of free text fields that I would like the user to edit in line on a tableview (and not go to a seperate view for each individual item). 我正在制作一个包含许多自由文本字段的搜索表单,我希望用户在表格视图上进行行内编辑(而不是为每个单独的项目进入单独的视图)。 So far I have it so when you press on the field, a text input dialog appears, here is the relevant code: 到目前为止,我已经知道了,因此当您按该字段时,将出现一个文本输入对话框,这是相关代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

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

        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 130, 25)];
        label.textAlignment = UITextAlignmentRight;
        label.tag = kLabelTag;
        [cell.contentView addSubview:label];
        [label release];

        UITextField *theTextField = [[UITextField alloc] initWithFrame:CGRectMake(150, 10, 140, 25)];
        theTextField.tag = kTextFieldTag;
        theTextField.delegate = self;
        [cell.contentView addSubview:theTextField];
        [theTextField release];

This has the desired effect of allowing a user to press a row and get an edit dialog, but I have a few problems: 这具有允许用户按下一行并获得编辑对话框的预期效果,但是我有一些问题:

1) You can see I have set the text field's delegate to equal self. 1)您可以看到我已将文本字段的委托设置为等于self。 The class this is run in implements UITextFieldDelegate yet textFieldShouldEndEditing is not being fired. 运行该类的类实现UITextFieldDelegate,但不会触发textFieldShouldEndEditing

2)How can i realisticly persist what the user puts in memory. 2)我如何才能现实地坚持用户存储在内存中的内容。 In other words, is it possible to find out which text field the user has selected currently so i can go [myArray getObjectAtIndex:blah] and edit the actual data. 换句话说,是否有可能找出用户当前选择的文本字段,以便我可以[myArray getObjectAtIndex:blah]并编辑实际数据。

Thanks 谢谢

1) Are the other delegate methods being called? 1)是否调用了其他委托方法?

2) You can for example set the tag attribute on each text field to identify them, and use that as an index into the array. 2)例如,您可以在每个文本字段上设置tag属性以标识它们,并将其用作数组的索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM