我在导航控制器中放置了一个表格视图。表格视图的单元格宽度小于原始单元格,背景是图像。如何设置“选定颜色”?这是我目前的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"ApplicationCell";
ApplicationCell *cell = (ApplicationCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[CompositeSubviewBasedApplicationCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
return cell;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
{
cellContentView = [[CompositeSubviewBasedApplicationCellContentView alloc] initWithFrame:CGRectInset(self.contentView.bounds, 0.0, 1.0) cell:self];
cellContentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
cellContentView.contentMode = UIViewContentModeRedraw;
//here i'm making the cells smaller in width than the rest of the tableView
CGRect framme = cellContentView.frame;
framme.size.width = framme.size.width-58;
//set the left space
framme.origin.x = framme.origin.x+29;
[cellContentView setFrame:framme];
[self.contentView addSubview:cellContentView];
}
return self;
}
答案 0 :(得分:2)
TableViewCell
有一个backgroundView
和一个selectedBackgroundView
。将这些用于背景,仅将标签和图片视图放在contentView
。
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
{
UIView* backgroundView = [[UIView alloc] initWithFrame:self.bounds];
UIView* visibleBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(29.0f, 0.0f, backgroundView.bounds.size.width - 58.0f, backgroundView.bounds.size.height)];
// configure the visibleBackgroundView with the color you want for unselected cell here
[backgroundView addSubview:visibleBackgroundView];
self.backgroundView = backgroundView;
UIView* selectedBackgroundView = [[UIView alloc] initWithFrame:self.bounds];
UIView* visibleSelectedBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(29.0f, 0.0f, backgroundView.bounds.size.width - 58.0f, backgroundView.bounds.size.height)];
// configure the visibleSelectedBackgroundView with the color you want for selection here
[selectedBackgroundView addSubview:visibleSelectedBackgroundView];
self.selectedBackgroundView = selectedBackgroundView;
// configure your content view with all the labels you need here
}
return self;
}
答案 1 :(得分:1)
UITableViewCell是UiView的子类,因此您可以使用其上的所有UIView类属性,如backgroundColor等。 正如Madboy所提到的那样,它还将backgroundView和selectedBackgroundView作为其属性,因此您可以使用这些属性来使其成为自定义单元格。