tableView convertPoint: fromView: odd behaviour and workaround

和自甴很熟 提交于 2019-12-01 11:04:15

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

    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!