繁体   English   中英

UITableView滚动前单元格大小错误?

[英]UITableView wrong cell size before scrolling?

我有这个用代码编写的UITableView ,当我第一次看到它时,我读取的单元格大小是错误的-因此,单元格上的图标计算错误并且非常小。

比起当我开始滚动时,我滚动浏览每个单元格时,其图标(在此单元格上)会变大并获得正确的尺寸,我也看到了小图标,因此每个单元格都有一个小图标和一个大图标图标,我应该只有大的地方。

为什么会这样呢? (此视图内部还具有一些集合视图)

      //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;
}

如果要在viewDidLoad创建表,我认为UITableView委托方法是在视图的自动布局完成之前调用的; 因此将heightForRowAtIndexPath:设置为

return self.frame.size.height/5.0;

使用自动布局视图的框架来计算行高。 但是,如果绝对需要heightForRowAtIndexPath:依赖于视图的高度,则可以视图的布局完成后将表添加为子视图。 例如,而不是你将它添加viewDidLoad ,将其添加在viewDidLayoutSubviews ,例如:

- (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];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM