I'm trying to change the cell background image when user tap on a cell (highlighted state), I've been trying this way but it's not working:
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor whiteColor];
cell.detailTextLabel.textColor = [UIColor whiteColor];
cell.textLabel.textAlignment = NSTextAlignmentRight;
cell.detailTextLabel.textAlignment = NSTextAlignmentRight;
cell.textLabel.font = [UIFont fontWithName:kFONT_NAME size:kFONT_SIZE];
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"row320x63.png"] highlightedImage:[UIImage imageNamed:#"row320x63_pressed.png"]];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"row320x63.png"]];
cell.textLabel.text = [self.listOfMenuSettings objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:#"settings_icon_%d", indexPath.row]];
UIImageView *pressed = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"row320x63_pressed.png"]];
[cell setSelectedBackgroundView:pressed];
return cell;
}
What I'm missing?
I don't find setSelectedBackgroundView: instance method in apple documentation. But there is a property selectedBackgroundView.So try:
cell.selectedBackgroundView = pressed;
Related
I'm trying to add labels (right and left side) on the tableView. However, label texts are cut, I wonder how to solve. I am adding my source code as well as attached the screen-capture.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
cell.imageView.image = [UIImage imageNamed:#"placeholder.png"];
cell.textLabel.text = [[dataArray objectAtIndex:indexPath.row] objectForKey:#"name"];
cell.detailTextLabel.text= [NSString stringWithFormat:#"$ %#", [[dataArray objectAtIndex:indexPath.row] objectForKey:#"price"]];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 70.0;
}
you can do this in two ways
one you need to change the textalignment to right, for e.g
cell.textLabel.textAlignment = NSTextAlignmentRight;
cell.detailTextLabel.textAlignment = NSTextAlignmentRight;
or change your UITableViewCell style as Right Detail in attribute inspector in your XCode
ir change your UITableViewCellStyleDefault to UITableViewCellStyleValue1 or UITableViewCellStyleValue2
you can get the style property in UITableViewCell class
and call like
if (cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:cellIdentifier];
cell.imageView.image = [UIImage imageNamed:#"placeholder.png"];
cell.textLabel.text = [dataArray objectAtIndex:indexPath.row];
cell.detailTextLabel.text= #"Yesterday";
for more information you can get the sample here else create the Custom cell and set the label in right alignment based on your needs
Change the following line of code:
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
Try this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
cell.imageView.image = [UIImage imageNamed:#"placeholder.png"];
cell.textLabel.text = [dataArray objectAtIndex:indexPath.row];
cell.detailTextLabel.text= #"Yesterday";
return cell;
}
Hope it helps !
You can wrap your label by using NSLineBreakByCharWrapping
Go through this code here myLabelWidth is ur label width and myLabelHeight is your label height
CGSize size = [textLabel.text sizeWithAttributes:
#{NSFontAttributeName:
[UIFont systemFontOfSize:17.0f]}];
int sizeofL=size.width;
if(sizeofL>(mylabelWidth))
{
[textLabel setMinimumFontSize:5.0];
[textLabel setNumberOfLines:0];
int hgtofL=size.height;
if(hgtofL>mylabelHieght)
{
if(height<1024)
textLabel.font = [UIFont systemFontOfSize:12];
else
textLabel.font = [UIFont systemFontOfSize:14];
}
else
{
if(height<1024)
textLabel.font = [UIFont systemFontOfSize:16];
else
textLabel.font = [UIFont systemFontOfSize:18];
}
textLabel.lineBreakMode = NSLineBreakByCharWrapping;
[textLabel sizeToFit];
}
else
{
if(height<1024)
textLabel.font = [UIFont systemFontOfSize:16];
else
textLabel.font = [UIFont systemFontOfSize:18];
}
cell.separatorInset = UIEdgeInsetsMake(0, 10000, 0, 0);
[cell.contentView addSubview:textLabel];
[cell setEditing:NO];
}
It should be like this
- (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
}
cell.imageView.image = [UIImage imageNamed:#"placeholder.png"];
cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
cell.textLabel.text = [dataArray objectAtIndex:indexPath.row];
cell.textLabel.textAlignment = NSTextAlignmentLeft;
cell.textLabel.font=[UIFont fontWithName:#"Arial" size:12];
cell.textLabel.adjustsFontSizeToFitWidth = YES;
cell.detailTextLabel.text= #"Yesterday";
cell.detailTextLabel.textAlignment = NSTextAlignmentRight;
cell.detailTextLabel.font=[UIFont fontWithName:#"Arial" size:12];//If you want decrease font size here
cell.detailTextLabel.adjustsFontSizeToFitWidth = YES;
return cell;
}
Hi i am beginner in Ios and in my project i have added some labels and image on table-list cell ok they have added so for everything is ok
Here my main requirement is when we tapped on table-list cell labels colors must be change and image need to be change as like below image
for this i have written some code but that's not working pleas help me
my code:-
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *cells=#"cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cells];
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cells];
}
strip = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 7, cell.frame.size.height)];
strip.backgroundColor = [UIColor orangeColor];
[cell.contentView addSubview:strip];
TitleLbl = [[UILabel alloc] initWithFrame:CGRectMake(65, 7, 130, 35)];
TitleLbl.text =[Right_Menu_array objectAtIndex:indexPath.row];
TitleLbl.textColor=[UIColor darkGrayColor];
TitleLbl.font = [UIFont systemFontOfSize:15];
[cell.contentView addSubview:TitleLbl];
img = [[UIImageView alloc] initWithFrame:CGRectMake(10, 7, 35, 35)];
img.image = [UIImage imageNamed:[imageArr1 objectAtIndex:indexPath.row]];
[cell.contentView addSubview:img];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
TitleLbl.textColor = [UIColor redColor];
strip.backgroundColor = [UIColor redColr];
img.image = [UIImage imageNamed:#"rahul.png"];
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
TitleLbl.textColor = [UIColor darkGrayColor];
strip.backgroundColor = [UIColor whiteColor];
img.image = [UIImage imageNamed:#"rahul.png"];
}
The easy way would be to create a custom UITableViewCell and then in the delegate functions of UITableView get the instance of CustomTableViewCell and set the Labels textColor.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath{
CustomTableViewCell *cell =(CustomTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
cell.TitleLbl.textColor = [UIColor redColor];
cell.strip.backgroundColor = [UIColor redColr];
cell.img.image = [UIImage imageNamed:#"rahul.png"];
}
PS: there are many other work arounds for this...it depends on how do you want to implement the code.
Other way,
//set flag variable as
#property (strong ,nonatomic) NSIndexPath *selectedIndexPath;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
self.selectedIndexPath = indexPath;
[tableView reloadData];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *cells=#"cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cells];
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cells];
}
strip = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 7, cell.frame.size.height)];
strip.backgroundColor = [UIColor orangeColor];
TitleLbl = [[UILabel alloc] initWithFrame:CGRectMake(65, 7, 130, 35)];
TitleLbl.text =[Right_Menu_array objectAtIndex:indexPath.row];
TitleLbl.textColor=[UIColor darkGrayColor];
TitleLbl.font = [UIFont systemFontOfSize:15];
img = [[UIImageView alloc] initWithFrame:CGRectMake(10, 7, 35, 35)];
img.image = [UIImage imageNamed:[imageArr1 objectAtIndex:indexPath.row]];
if(indexPath == selectedIndexPath)
{
TitleLbl.textColor = [UIColor redColor];
strip.backgroundColor = [UIColor redColr];
img.image = [UIImage imageNamed:#"rahul.png"];
}
[cell.contentView addSubview:strip];
[cell.contentView addSubview:TitleLbl];
[cell.contentView addSubview:img];
}
//This code is not tested.
I suppose you are referring to the tableview header.
You can only set tableview header view to your own custom view using the tableHeaderView property of the tableview. You can set the background color of your custom header view normally.
E.g.
self.tableView.tableHeaderView = myHeaderView;
Alternatively, if you want to modify the section header view, you can use one of the following delegate methods:
tableView:viewForHeaderInSection:
dequeueReusableHeaderFooterViewWithIdentifier:
and return your custom header view there.
From the code, I can see you hold the reference for properties of last known strip, TitleLbl and img, which will always be the components from last UITableViewCell accessed from
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
Since your cell has an orange strip on left side, I would prefer creating a custom UITableViewCell to hold the references.
#interface CustomCell : UITableViewCell
#property (nonatomic,strong) UIView *strip;
#property (nonatomic,strong) UILabel *titleLbl;
#property (nonatomic,strong) UIImageView *imgView;
#end
then with a custom cell, your code can be
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *cellIdentifier = #"cellIdentifier";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
// create and add components
cell.strip = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 7, cell.frame.size.height)];
[cell.contentView addSubview:cell.strip];
cell.titleLbl = [[UILabel alloc] initWithFrame:CGRectMake(65, 7, 130, 35)];
[cell.contentView addSubview:cell.titleLbl];
cell.imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 7, 35, 35)];
[cell.contentView addSubview:cell.imgView];
}
// modify existing components
cell.strip.backgroundColor = [UIColor orangeColor];
cell.titleLbl.text = [Right_Menu_array objectAtIndex:indexPath.row];
cell.titleLbl.textColor = [UIColor darkGrayColor];
cell.titleLbl.font = [UIFont systemFontOfSize:15];
cell.imgView.image = [UIImage imageNamed:[imageArr1 objectAtIndex:indexPath.row]];
if ([cell isSelected]) {
// remember to keep the changes for cell in it's selected state
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
CustomCell *cell = (CustomCell *) [tableView cellForRowAtIndexPath:indexPath];
cell.titleLbl.textColor = [UIColor redColor];
cell.strip.backgroundColor = [UIColor redColor];
cell.imgView.image = [UIImage imageNamed:#"rahul.png"];
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = (CustomCell *) [tableView cellForRowAtIndexPath:indexPath];
cell.titleLbl.textColor = [UIColor darkGrayColor];
cell.strip.backgroundColor = [UIColor whiteColor];
cell.imgView.image = [UIImage imageNamed:#"rahul.png"];
}
also you can override the method in your custom cell
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
I have two tableviews in a menu controller. first tableview populates a dynamic menu list from db and second tableview should only display the strings I tell it. So right now I only need 2 cells, Settings and Login. The first table view works fine. But, the second is not displaying the items. code bellow represent the second tableview
ViewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad];
[self.slidingViewController setAnchorRightRevealAmount:280.0f];
self.slidingViewController.underLeftWidthLayout = ECFullWidth;
self.view.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f];
self.tableView.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f];
self.tableView.separatorColor = [UIColor colorWithWhite:0.15f alpha:0.2f];
self.extraTableView.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f];
self.extraTableView.separatorColor = [UIColor colorWithWhite:0.15f alpha:0.2f];
}
Main table
-(void)setMenuItems:(NSArray *)menuItems
{
if(_menuItems != menuItems)
{
_menuItems = menuItems;
}
[self.tableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectionIndex
{
return self.menuItems.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = #"MenuItemCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
Department *dept = [self.menuItems objectAtIndex:indexPath.row];
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.textLabel.numberOfLines = 0;
cell.textLabel.text = dept.name;
cell.textLabel.textColor = [UIColor whiteColor];
cell.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f];
UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
myBackView.backgroundColor = [UIColor redColor];
cell.selectedBackgroundView = myBackView;
return cell;
}
Second table
-(void)setExtraMenuItems:(NSArray *)extraMenuItems
{
if(_extraMenuItems != extraMenuItems)
{
_extraMenuItems = extraMenuItems;
}
[self.extraTableView reloadData];
}
- (NSInteger)extraTableView:(UITableView *)extraTableView numberOfRowsInSection:(NSInteger)sectionIndex
{
return self.extraMenuItems.count;
}
- (UITableViewCell *)extraTableView:(UITableView *)extraTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Formal";
UITableViewCell *cell = [extraTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
[_extraMenuItemFiller addObject:#"Settings"];
[_extraMenuItemFiller addObject:#"Logout"];
NSString *cellValue = [_extraMenuItemFiller objectAtIndex:indexPath.row];
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.textLabel.numberOfLines = 0;
cell.textLabel.text = cellValue;
cell.textLabel.textColor = [UIColor blackColor];
cell.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f];
UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
myBackView.backgroundColor = [UIColor redColor];
cell.selectedBackgroundView = myBackView;
return cell;
}
What is wrong with it?
You shouldn't rename the tableView delegate and datasource methods: just test the tableView parameter that is passed to them, to determine which tableView they relate to. For example:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectionIndex
{
if (tableView == self.extraTableView) {
return self.extraMenuItems.count;
} else {
return self.menuItems.count;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == self.extraTableView) {
NSString *CellIdentifier = #"Formal";
UITableViewCell *cell = [extraTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
[_extraMenuItemFiller addObject:#"Settings"];
[_extraMenuItemFiller addObject:#"Logout"];
NSString *cellValue = [_extraMenuItemFiller objectAtIndex:indexPath.row];
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.textLabel.numberOfLines = 0;
cell.textLabel.text = cellValue;
cell.textLabel.textColor = [UIColor blackColor];
cell.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f];
UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
myBackView.backgroundColor = [UIColor redColor];
cell.selectedBackgroundView = myBackView;
return cell;
} else {
NSString *cellIdentifier = #"MenuItemCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
Department *dept = [self.menuItems objectAtIndex:indexPath.row];
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.textLabel.numberOfLines = 0;
cell.textLabel.text = dept.name;
cell.textLabel.textColor = [UIColor whiteColor];
cell.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f];
UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
myBackView.backgroundColor = [UIColor redColor];
cell.selectedBackgroundView = myBackView;
return cell;
}
}
And likewise for all the other tableView delegate and datasource methods. You also need to make sure that the delegate and datasource are set for both table views. You can do either do this in your storyboard, or in code eg. in viewDidLoad:
self.extraTableView.delegate = self;
self.extraTableView.datasource = self;
EDIT
You don't need both extraMenuItems and extraMenuItemFiller. I would use just extraMenuItems. Load it with the two values in viewDidLoad as follows:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.slidingViewController setAnchorRightRevealAmount:280.0f];
self.slidingViewController.underLeftWidthLayout = ECFullWidth;
self.view.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f];
self.tableView.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f];
self.tableView.separatorColor = [UIColor colorWithWhite:0.15f alpha:0.2f];
self.extraTableView.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f];
self.extraTableView.separatorColor = [UIColor colorWithWhite:0.15f alpha:0.2f];
self.extraMenuItems = #[#"Login",#"Settings"];
self.extraTableView.delegate = self;
self.extraTableView.datasource = self;
}
and amend the cellForRowAtIndexPath to use extraMenuItems rather than extraMenuItemFiller:
NSString *cellValue = [self.extraMenuItems objectAtIndex:indexPath.row];
In my my application I want to change the cell text color with out disappearing the cell separator.And I am using the following code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
static NSString *identifier = #"cell";
cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
UIView * selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
[selectedBackgroundView setBackgroundColor:[UIColor clearColor]]; // set color here
[cell setSelectedBackgroundView:selectedBackgroundView];
cell.backgroundColor=[UIColor clearColor];
cell.textLabel.highlightedTextColor = [UIColor redColor];
[cell setOpaque:NO];
}
cell.textLabel.text=[contentArray objectAtIndex:indexPath.row];
return cell;
}
But when I clicked the cell the cell separator also is disappearing ? How to change the text color without hiding the separator ?
It seems the cell separators are a problem for a lot of people. So, I would say rather than doing what I suggested to disable the selection, it would be easier to set the cell separator to 'none' and manage the separator yourself in the background and selected background views:
- (void)viewDidLoad
{
[super viewDidLoad];
// ...
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
static NSString *identifier = #"cell";
cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
UIView *selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
[selectedBackgroundView setBackgroundColor:[UIColor clearColor]];
[cell setSelectedBackgroundView:selectedBackgroundView];
UIView *backgroundView = [[UIView alloc] initWithFrame:cell.frame];
[backgroundView setBackgroundColor:[UIColor clearColor]];
[cell setBackgroundView:backgroundView];
UIView *selectedBackgroundSeparator = [[UIView alloc] initWithFrame:CGRectMake(tableView.separatorInset.left, cell.frame.size.height - 1, cell.frame.size.width - tableView.separatorInset.left, 1)];
UIView *backgroundSeparator = [[UIView alloc] initWithFrame:selectedBackgroundSeparator.frame];
selectedBackgroundSeparator.backgroundColor = backgroundSeparator.backgroundColor = tableView.separatorColor;
[selectedBackgroundView addSubview:selectedBackgroundSeparator];
[backgroundView addSubview:backgroundSeparator];
cell.textLabel.highlightedTextColor = [UIColor redColor];
[cell setOpaque:NO];
}
cell.textLabel.text=[contentArray objectAtIndex:indexPath.row];
return cell;
}
Alternatively, you can use the default cell separator, and instead just add your own top and bottom separators to the selectedBackgroundView:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
static NSString *identifier = #"cell";
cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
UIView *selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
[selectedBackgroundView setBackgroundColor:[UIColor clearColor]];
[cell setSelectedBackgroundView:selectedBackgroundView];
UIView *topSelectedBackgroundSeparator = [[UIView alloc] initWithFrame:CGRectMake(tableView.separatorInset.left, 0, cell.frame.size.width - tableView.separatorInset.left, 1)];
UIView *selectedBackgroundSeparator = [[UIView alloc] initWithFrame:CGRectOffset(topSelectedBackgroundSeparator.frame, 0, cell.frame.size.height)];
topSelectedBackgroundSeparator.backgroundColor = selectedBackgroundSeparator.backgroundColor = tableView.separatorColor;
[selectedBackgroundView addSubview:selectedBackgroundSeparator];
[selectedBackgroundView addSubview:topSelectedBackgroundSeparator];
cell.textLabel.highlightedTextColor = [UIColor redColor];
[cell setOpaque:NO];
}
cell.textLabel.text=[contentArray objectAtIndex:indexPath.row];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
for(id view in cell.containtView.subview) {
if([view isKindaOfClass:[UILabel class]])
UILabel* titleLabel = (UILabel*)view;
[titleLabel setTextColor:[UIColor whiteColor]]; // any you want
}
}
Rather than setting the cell's selectedBackgroundView to a clear view, would simply not allowing the cell to be highlighted when selected accomplish what you want? It will prevent the cell and separators from changing automatically based on the selection, but you will have to manage recognizing the tap gesture and highlighting the label text yourself.
Remove the selectedBackgroundView from your code.
You then need to implement tableView:shouldHighlightRowAtIndexPath: in your UITableViewDelegate:
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
return NO; // do what's appropriate based on the indexPath
}
I know this may be obvious, but i didn't set the background to any color in IB. My code for the UITableView is this:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellID = #"Cell Identifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if(!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
}
cell.textLabel.text = [_listeDesChapitres objectAtIndex:indexPath.row];
cell.backgroundColor = [UIColor clearColor];
return cell;
}
What I get is this:
The cell background is black by default, until I click it to see its text. Am i missing something?
You need to set the background of the label, and the background of the cell:
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.contentView.backgroundColor = [UIColor clearColor];
use :
cell.contentView.backgroundColor=[UIColor clearColor];
instead of:
cell.backgroundColor=[UIColor clearColor];