2011-03-15 106 views
2

我想按下QTY按钮(红色文本)并将文本(即13)复制到同一行中的文本域。帮助! uitableviewcell按钮更新文本域

here is my sample uitableviewcell

-(IBAction)qtyButtonPressed:(id)sender { 

UITextField *textField = (UITextField *)[self.view viewWithTag:3]; 

textField.text = @"13"; 

这是我有个大气压。

+0

如果您发布相关代码,您可能会得到更多答案。 – sarnold 2011-03-15 23:53:05

回答

1

如果每个单元格都有一个按钮,首先您需要能够识别哪个按钮从哪个行被点击。通常,如果它与1节表格,你可以设置的行号内的cellForRowAtIndexPath按钮标记值:...设置在小区可见

[button setTag:indexPath.row]; 

然后在选择调用的时候,按钮压制,取得标记值来确定行号,并设置文本框的文本行

int row = [sender tag]; 
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row section:0]; 
    id cell = [tableView cellForRowAtIndexPath:indexPath]; 
    [cell.textField setText:....]; 

在对于这个工作,你需要继承一个UITableViewCell,使按钮和文本框财产/合成访问。

+0

非常感谢honcheng! – johnstontrav 2011-03-16 01:53:11

0

您可以在按钮上使用addTarget:action:forControlEvents:并使用UIControlEventTouchUpInside来注册一个选择器,该按钮被触摸时会被调用。然后在该方法中找到相应的文本字段并分配其text属性。

+0

感谢Anomie,我已经这样做了,但我很难“找到”相应的文本字段。有什么帮助吗? – johnstontrav 2011-03-16 00:21:12

1

我知道这已被回答,但我有一个类似的问题,不幸的是我已经使用标签来查找表视图单元格内的字段,让我把它放在InterfaceBuilder/Xcode中,并仍然避免这样的子类化:

- (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath { 
    static NSString *AttributeCellIdentifier = @"AttributeCell"; 
    UITableViewCell *cell; 
    UILabel *label; 
    UITextField *value; 
    MyAttribute *a; 

    switch(indexPath.section) { 
     case ATTRIBUTES_SECTION: 
      cell = [tableView dequeueReusableCellWithIdentifier: AttributeCellIdentifier]; 
      label = (UILabel *) [cell viewWithTag: 1]; 
      value = (UITextField *) [cell viewWithTag: 2]; 
      a = [attributeList objectAtIndex: indexPath.row]; 
      label.text = a.label; 
      value.text = a.value; 
      break; 
     // Other sections... 
    } 
    return cell; 
} 

但这意味着我不能使用标签的行的文本框是那么作为替代使用标签我使用文本字段的坐标,看看排它是这样的:

- (void) textFieldDidEndEditing: (UITextField *) textField { 
    NSLog(@"Entering %s with %@", __func__, textField); 
    NSIndexPath *textFieldLocation = [self.tableView indexPathForRowAtPoint: [textField convertPoint:textField.bounds.origin toView: self.tableView]]; 
    NSLog(@"- The textfield is in the cell at: %@", textFieldLocation); 

    if(textFieldLocation.section == ATTRIBUTES_SECTION) { 
     MyAttribute *a = [attributeList objectAtIndex: textFieldLocation.row]; 
     a.value = textField.text; 
    } 
} 

如果我有多个文本字段w在单元格中,我仍然可以使用标记值来知道哪一个结束编辑。

它甚至可能是明智的建立,返回的tableview指数为任一视图的小帮手方法:

- (NSIndexPath *) indexPathForView: (UIView *) view { 
    NSIndexPath *loc = [self.tableView indexPathForRowAtPoint: [view convertPoint: view.bounds.origin toView: self.tableView]]; 
    return loc; 
} 

这可以在一个类别提出和容易获得任何的tableview而不需要编写任何代码。