I use below code to try to make tableview cell background clear colour when selected and it works without checkbox but when there is it's not clear colour totally.
[cell setSelectionStyle:UITableViewCellSelectionStyleBlue];
cell.tintColor = [UIColor redColor];
UIView *selectionView = [UIView new];
selectionView.backgroundColor = [UIColor clearColor];
[[UITableViewCell appearance] setSelectedBackgroundView:selectionView];
As you can see from below picture, it's only clear under checkbox but not entire cell. But when there's no checkbox (not editing mode) it's entirely clear. How can I fix this? Thanks in advance.
If I remove the above code, the result will look like this. The white background will cover entire cell. I don't know why the above code can just set clear background to the area beneath checkbox?
add this method
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
[cell setBackgroundColor:[UIColor clearColor]];
}
Need to set clear background of cell and cell'contentView.
how about setting the selection style to none instead of blue?
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
I have find solution, We can edit below code
- (void)viewDidLoad {
[super viewDidLoad];
//Add below to line
[self.tblContact setAllowsMultipleSelectionDuringEditing:YES];
[self.tblContact setEditing:YES animated:YES];
//tblContact is tableView Outlet
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//ContactTableViewCell is xib file of UITableViewCell
static NSString *identifier = #"ContactTableViewCell";
ContactTableViewCell *cell = (ContactTableViewCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ContactTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleBlue];
cell.tintColor = [UIColor redColor];
UIView *selectionView = [UIView new];
selectionView.backgroundColor = [UIColor clearColor];
[[UITableViewCell appearance] setSelectedBackgroundView:selectionView];
cell.backgroundColor = [UIColor clearColor];
return cell;
}
Related
I want to enable a "dark mode" feature to my app and in my tableview cells, I'm changing textcolors like so
if ([FS isDarkModeEnabled]) {
cell.CATEGORY.textColor = [UIColor whiteColor];
cell.ORIGINUSER.textColor = [UIColor whiteColor];
cell.NUMFILES.textColor = [UIColor whiteColor];
cell.NUMTASKS.textColor = [UIColor whiteColor];
cell.SHARECOUNT.textColor = [UIColor whiteColor];
}
I figured I'd replace the above code with a for loop but
for (id obj in [cell.contentView subviews]) {
if ([obj isKindOfClass:[UILabel class]]) {
NSLog(#"%#", ((UILabel *)obj).text);//<-- this spits out my storyboard placeholder text and not the acutal text for some reason.
[((UILabel *)obj) setTextColor:[UIColor greenColor]];
}
}
and
FSCategoriesTVCCell *cell2 = [self.tableView cellForRowAtIndexPath:indexPath];
for (UILabel *lbl in [cell2.contentView subviews]) {
[lbl setTextColor:[UIColor greenColor]];
}
but am unable to get it working.
All the above code is done in - tableView:cellForRowAtIndexPath:
edit to add some clarification
I know how to change the text colors manually, what I want to do is loop through the cell's content and change the colors via a for loop and NOT by declaring each label and changing each one individually.
Or, in shorter words, I want a for loop, not to do cell.Label1.textColor = blah
When the dark mode is enabled by the user, you have to inform the tableview that it should update the cells. To do this, simply call tableView's [tableView reloadData] method.
Option 1:When View Load
- (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.textLabel.textColor = [UIColor blueColor];
cell.textLabel.text = #"highboi";
return cell;
}
Option 2:When Click or Select the tableView
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView cellForRowAtIndexPath:indexPath].textLabel.textColor = [UIColor redColor];
}
I have subclassed a UITableViewCell so that I can increase the scrolling performance which has worked out great for me so far.
In my subclass I have a method called seSelected that looks like this
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
if (selected) {
self.backgroundColor = [UIColor lightGrayColor];
}else{
self.backgroundColor = [UIColor whiteColor];
}
}
I would like to know how to make it so that if I touch the same cell it deselects the cell and changes the color back to white? I have tried a few different if statments in setSelected but nothings working.
Use this delegate method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
and call here method which manage selected and unselected cell.
One approach could be to set the tag on the cell and then use it to set the background color.
typedef enum {
CellSelected = 0,
CellDeselected
} CellSelection;
While creating the cell set the tag to "CellDeselected"
cell.tag = CellDeselected;
And then when cell is tapped just check which color you want to set in the background.
switch (customCell.tag) {
case CellSelected:
self.backgroundColor = [UIColor lightGrayColor];
break;
case CellDeselected:
self.backgroundColor = [UIColor whiteColor];
break;
default:
break;
}
customCell.tag = !customCell.tag;
Do like this...
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
str = [YourArray objectAtIndex:indexPath.row];//str is a string variable
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIndentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIndentifier];
}
cell.textLabel.text = [reminder objectAtIndex:indexPath.row];
cell.backgroundColor = [UIColor whiteColor];
if ([str isEqualToString:[reminder objectAtIndex:indexPath.row]])
{
cell.backgroundColor = [UIColor grayColor];
}
return cell;
}
You can use for this for your custom cell.....
use UITableViewCell method: [cell setSelectedBackgroundView:myBgColorView];.
Apple Documentation.
Example:
UIView *myBgColorView = [[UIView alloc] init];
myBgColorView.backgroundColor = [UIColor greenColor];
[cell setSelectedBackgroundView:myBgColorView];
set cell style:
cell.selectionStyle = UITableViewCellSelectionStyleNone;
and your code will work. But, you set color only selected cell.
If you need set color when cell is pressed, override this method:
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
I tried to set a background image to a label in UITableViewCell,But while running ,only the background image is displayed in label,text is not displayed.
This is my code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"TableCell";
// Dequeue or create a cell of the appropriate type.
TableCell *cell = (TableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"TableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
UIImageView *preparationLabelBg = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:#"cellbg.jpeg"]];
[cell.preparationLabel addSubview:preparationLabelBg];
cell.preparationLabel.backgroundColor=[UIColor clearColor];
[cell.preparationLabel sendSubviewToBack:preparationLabelBg];
[cell.preparationLabel setOpaque:NO];
}
[cell.preparationLabel setFont:[UIFont fontWithName:#"Marker Felt" size:14]];
// Configure the cell.
if (btnselected==1)
{
cell.preparationLabel.text = [recipe.ingredients objectAtIndex:indexPath.row];
}
else
{
cell.preparationLabel.text = [recipe.preparation objectAtIndex:indexPath.row];
}
return cell;
}
Why text is not displayed here?Can anyone help?
Try this code,
theLabel.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"cellbg.jpeg"]];
You should not add background image to UILable instead add background image to tableCell as subnview or add background image as [cell.contentview addSubview:[UIImage imageNamed:#"cellbg.jpeg"]]
Also use PNG instead of JPEG/JPG. Apple prefer PNG.
Instead of putting background to the lable put one ImageView at the back of lable and then set the image to that ImageView. It will work.
I want to implement touch up and touch down like functionality for my TableView Cell.
My need is giving different background color to the cell.
Give different background when we touch down the cell and different color when we touch up.
I try it with following way :
- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
[cell setBackgroundColor:[UIColor redColor]];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[cell setBackgroundColor:[UIColor greenColor]];
}
but the issue is didHighlightRowAtIndexPath is only available for iOS 6.0 not below that.
Is there another way to implement it.
In cellForRowAtIndexPath write below code:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CustomCellIdentifier = #"yourcell";
SettingsCell *cell = (SettingsCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
if (cell == nil) {
NSArray *nib = nib = [[NSBundle mainBundle] loadNibNamed:#"yourcell" owner:self options:nil];
for(id oneObject in nib) {
if([oneObject isKindOfClass:[yourCell class]]) {
cell = (yourCell *)oneObject;
}
}
//to change background color of selected cell
UIView *backgroundView = [[UIView alloc] init];
backgroundView.backgroundColor = [UIColor blueColor];
cell.selectedBackgroundView = backgroundView;
}
return cell;
}
And on didselectRow at indexPath, change to another color
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView reloadData];
UITableViewCell *cell=(UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
[cell setBackgroundColor:[UIColor redColor]];
}
Try this, you can have two images for selected and unselected rows.
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"unselected.png"] ];
cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"selected.png"] ];
You will be adding this in cellForRowAtIndexPath method. You can take state from didSelect right?? Trust me i have tried almost 10 different ways to do this and only this solution worked fine in all cases perfectly.
I am having a tableview and I want to know that how can I change the selected row's text color, say to Red ? I tried by this code :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell= [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:nil] autorelease];
cell.text = [localArray objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
cityName = [localArray objectAtIndex:indexPath.row];
UITableViewCell* theCell = [tableView cellForRowAtIndexPath:indexPath];
theCell.textColor = [UIColor redColor];
//theCell.textLabel.textColor = [UIColor redColor];
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
(1) When I select any row then text color changed to the red but when I select another then previously selected row's text remains red. how can I solve this ?
(2) When I scroll the table text color change to the black color how to solve this ?
Thanks..
Do this in tableView:cellForRowAtIndexPath::
cell.textLabel.highlightedTextColor = [UIColor redColor];
(And don't use cell.text = ... anymore. It has been deprecated for almost 2 years now. Use cell.textLabel.text = ... instead.)
As Raphael Oliveira mentioned in the comments, if the selectionStyle of your cell equals UITableViewCellSelectionStyleNone this won't work. Check Storyboard as well for the selection style.
If you want to change the text colour only, with out changing cell background colour. you can use this.Write this code in in cellForRowAtIndexPath method
UIView *selectionColor = [[UIView alloc] init];
selectionColor.backgroundColor = [UIColor clearColor];
cell.selectedBackgroundView = selectionColor;
cell.textLabel.highlightedTextColor = [UIColor redColor];
I had the same problem, try this!
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
for (id object in cell.superview.subviews) {
if ([object isKindOfClass:[UITableViewCell class]]) {
UITableViewCell *cellNotSelected = (UITableViewCell*)object;
cellNotSelected.textLabel.textColor = [UIColor blackColor];
}
}
cell.textLabel.textColor = [UIColor redColor];
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
That may be the solution to your (and mine) problem.
If you're already subclassing UITableViewCell, it's easier/cleaner to set the colors in the awakeFromNib method (assuming you're instantiating from a storyboard or xib).
#implementation MySubclassTableViewCell
- (void)awakeFromNib {
[super awakeFromNib];
self.selectedBackgroundView = [[UIView alloc] initWithFrame:self.frame];
self.selectedBackgroundView.backgroundColor = [UIColor colorWithRed:0.1 green:0.308 blue:0.173 alpha:0.6];
self.customLabel.highlightedTextColor = [UIColor whiteColor];
}
#end