簡體   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