【问题标题】:How to insert the value in cell in tableview at particular indexpath?如何在特定索引路径的表格视图中插入单元格中的值?
【发布时间】:2012-06-11 12:38:27
【问题描述】:

我在 tableview 中有一个标签和文本字段,现在我想要的是我在 textfieldDidEditing 方法的文本字段中插入的任何值,它应该计算该值并将其返回到 tableviewww 的索引路径中的标签。

【问题讨论】:

  • 到目前为止你尝试过什么?
  • 在编辑文本字段时是否获取单元格的索引路径?

标签: iphone ios5 uitableview xcode4.2


【解决方案1】:

在textfieldDidEditing中,可以获取对应textfield的单元格,计算value赋值给label,如下:

yourTableViewCell *cell = (yourTableViewCell *)[[textField superview] superview];
cell.yourLabel.text = value;

【讨论】:

  • 我有一个单元格,其中我有子视图标签,我无法调用 cell.labelname.text
  • 无论层次结构如何,如果它是单元格的子视图,您可以从文本字段中获取单元格。您可以使用上述方法(超级视图)获取单元格,您只需要识别视图的层次结构.
【解决方案2】:
/* u just give Tag to TextField and in textFieldDidEndEditing, save textfield text into an string and then reload the row...here is the code..also have sample */


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexpath
{
    UITableViewCell *cell = [[UITableViewCell alloc] init];

    UITextField *txtField1 = [[UITextField alloc]initWithFrame:CGRectMake(20, 10, 100, 80)];
    txtField1.tag = indexpath.row;
    txtField1.delegate= self;
    [cell addSubview:txtField1];

    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(140, 10, 100, 80)];

    [cell addSubview:label1];

    //set text in label ** str1 comes from textFieldDidEndEditing
    label1.text = str1;

    return cell;
}

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    //save textField text to any string**declair it global
    str1 = textField.text;

    //reload selected row in table
    NSIndexPath *durPath = [NSIndexPath indexPathForRow:textField.tag inSection:0];
    NSArray *paths = [NSArray arrayWithObject:durPath];
    [tbl reloadRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationRight];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-21
    • 1970-01-01
    • 1970-01-01
    • 2011-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多