سؤال

I got a UIView inside a UITableViewCell (dynamic prototype, not sure if it's important to clarify that) with a background color and changed the view's frame with the following code (inside cellForRowAtIndexPath method):

UIView* verde = (UIView*) [cell viewWithTag:202];
verde.frame =CGRectMake(20, 30, x, y);

The problem is that when the UITableView is drawn for the first time (when the screen loads) the UIView has the original size (established by default on the original prototype from the storyboard). But when I scroll down, the cell leaves the screen, therefore reused by another cell. When scrolling back to the cell, cellForRowAtIndexPath is called again and now the frame has the correct size.

I've tried calling [verde setNeedsDisplay]; after changing the frame without success.

هل كانت مفيدة؟

المحلول

Disabling autolayout solved the issue as pointed out by Timothy Moose. Somehow the cells in the first draw (screen first load) retain the layout specified in the storyboard, when they leave screen and they are reused or created again the views are finally drawn with the correct frame.

نصائح أخرى

Try this , Hope you can resolve the issue

-(UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [theTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
    }
    else{
      [[cell.contentView viewWithTag: 202] removeFromSuperview];
    }

   UIView *view =[[UIView alloc]init];   
   view.frame = cell.contentView.frame;        
   view.tag = 202;

   view.backgroundColor = [UIColor redColor];//To be sure that the custom view in the cell  
    [cell addSubview:view];

    return cell;

}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top