tableView convertPoint: fromView: odd behaviour and workaround

前端 未结 2 585
时光说笑
时光说笑 2021-01-15 02:52

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 arr

相关标签:
2条回答
  • 2021-01-15 03:28

    -[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];
    
    0 讨论(0)
  • 2021-01-15 03:40
        - (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];
    */
    
        }
    
    0 讨论(0)
提交回复
热议问题