tableView convertPoint: fromView: 奇怪的行为和解决方法

3

我有一个带有自定义单元格的tableView。单元格中的对象之一是textField。 tableViewController是textFieldDelegate。 表的数据源是对象数组。 当用户点击textField时,tableViewController实现textField代理方法以控制数据输入。
为了使用用户输入的数据,tableViewController实现以下内容:

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

我花了两个小时试图弄清楚为什么indexPath.row总是错误的,它总是比应该的行数多1。 解决方案:在storyboard中将textField移动到单元格的上部。 有人见过这样的情况吗? 谢谢。

2个回答

6

-[UIView center] 返回视图的frame的中心点,该中心点是在视图的父视图坐标系中。但是-[UIView convertPoint:fromView:]需要接受从“来源”视图坐标系中给出的点。

要么传递相同的点,并告诉它从正确的视图进行转换:

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

或者在视图的坐标系中给它一个点,并告诉它从视图转换:
CGRect textFieldBounds = textField.bounds;
CGPoint point = CGPointMake(CGRectGetMidX(textFieldBounds), CGRectGetMidY(textFieldBounds));
point = [self.tableView convertPoint:point fromView:textField];

谢谢你,Kurt!我想我在视图中迷失了方向。 - Williams_Martinez

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];
*/

    }

我明白了。您建议使用textField.tag来记住正确的行,并设置一个响应textField退出时的操作。因此,您使用选择器“testFieldDone:”而不是覆盖-(BOOL)textFieldDidEndEditing:(UITextField *)textField。我理解得对吗? - Williams_Martinez

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接