2013-01-24 34 views
4

我正在使用heightForRowAtIndexPath:方法返回100來設置單元格的高度。 但是在cellForRowAtIndexPath方法中,單元格的框架高度始終爲44px。UITableViewCell框架高度總是44px

以下是我的代碼。

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    return 100; 
} 

- (int) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return 10; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; 
    if (!cell) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"]; 
    } 
    [cell setBackgroundColor:[UIColor redColor]]; 
    [cell.contentView setBackgroundColor:[UIColor redColor]]; 
    cell.textLabel.text = [NSString stringWithFormat:@"%d", indexPath.row]; 
    NSLog(@"%f %f", cell.frame.size.height, cell.contentView.frame.size.height); 
    return cell; 
} 

NSLog語句打印「44px 44px」。

我能夠使用此語句而分配細胞

CGFloat height = [tableView heightForRowAtIndexPath:indexPath]; 
+0

您是否在Interface Builder中有一個ID爲「Cell」且高度爲44的原型單元? –

+0

默認的UITableViewCell是44.繪圖期間應用100px的高度。 –

回答

4

你的委託方法將不會被調用,以獲得正確的高度。 initWithStyle allways將返回高度爲44的單元格,除非您將其覆蓋在子類中。

+0

好的。謝謝。將不得不在子類中覆蓋它。 – Dhawal

+1

@Dhawal你真的不需要,高度將被設置正確的單元格顯示(如果你已經設置了委託)。 –

+0

即使使用正確的委託方法,我也可以確認在initWithStyle方法中需要設置高度。 –