I have a table view placed in a navigation controller. Cells of the table view are smaller in width than the original and the background is an image. How can i set the "selected color"? here is my code so far:
- (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;
}
TableViewCell
s have a backgroundView
and a selectedBackgroundView
. Use these for the background and only put the labels and image views in the 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;
}
UITableViewCell是UiView的子类,因此您可以使用它上面的所有UIView类属性,如backgroundColor等。正如Madboy所提到的,它还具有backgroundView和selectedBackgroundView作为其属性,因此您可以使用这些属性来使其成为自定义单元格。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.