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
Related
I have a tableviewcontroller that has dynamic controls created in cells. If it's a dropdown type, I take the user to a different tableviewcontroller to select the value. Once selected, I pop back and reload the data, but when I do that it overwrites the cells on top of one another. I know this is because I'm reusing the cells, but I cannot seem to figure out how to prevent it.
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
[self.tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
EWHInboundCustomAttribute *ca = [visibleCustomAttributes objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
cell.tag=indexPath.row;
if (ca.CustomControlType == 1) {
cell.detailTextLabel.hidden=true;
cell.textLabel.hidden=true;
UITextField *caTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 185, 30)];
caTextField.adjustsFontSizeToFitWidth = YES;
caTextField.textColor = [UIColor blackColor];
caTextField.placeholder = ca.LabelCaption;
if (ca.ReadOnly) {
[caTextField setEnabled: NO];
} else {
[caTextField setEnabled: YES];
}
caTextField.text=nil;
caTextField.text=ca.Value;
caTextField.tag=indexPath.row;
caTextField.delegate=self;
[cell.contentView addSubview:caTextField];
} else if (ca.CustomControlType == 4) {
cell.detailTextLabel.text=ca.Value;
cell.textLabel.text=ca.LabelCaption;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
} else {
cell.detailTextLabel.hidden=true;
cell.textLabel.hidden=true;
UITextField *caTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 185, 30)];
caTextField.adjustsFontSizeToFitWidth = YES;
caTextField.textColor = [UIColor grayColor];
caTextField.placeholder = ca.LabelCaption;
[caTextField setEnabled: NO];
caTextField.text = ca.Value;
caTextField.tag=indexPath.row;
caTextField.delegate=self;
[cell.contentView addSubview:caTextField];
}
return cell;
}
Instead of creating the UITextfield each time I would suggest at least using [UIView viewWithTag:tag] to capture the same UITextField object.
I'd suggest you to create custom UITableViewCell subclass and put all subviews related logic there.
Next, in order to reset/clear cell before reuse - you should override prepeareForReuse function.
Swift:
override func prepareForReuse() {
super.prepareForReuse()
//set cell to initial state here
}
First,I suggest you to use custom cells.If not and your cells are not so many,maybe you can try unique cell identifier to avoid cell reuse:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// unique reuseID
NSString *cellReuseID = [NSString stringWithFormat:#"%ld_%ld", indexPath.section, indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellReuseID];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellReuseID];
// do something
}
return cell;
}
Hope it's helpful.
I want my cell text color to change when tapped, but not the background color.
I want the cell background to always be white, and only the text color to change when selected.
I've seen a bunch of answers about how to do this...
UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = [UIColor whiteColor];
[cell setSelectedBackgroundView:bgColorView];
... but the view that gets created runs on top of the cell separator.
And to use cell.textLabel.highlightedTextColor = [UIColor brownColor]; to change the textColor, I can't have cell.selectionStyle = UITableViewCellSelectionStyleNone;, so I need to figure out something.
if you want to show the separator of cell , you may need this:
add this code :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
cell.selectionStyle = UITableViewCellSelectionStyleDefault;
cell.textLabel.highlightedTextColor = [UIColor brownColor];
UIView *backgroudView = [[UIView alloc]initWithFrame:CGRectMake(0, 1, tableView.bounds.size.width, [self tableView:tableView heightForRowAtIndexPath:indexPath] - 2)];
backgroudView.backgroundColor = [UIColor whiteColor];
UIView *placeholderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
placeholderView.backgroundColor = [UIColor clearColor];
[placeholderView addSubview:backgroudView];
cell.selectedBackgroundView = placeholderView;
...
}
// These codes are used to show the separatorView when the cell didSelected
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
void (^showSeparatorView)(UITableViewCell *cell) = ^(UITableViewCell *cell){
for (id obj in cell.subviews) {
if([obj isKindOfClass:NSClassFromString(#"_UITableViewCellSeparatorView")]){
UIView *view = (UIView *)obj;
view.hidden = NO;
}
}
};
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
showSeparatorView(cell);
if (indexPath.row > 0)
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row - 1 inSection:indexPath.section]];
showSeparatorView(cell);
}
...
}
// These codes are used to show the separatorView when the cell didHightlight
- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
void (^showSeparatorView)(UITableViewCell *cell) = ^(UITableViewCell *cell){
for (id obj in cell.subviews) {
if([obj isKindOfClass:NSClassFromString(#"_UITableViewCellSeparatorView")]){
UIView *view = (UIView *)obj;
view.hidden = NO;
}
}
};
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
showSeparatorView(cell);
if (indexPath.row > 0)
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row - 1 inSection:indexPath.section]];
showSeparatorView(cell);
}
}
Hope these can help to you!
Add this in cellForRowAtIndexPath method so when you tap in cell than change label text color
cell.textLabel.highlightedTextColor = [UIColor brownColor];
You can use this -
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *c = [tableView cellForRowAtIndexPath:indexPath];
[c.textLabel setTextColor:[UIColor brownColor]];
}
You can add the code cell.selectedBackgroundView = [UIView new]; to figure out this. With this method, you don't need cell.selectionStyle = UITableViewCellSelectionStyleNone;.
I have a custom UITableViewCell named as Hobbies.
Everything is working fine.Except one UIIssue.
When user taps on any cell I want to change the text colour of that particular Cell .And when user select another I want the previous selected cell should return to its original state.
Currently I am able to change the colour on Cell select but not able to revert it back when user selects another.
Here is the code I am using to change the textColor of a particular cell:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
HobbiesCell *cell = (HobbiesCell*)[tableView cellForRowAtIndexPath:indexPath];
cell.dateLabel.textColor = [UIColor colorWithWhite:255 alpha:0.5f];
}
How can I revert It back when user selects another cell.
You can create an object of UITableViewCell say previousCell and assign it each time you select one. This will be your last selected cell and you can assign it the default color each time you click a new cell.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
HobbiesCell *cell = (HobbiesCell*)[tableView cellForRowAtIndexPath:indexPath];
previousCell.dateLabel.textColor = [UIColor blackColor]; //Assuming that this is the color you want to go back
cell.dateLabel.textColor = [UIColor colorWithWhite:255 alpha:0.5f];
previousCell = cell;
}
declare this variable
int selectedIndex;
in your cellForRowAtIndexPath
if(indexPath.row == selectedIndex)
{
cell.dateLabel.textColor = [UIColor colorWithWhite:255 alpha:0.5f];
}
else
{
cell.dateLabel.textColor = [UIColor colorWithWhite:0 alpha:0.5f];//your default cell color
}
and in your didSelectRowAtIndex
selectedIndex = indexPath.row;
[tableView reloadData];
You should always keep in mind that the cells are reusable, so the one you change will be used as is for displaying other rows when you scroll.
Instead, you should keep an array of your own models that keep the data (in your case color information) and use the cells only for displaying it.
To revert the color simply keep a reference to the latest NSIndexPath.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
MyRowModel *prevRowModel = [self.rowModels objectAtIndex:self.lastIndexPath.row];
prevRowModel.color = [UIColor colorWithWhite:255 alpha:1f];
MyRowModel *rowModel = [self.rowModels objectAtIndex:indexPath.row];
rowModel.color = [UIColor colorWithWhite:255 alpha:0.5f];
[self.tableView reloadRowsAtIndexPaths:#[indexPath, self.lastIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
self.lastIndexPath = indexPath;
}
If you go for reload the tableview then try this .
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// reload the table
[tableView reloadData];
HobbiesCell *cell = (HobbiesCell*)[tableView cellForRowAtIndexPath:indexPath];
previousCell.dateLabel.textColor = [UIColor colorWithWhite:255 alpha:0.5f]; // color insert which you want to insert
}
hope it helps you without adding varible.
Try cell.textLabel.highlightedTextColor.
if (cell == nil) {
........
/* your cell initiation code */
........
cell.textLabel.textColor = [UIColor whiteColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.highlightedTextColor = [UIColor orangeColor];
}
You need to maintain a reference to the currently selected indexPath. Something like...
#property (nonatomic, strong) NSIndexPath *currentHobby;
Then in your didSelectRowAtIndexPath: method insert this...
if(_currentHobby && ![_currentHobby isEqual:indexPath]) {
HobbiesCell *currentCell = [tableView cellForRowAtIndexPath:_currentHobby];
currentCell.dateLabel.textColor = [UIColor originalColor];
}
_currentHobby = indexPath;
Then in your cellForRowAtIndexPath: make sure you include...
if([indexPath isEqual:_currentHobby]) {
cell.dateLabel.textColor = [UIColor selectedColor];
} else {
cell.dateLabel.textColor = [UIColor originalColor];
}
I have UITableviewCell and i placed 4 buttons in cell. When i click one button i need to change its background color to red.
Soo right now i have written code for this and when i click one button then that button background color is not changing instead of that same button in some other row changing background color.
Use case:
1.I have 10 rows in UITableView and each cell contains 4 buttons named as
"Good","Better","Best","Worst".
When i click on "Good" button in first row am expecting it should change color to red.
3.Right now if i click "Good " button in first row then its not changing color instead while scrolling down but i can see "Good" button in 4th and 8th is changed to red .
So some mistake in my code for changing color.
Please check my tableview code and Button click code
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 4;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
int sectionCount;
if(section==0)
{
sectionCount=4;
}else if(section==1){
sectionCount=4;
}else if (section==2){
sectionCount=3;
}else if(section==3){
sectionCount=1;
}
return sectionCount;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
sharedManager=[Mymanager sharedManager];
static NSString *CellIdentifier = #"cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[questioncell alloc] initWithStyle:UITableViewCellStyleValue1
reuseIdentifier:CellIdentifier];
}
cell.excellentButton.tag=indexPath.row;
cell.goodButotn.tag=indexPath.row;
cell.fineButton.tag=indexPath.row;
cell.dorrButton.tag=indexPath.row;
if(indexPath.section==0){
cell.question.text=[questions objectAtIndex:indexPath.row];
} else if(indexPath.section==1){
cell.question.text=[section1 objectAtIndex:indexPath.row];
}else if(indexPath.section==2){
cell.question.text=[section2 objectAtIndex:indexPath.row];
}else if(indexPath.section==3){
cell.question.text=[section3 objectAtIndex:indexPath.row];
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 200;
}
Button click code
- (IBAction)goodButton:(id)sender {
UIButton *button = (UIButton *)sender; // first, cast the sender to UIButton
NSInteger row = button.tag; // recover the row
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
NSString *key=[NSString stringWithFormat:#"%ld",(long)indexPath.row];
[sharedManager.ratingDic setValue:#"Good" forKey:key];
cell.goodButotn.layer.backgroundColor=[[UIColor colorWithRed:39/255.0 green:174/255.0 blue:96/255.0 alpha:100] CGColor ];
cell.betterButton.layer.backgroundColor=[[UIColor clearColor] CGColor ];
cell.bestButton.layer.backgroundColor=[[UIColor clearColor] CGColor ];
cell.worstButton.layer.backgroundColor=[[UIColor clearColor] CGColor ];
}
Please help me to clear this issue
UIButton *button = (UIButton *)sender; <-- this is already your reference to the button. I see there 2 ways to do it
create a custom UITableViewCell and implement methods for resetting all colors and setting the correct color. This is the clean way. Here you can implement more logic and have always the correct corresponding data. Here every of your buttons call an own method and there you have also a clear method.
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if( self = [super initWithStyle:style reuseIdentifier:reuseIdentifier] )
{
[excellentButton addTarget:self action:#selector(clickedExcellentButton) forControlEvents:UIControlEventTouchDown]; // or TouchUp, how ever you like
[goodButton addTarget:self action:#selector(clickedGoodButton) forControlEvents:UIControlEventTouchDown];
[fineButton addTarget:self action:#selector(clickedFineButton) forControlEvents:UIControlEventTouchDown];
[dorrButton addTarget:self action:#selector(clickedWoButton) forControlEvents:UIControlEventTouchDown];
}
return self;
}
-(UIColor *)selectionColor
{
return [UIColor colorWithRed:39/255.0 green:174/255.0 blue:96/255.0 alpha:100];
}
-(void)resetSelection
{
excellentButton.backgroundColor = [UIColor clearColor];
goodButton.backgroundColor = [UIColor clearColor];
fineButton.backgroundColor = [UIColor clearColor];
dorrButton.backgroundColor = [UIColor clearColor];
}
-(void)clickedExcellentButton
{
[self resetSelection];
excellentButton.backgroundColor = [self selectionColor];
NSString *key=[NSString stringWithFormat:#"%ld",(long)indexPath.row];
[sharedManager.ratingDic setValue:#"Excellent" forKey:key]; // if you have your sharedManager object here. If you cannot access it from here, you have to forward it or give the cell a reference to it
}
-(void)clickedGoodButton
{
[self resetSelection];
goodButton.backgroundColor = [self selectionColor];
NSString *key=[NSString stringWithFormat:#"%ld",(long)indexPath.row];
[sharedManager.ratingDic setValue:#"Good" forKey:key];
}
...
or
- (IBAction)goodButton:(id)sender {
UIButton *button = (UIButton *)sender; // this is the button that was clicked
// [...]
for(UIButton *ctrl in [button.superview subviews]) // button.superview get the view that holds him. Subviews all the others in his layer
{
if([ctrl isKindOfClass:[UIButton class]])
{
ctrl.backgroundColor = [UIColor clearColor];
}
}
button.backgroundColor = [UIColor colorWithRed:39/255.0 green:174/255.0 blue:96/255.0 alpha:100];
I create a custom tableview cell, and those four button use the same IBAction when the button been pressed. Then change the background color of that button. It works.
TableView
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 2;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellID = #"cell";
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (!cell) {
cell = [[NSBundle mainBundle] loadNibNamed:#"TableViewCell" owner:self options:nil][0];
}
return cell;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
Button click code
- (IBAction)actionButtonPressed:(UIButton *)sender {
[sender setBackgroundColor:[UIColor redColor]];
}
You don't need to use the layer of the buttons it is enough to set the backgroundColor ( i assume that you are able to access your buttons over cell.Button)
cell.goodButton.backgroundColor = [[UIColor colorWithRed:39/255.0 green:174/255.0 blue:96/255.0 alpha:100] CGColor ];
...
cell.betterButton.backgroundColor = [UIColor clearColor];
cell. bestButton.backgroundColor = [UIColor clearColor];
cell. worstButton.backgroundColor = [UIColor clearColor];
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