3

I have a tableView with custom cells. One of the objects in the cell is a textField. The tableViewController is the textFieldDelegate. The datasource for the table is an array of objects. When the user tap on the textField, the tableViewController implements textField delegates methods controlling this way the data entry.
To use the data entered by user, the tableViewController implements the following:

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
  {
    CGPoint point = [textField center];
    point = [self.tableView convertPoint:point fromView:textField];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:point];
... 
...
    return YES
  }

I spent two hours trying to figure out why indexPath.row was always wrong, it was always row+1 of what it suppose to be. Solution: In the storyboard I move the textField to the upper portion of the cell. Has anyone see something like this before? Thanks.

rmaddy
  • 314,917
  • 42
  • 532
  • 579

2 Answers2

6

-[UIView center] returns the center of the view's frame, which is in the view's superview's coordinate system. But -[UIView convertPoint:fromView:] expects to be given a point in the "from" view's coordinate system.

Either give it the same point, and tell it to convert from the correct view:

CGPoint point = [textField center];
point = [self.tableView convertPoint:point fromView:textField.superview];

Or give it a point in the view's coordinate system, and tell it to convert from the view:

CGRect textFieldBounds = textField.bounds;
CGPoint point = CGPointMake(CGRectGetMidX(textFieldBounds), CGRectGetMidY(textFieldBounds));
point = [self.tableView convertPoint:point fromView:textField];
Kurt Revis
  • 27,695
  • 5
  • 68
  • 74
1
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {

            NSString *text = [array_loaddata objectAtIndex:indexPath.row] ;
            UIFont *font = [UIFont systemFontOfSize:10];
            CGSize size = [(text ? text : @"") sizeWithFont:font constrainedToSize:CGSizeMake(220, 9999) lineBreakMode:NSLineBreakByWordWrapping];

            int count = size.height + 0;

            return count;

    }


    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 1;
    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return [array_loaddata count];
    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSString *CellIdentifier =[NSString stringWithFormat:@"Cell%d",indexPath.row] ;

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];

            NSString *text = [array_loaddata objectAtIndex:indexPath.row] ;
            UIFont *font = [UIFont systemFontOfSize:10];
            CGSize size = [(text ? text : @"") sizeWithFont:font constrainedToSize:CGSizeMake(220, 9999) lineBreakMode:NSLineBreakByWordWrapping];
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(46, 0, size.width, size.height)];
            label.numberOfLines = 0;
            label.textColor = [UIColor grayColor];
            label.lineBreakMode = NSLineBreakByWordWrapping;
            label.text = (text ? text : @"");
            label.font = font;
            label.backgroundColor = [UIColor clearColor];
            [cell.contentView addSubview:label];
            [label release];


        }
        return cell;
    }
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
link==http://mobile.tutsplus.com/tutorials/iphone/iphone-json-twitter-api/
        /*
    NSString *appurl =[NSString stringWithFormat:@"xx"];
    appurl = [appurl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:appurl]];
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse: nil error: nil ];
    NSString  *returnString = [[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding];
*/

    }
2014
  • 119
  • 1
  • 1
  • 13
  • I see. You propose to use textField.tag to remember the correct row and set an action to respond to when textField exit. So, instead of override - (BOOL)textFieldDidEndEditing:(UITextField *)textField, you use the selector "testFieldDone:". Did I got it right? – Williams_Martinez Jun 28 '13 at 15:03