【问题标题】:tableView convertPoint: fromView: odd behaviour and workaroundtableView convertPoint:fromView:奇怪的行为和解决方法
【发布时间】:2013-06-28 06:12:41
【问题描述】:

我有一个带有自定义单元格的 tableView。单元格中的对象之一是文本字段。 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 它应该是什么。 解决方案:在情节提要中,我将 textField 移动到单元格的上部。 有没有人见过这样的东西? 谢谢。

【问题讨论】:

    标签: ios uitableview uitextfield


    【解决方案1】:

    -[UIView center] 返回视图的frame 的中心,它位于视图的superview 的 坐标系中。但是-[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];
    

    【讨论】:

    • 谢谢库尔特!我想我迷失在风景中。
    【解决方案2】:
        - (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。我做对了吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多