1

我开始了我的 ios 开发,一开始我认为制作一个度量转换器会很有趣。

我设法用故事板创建了带有segues的表格视图,带有文本字段的自定义单元格和对应于不同尺寸的标签,但现在我卡住了,无法在我正在阅读的教程或书中找到答案。

在每个单元格中都有一个对应于给定尺寸(即米、厘米等)的文本字段。如何获取给定行中的文本字段已完成编辑的事件,并在计算后更改其他文本字段?(单元格是从包含计算所需的维度名称和值的数组创建的)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UILabel *label;
    UITextField *field;
    static NSString *CellIdentifier = @"DimensionCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    label = (UILabel *)[cell viewWithTag:1];
    field = (UITextField *)[cell viewWithTag:2];
    field.delegate = self;
    NSString *DimensionLabel = [self.dimension objectAtIndex:indexPath.row];
    label.text = DimensionLabel;
    return cell;
}
4

2 回答 2

3

您需要将 UITextField 的委托设置为呈现 tableView 的 viewController。

您可以给每个 textField 标记,也可以在textFieldDidEndEditing:委托中检查 textField 的点以找到用于识别 textField 的 indexPath。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier
                                                            forIndexPath:indexPath];

    cell.customTextField.delegate = self;
    //You can use tag if there is only one section
    //If there is more than one section then this will be ambiguos
    cell.customTextField.tag = indexPath.row;

    //Set other values

    return cell;
}

- (void)textFieldDidEndEditing:(UITextField *)textField{

    CGPoint textFieldOrigin = [textField convertPoint:textField.frame.origin toView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:textFieldOrigin];
    //Now you can use indexPath for updating your dataSource 

}
于 2013-06-15T15:13:50.627 回答
0

您可以使用restorationIdUITextField 的属性。像这样给出每个文本字段的 restoreID :

textField.restorationID = @"dimension1";
textField.delegate = self;

然后在下面的文本字段委托方法中,您可以检查 restoreID 以确定哪个文本字段被编辑。

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if([textField.restorationID isEqualToString:@"dimension1"])
    {
         // perform calculation and any updation required.
    }
}

我只是建议您使用您的维度名称作为 restoreID。这可以帮助您识别委托方法中的文本字段。

于 2013-06-15T15:26:52.293 回答