Curnelious
Curnelious

Reputation: 1

UITableView wrong cell size before scrolling?

I have this UITableView, made in code ,and when i see it first time, the cells size that i read are wrong-hence the icons on the cells calculated wrong and are very small.

Than , when i start scrolling,every cell i scroll through ,its icons (on this cell),becomes bigger and get their right size,and i see also the small icons too, so i have for each cell a small icon and a big icon,where i should only have the big.

Why is that happens ? (this view also has some collection view inside it)

      //tabel view
        frm.origin.y=self.frame.size.height-openY;
        tableView = [[UITableView alloc]initWithFrame:frm style:UITableViewStylePlain];
        tableView.delegate=self;
        tableView.dataSource=self;
        [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
        [self  addSubview:tableView];

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  [actionsMenus count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
     return self.frame.size.height/5.0;
}

- (UITableViewCell *)tableView:(UITableView *)ttableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     static NSString *simpleTableIdentifier = @"Cell";
    UITableViewCell *tcell=  [ttableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    NSLog(@"%f", tcell.frame.size.height );

    if (tcell == nil)
    {
        tcell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

     NSString *kind=[actionsMenus objectAtIndex:indexPath.row];
     NSString *icon=[NSString stringWithFormat:@"%@%d.png",kind,colorIndex];

    //icon
    UIImage *image=[UIImage imageNamed:icon];
    UIImageView *view=[[UIImageView alloc] initWithFrame:CGRectMake(tcell.frame.size.width/2.0-0.8*tcell.frame.size.height/2.0, 0, 0.8*tcell.frame.size.height,0.8*tcell.frame.size.height)];
    view.image=image;
    [tcell addSubview:view];

    return tcell;
}

Upvotes: 1

Views: 759

Answers (1)

Lyndsey Scott
Lyndsey Scott

Reputation: 37290

If you're creating the table in viewDidLoad, I think the UITableView delegate methods are being called before the view's auto-layout is complete; so setting the heightForRowAtIndexPath: to

return self.frame.size.height/5.0;

uses the frame of the view pre-auto-layout to calculate the row height. If you absolutely need heightForRowAtIndexPath: to be dependent on the view's height though, perhaps add the table as a subview after view's layout is complete. For example, instead of adding it in your viewDidLoad, add it in viewDidLayoutSubviews, ex:

- (void) viewDidLayoutSubviews {
    //table view
    frm.origin.y=self.frame.size.height-openY;
    tableView = [[UITableView alloc]initWithFrame:frm style:UITableViewStylePlain];
    tableView.delegate=self;
    tableView.dataSource=self;
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
    [self addSubview:tableView];
}

Upvotes: 2

Related Questions