2015-09-05 85 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