2015-09-05 81 views
0

所以我要定製UITableViewCell選定背景視圖。在這裏我做了什麼:自定義高度的UITableViewCell選擇背景視圖

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    ... 

    CGRect cellFrame = [tableView rectForRowAtIndexPath:indexPath]; 
    CGRect selectedBackgroundFrame = cellFrame; 
    UIView *selectedBackgroundView = [[UIView alloc] init]; 

    selectedBackgroundFrame.size.height = 5; 
    [selectedBackgroundView setFrame:selectedBackgroundFrame]; 
    selectedBackgroundView.backgroundColor = [UIColor colorWithRed:180/255.0 
                green:138/255.0 
                blue:171/255.0 
                alpha:1]; 

    NSLog(@"%f %f %f %f", 
     selectedBackgroundFrame.size.width, 
     selectedBackgroundFrame.size.height, 
     selectedBackgroundFrame.origin.x, 
     selectedBackgroundFrame.origin.y); 
    cell.selectedBackgroundView = selectedBackgroundView; 

    return cell; 
} 

但結果是選擇背景視圖仍填充整個細胞,而不是我的預期是高度5.

它還打印正確的事情320.000000 5.000000 0.000000 0.000000。怎麼了?

+0

您的意思是,您想在選擇它之後增加單元高度嗎? –

+0

不,是selectedBackgroundView。編輯我的帖子.. – Rendy

+0

你可以嘗試打印'selectedBackgroundView.frame'。並且selectedBackgroundView的顏色顯示是否正常? – anhtu

回答

0

你可能錯過以下行

ViewDidLoad當插入新行,使高度一致:

self.tableView.estimatedRowHeight = kCellHeight; // Your custom height for cell 
self.tableView.rowHeight = UITableViewAutomaticDimension; 

而且也

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    return kCellHeight; 
} 
+0

我想要單元格的selectedBackgroundView,而不是單元格本身。 – Rendy

0

這可能是因爲一些地方的設置在視圖上的autoresizingMask。也許嘗試將其設置爲flexibleWidth而不是將flexibleHeight設置爲cellForRow

+0

抱歉,我無法理解你的。你能解釋更多嗎? – Rendy