开发者

HELP! uitableviewcell buttonpressed update textfield

I want to press the QTY button (red text) and copy the text (ie 13) into the textfield in the same row.

HELP! uitableviewcell buttonpressed update textfield

-(IBAction)qtyButtonPressed:(id)sender {

UITextField *textField = (UITextFie开发者_JAVA百科ld *)[self.view viewWithTag:3];

textField.text = @"13";

this is what i have atm.


If every cell has a button, first you need to be able to identify which button from which row was clicked. Usually if it is a table with 1 section, you can just set the row number as the button tag value inside cellForRowAtIndexPath:... set when the cell is visible

[button setTag:indexPath.row];

Then in the selector called when the button is pressed, get the tag value to determine row number, and set text for textfield in that row

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

For this to work, you need to subclass a UITableViewCell, and make button and textField accessible with property/synthesize.


I know this has been answered but I had a similar problem and unfortunately I had used tags to find the fields within the table view cell, to allow me to lay it out in InterfaceBuilder/Xcode and still avoid subclassing like this:

- (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;
}

but this means I cannot use the tags for the row that the textfield is in. So as an alternative to using tags I used a coordinate from the text field to see what row it is in like this:

- (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;
    }
}

If I have more than one text field within the cell I can still use the tag value to know which one it is that is ending editing.

It might even be smart to build a little helper method that returns the tableview index for any view:

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

This could be put in a category and be easily available for any tableview without any coding needed.


You would use addTarget:action:forControlEvents: on the button with the UIControlEventTouchUpInside to register a selector that will be called when the button is touched. Then in that method, find the corresponding text field and assign its text property.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜