2014-12-07 45 views
1

我有這個UITableView,用代碼編寫,當我第一次看到它的時候,我讀的單元大小是錯誤的 - 因此單元格上的圖標計算錯誤並且非常小。當我開始滾動時,我滾動的每個單元格,它的圖標(在這個單元格上)變得更大並且尺寸合適,而且我也看到了小圖標,所以我對每個單元格都有一個小的圖標和一個大圖標,我應該只有大的圖標。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; 
} 

回答

2

如果要創建在viewDidLoad表,我覺得被稱爲UITableView委託方法前視圖的自動佈局完成;所以設置heightForRowAtIndexPath:

return self.frame.size.height/5.0; 

使用視圖的幀 -auto佈局來計算行高。如果你絕對需要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]; 
} 
+0

感謝,這是原因之一。 – Curnelious 2014-12-07 17:41:06