2

I have created a table which dynamically creates cells which are customized to contain a textField. When I run the program, I am able to enter text into the textFields. However, I am not able to collect the text entered into them before quitting the program/switching to a different viewController. Can you please suggest what I should do in order to extract the text entered by the user.

I understand that I can access the cells using the following code...

for (int section = 1; section < [self.tableView numberOfSections]; section++) // section 0: profile picture
{
    for(int row = 0; row < [self.tableView numberOfRowsInSection:section]; row++)
    {
        NSLog(@"section = %d, row = %d", section, row);
        NSIndexPath *tempIndexPath = [NSIndexPath indexPathForRow:row inSection:section];
        UITableViewCell *tempCell = [self tableView:self.tableView cellForRowAtIndexPath:tempIndexPath];
//            NSLog(@"tempCell = %@", tempCell);

    }
}

But I am not able to extract the text contained in them.

I also referred to: Accessing UITextField in a custom UITableViewCell. But I am looking for a cleaner solution.

Thanks!

4

3 回答 3

1

The link that you refer to is very close to what you need to do, however there is a better way to get the indexPath.

A common mis-conception when starting with iOS programming is that you need to get all of the values of the text fields at the time you need the data (such as when the user hits "Submit"). The problem, especially when they are in a table, is that the text fields are not always available. If the cell is off the screen, it quite possibly doesn't exist, or it has been reused in a different row of the table. Text fields are view's which are supposed to display data, and not act as your model where you store it.

So, the first thing that you need to do is to make your view controller conform to the UITextFieldDelegate protocol and set the delegate of the textfield when you create it to your view controller:

Your .h file (the <UITextFieldDelegate> is the important part):

@interface YourViewController : UIViewController <UITextFieldDelegate>

When you create your text field:

myNewTextfield.delegate = self;

This tells the text field to inform you of important changes to it. Now, you only need to create the text field delegate method which is called as soon as they finish editing the text field and wait for it to be called so that you can store the text:

- (void) textFieldDidEndEditing:(UITextField *)textField {
    // If you need the index path of the table view cell which contains the text field in order to know how to store it, use:
    CGRect position = [self convertRect:textField.frame toView:self.tableView];
    NSArray *indexPaths = [self.tableView indexPathsForRowsInRect:position];
    NSIndexPath *indexPath = [indexPaths objectAtIndex:0];

    // Save the contents of the text field somewhere so that you have it later when you need it:
    something = textField.text;
}
于 2012-12-24T05:34:40.007 回答
0

教程对我很有帮助。您可以通过标签引用您需要的任何对象。

在情节提要中拖动UIImageViewUITextField等并将标签设置为 100(无论您想要什么),然后在您- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath使用标签来引用它。

这是您可以做的事情,只需记住在情节提要中设置标签:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

// Configure the cell...
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

 UITextField *tField = (UITextField *)[cell viewWithTag:100];

return cell;
 }
于 2013-04-03T19:54:17.263 回答
0

这是我的 Swift 4 解决方案,因为我遇到了同样的问题。

我也将文本字段添加到单元格中,并且表格是动态创建的,因此表格视图控制器无法直接访问文本字段。

首先,我创建了一个协议,允许表格视图控制器保存单元格文本字段的输入值的数据。

protocol SMTextFieldRowCellProtocol: class {
    var dataValues: [String: String] { get set }
    func updateDataValues(with data: [String: String])
}

在单元格的自定义类中,添加以下委托属性:

var delegate: SMTextFieldRowCellProtocol?

仍然在单元格的类中,使其符合UITextFieldDelegate,将文本字段的委托设置为 self 在awakeFromNib().

textField.delegate = self

然后在单元格的类中,添加UITextFieldDelegate' 方法并使其调用该updateDataValues方法。我使我的键等于单元格的标题标签,所以所有键都是唯一的。

func textFieldDidEndEditing(_ textField: UITextField) {
    delegate?.updateDataValues(with: [titleLabel.text!: textField.text!])
}

上述方法将更新表视图控制器的字典。几乎完成了,但是每当您在表格视图的 cellForRow 方法中创建行时,请设置单元格的委托:

cell.delegate = self

最后,将协议添加到类并遵守它:

class SMEditProfileTVC: UITableViewController, SMTextFieldRowCellProtocol {
    var dataValues: [String: String] = [String: String]()
    func updateDataValues(with data: [String: String]) {
        for key in data.keys {
            dataValues[key] = data[key]
        }
    }
}

所以发生的事情是,当您完成对单元格文本的编辑时,它将调用表格视图控制器的updateDataValues方法并将数据保存到dataValue的字典中。现在,这将使在保存等时或在视图控制器中完成时传输数据变得容易。

于 2018-03-12T00:56:58.763 回答