I have tried everything to get this working, but when I scroll cellForRowAtIndexPath, it seems to be adding and removing checkmarks on my tableview, can anyone help?
// Configure the cell...
cell.textLabel.text = [currencyList objectAtIndex:indexPath.row];
// Check to see what currency is currently selected and checkstyle
if ([selectedCurrency isEqualToString:cell.textLabel.text]) {
// Add a tick to the selected row
cell.accessoryType = UITableViewCellAccessoryCheckmark;
// Change the text colour
cell.textLabel.textColor = [UIColor colorWithRed:0 green:0 blue:0.5 alpha:1];
self.checkedIndexPath = indexPath;
} else {
UITableViewCell *uncheckCell = [self.tableView
cellForRowAtIndexPath:self.checkedIndexPath];
uncheckCell.accessoryType = UITableViewCellAccessoryNone;
// Change the font colour back to black
uncheckCell.textLabel.textColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1];
}
You don't need to uncheck cell in cellForRowAtIndexPath. Better sollution is just reload checked cell.
- (void)setCheckedIndexPath:(NSIndexPath *)indexPath
{
NSIndexPath *uncheckIndexPath = self.checkedIndexPath;
_checkedIndexPath = indexPath;
[self.tableView reloadRowsAtIndexPaths:#[uncheckIndexPath]
withRowAnimation:UITableViewRowAnimationLeft];
}
And change cellForRowAtIndexPath. Please note that we set UITableViewCellAccessoryNone to cell that we must return, not for cell that you want to uncheck.
// Configure the cell...
cell.textLabel.text = [currencyList objectAtIndex:indexPath.row];
// Check to see what currency is currently selected and checkstyle
if ([selectedCurrency isEqualToString:cell.textLabel.text]) {
// Add a tick to the selected row
cell.accessoryType = UITableViewCellAccessoryCheckmark;
// Change the text colour
cell.textLabel.textColor = [UIColor colorWithRed:0 green:0 blue:0.5 alpha:1];
self.checkedIndexPath = indexPath;
} else {
//If cell unchecked
cell.accessoryType = UITableViewCellAccessoryNone;
// Change the font colour back to black
cell.textLabel.textColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1];
}
Related
Trying to change the UITableViewCell text UIColour (costume cell) will make the text become white and unseen, than only when touching the UITableViewCell, I can see the text .
Why setting UIColour to a UITableViewCell that is not blackColor will not work ?
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell =
[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier
forIndexPath:indexPath];
if (!indexPath.row) {
NSDictionary *dic= [[GlobalData sharedGlobals].categories
objectAtIndex:indexPath.section];
cell.textLabel.text = [dic objectForKey:#"title"]; // only top row showing
cell.textLabel.font = [UIFont fontWithName:#"Arial Rounded MT Bold"
size:22];
cell.textLabel.textColor = [UIColor colorWithRed:177
green:218
blue:229
alpha:1];
//cell.textLabel.backgroundColor = [UIColor colorWithRed:218
green:241
blue:245
alpha:1];
} else {
NSMutableArray *typesOfSection =
[[GlobalData sharedGlobals].typesByCategories
objectAtIndex:indexPath.section];
NSDictionary *type = [typesOfSection objectAtIndex:indexPath.row-1];
NSString *titleOfType = [type objectForKey:#"title"];
cell.textLabel.text = titleOfType;
cell.textLabel.textColor = [UIColor colorWithRed:122
green:181
blue:196
alpha:1];
cell.textLabel.font = [UIFont fontWithName:#"Arial Rounded MT Bold"
size:22];
//cell.textLabel.text = #"check";
cell.accessoryView = nil;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
return cell;
}
And when doing this :
cell.textLabel.textColor = [UIColor redColor];
its working..
You are basically setting the cell's text colour to white. The RGB values should be between 0.0 and 1.0.
Try this
[UIColor colorWithRed:122.0/255.0 green:181.0/255.0 blue:196.0/255.0 alpha:1.0];
Use this
You did not add .0f(float) behind each parameter by which you are not getting cell textcolor.
[UIColor colorWithRed:122.0f/255.0f green:181.0f/255.0f blue:196 .0f/255.0f alpha:1.0f];
In my cellForRowAtIndexPath method I am using following if statements:
if ([taskitem.isCritical isEqualToString:#"iscritical"]){
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.backgroundColor = [UIColor redColor];
}
else if ([taskitem.isUrgent isEqualToString:#"isurgent"]){
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.backgroundColor = [UIColor blueColor];
}
else if ([taskitem.isCompletedOK isEqualToString:#"iscompleted"]){
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.backgroundColor = [UIColor greenColor];
UIButton *doneButton4 = [[UIButton alloc]initWithFrame:CGRectMake(0, 10, 12, 12)];
[doneButton4 setImage:[UIImage imageNamed:#"done"]forState:UIControlStateNormal];
[cell addSubview:doneButton4];
}
else {
cell.textLabel.textColor = [UIColor blackColor];
}
cell.textLabel.text = taskitem.taskName;
The problem is that the if statements are changing their behaviour if the user taps on any of the sections headers.
Do you find any error on my code that could be the reason of this weird behaviour or should I search for another reason in another method?
It looks like you're probably having a cell reuse problem. You need to make sure that anything you do in one case, is done for each case (or undone).
For example, in first your three conditions, you set the text colour and the background colour. But in the fourth case (the else) you only set the text colour, leaving the background colour to be whatever it was on this cell the last time it was used. You need to set the background colour in the else case as well. (Optionally, you can set all items back to defaults before the if).
You have a second problem here, with the third case, that creates and adds a button to the cell. But in the other cases, you do not remove that button if it exists. So when you get a cell that was used for a completed item earlier, you'll end up with a button that doesn't belong. Even if the cell is used for another completed item, you'll get two buttons on the same cell. One on top of the other (so it likely won't be visible, but it's still a problem).
Try using this code:
UIButton* oldButton = [cell viewWithTag:253];
if (oldButton)
{
[oldButton removeFromSuperview];
}
if ([taskitem.isCritical isEqualToString:#"iscritical"]) {
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.backgroundColor = [UIColor redColor];
}
else if ([taskitem.isUrgent isEqualToString:#"isurgent"]) {
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.backgroundColor = [UIColor blueColor];
}
else if ([taskitem.isCompletedOK isEqualToString:#"iscompleted"]) {
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.backgroundColor = [UIColor greenColor];
UIButton *doneButton4 = [[UIButton alloc] initWithFrame:CGRectMake(0, 10, 12, 12)];
[doneButton4 setImage:[UIImage imageNamed:#"done"] forState:UIControlStateNormal];
doneButton4.tag = 253;
[cell addSubview:doneButton4];
}
else {
cell.textLabel.textColor = [UIColor blackColor];
cell.textLabel.backgroundColor = [UIColor whiteColor];
}
cell.textLabel.text = taskitem.taskName;
I don't recommend using tags like this in production code, but it's a quick way to see if it solves your problems. In real code, use a UITableViewCell subclass with a property for the done button that you can just show and hide. Ideally you're not creating it each time as that's an expensive operation and could slow down your scrolling performance.
Create doneButton4 in (cell == nil) when reuse cells in table view
- (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];
UIButton *doneButton4 = [[UIButton alloc]initWithFrame:CGRectMake(0, 10, 12, 12)];
[doneButton4 setImage:[UIImage imageNamed:#"done"]forState:UIControlStateNormal];
[cell addSubview:doneButton4];
doneButton4.tag=100;
}
UIButton *doneButton4=(UIButton*)[cell viewWithTag:100];
doneButton4.hidden=YES;
if ([taskitem.isCritical isEqualToString:#"iscritical"]){
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.backgroundColor = [UIColor redColor];
}
else if ([taskitem.isCritical isEqualToString:#"isurgent"]){
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.backgroundColor = [UIColor blueColor];
}
else if ([taskitem.isCritical isEqualToString:#"iscompleted"]){
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.backgroundColor = [UIColor greenColor];
doneButton4.hidden=NO;
}
else {
cell.textLabel.textColor = [UIColor blackColor];
}
cell.textLabel.text = taskitem.taskName;
}
I am making an app which shows sport results and color codes them accordingly by setting their cell's background color:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:#"Cell"];
if (_tabBar.selectedItem == _homeGamesItem) {
cell.backgroundColor = [UIColor whiteColor];
cell.detailTextLabel.text = #"";
cell.textLabel.text = [NSString stringWithFormat:#"Home Game %d", indexPath.row];
} else if (_tabBar.selectedItem == _myEventsItem) {
cell.backgroundColor = [UIColor whiteColor];
cell.detailTextLabel.text = #"";
cell.textLabel.text = [NSString stringWithFormat:#"My Result %d", indexPath.row];
} else if (_tabBar.selectedItem == _resultsItem) {
SportEvent *event = [self.appDelegate.user.resultSportEvents objectAtIndex:indexPath.row];
if ([event.gender isEqualToString:#""]) {
cell.textLabel.text = [NSString stringWithFormat:#"%# %#", event.level, event.activity];
cell.detailTextLabel.text = event.result;
} else {
cell.textLabel.text = [NSString stringWithFormat:#"%# %# %#", event.gender, event.level, event.activity];
cell.detailTextLabel.text = event.result;
}
if ([[event.result substringToIndex:1] isEqualToString:#"W"])
cell.backgroundColor = [UIColor colorWithRed:0 green:1 blue:0 alpha:.05];
else if ([[event.result substringToIndex:1] isEqualToString:#"T"])
cell.backgroundColor = [UIColor colorWithRed:1 green:1 blue:0 alpha:.05];
else if ([[event.result substringToIndex:1] isEqualToString:#"L"])
cell.backgroundColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:.05];
}
if (_grayedOut) {
cell.textLabel.textColor = [UIColor grayColor];
cell.detailTextLabel.textColor = [UIColor grayColor];
} else {
cell.textLabel.textColor = [UIColor blackColor];
cell.detailTextLabel.textColor = [UIColor blackColor];
}
return cell;
}
The strange thing is that this causes some cells to have strange backgrounds right around the labels (my apologies for the massive image):
Why do only some cells get this strange darkening around the labels?
Since you use these cells in other tabs, and you only change some cell's background color to white (e.g. homeGames), then add the following code to make all reusable cells have the same color (clearColor) before actually using them again:
...
if (cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:#"Cell"];
//Add This Code
cell.backgroundColor = [UIColor clearColor];
//Also make labels same color just to be sure
cell.textLabel.backgroundColor = [UIColor clearColor]; //or whiteColor
cell.detailTextLabel.backgroundColor = [UIColor clearColor]; //or whiteColor
if (_tabBar.selectedItem == _homeGamesItem) {
...
I am using a slide in menu style which loads a UITableView. - ECSlidingViewController
I have about 7 cells in a table view setup as follows:
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.contentView.backgroundColor = [UIColor colorWithRed:75.0/255.0 green:83.0/255.0 blue:102.0/255.0 alpha:1.0];
UIView *topSplitterBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cell.bounds.size.width, 1)];
topSplitterBar.backgroundColor = [UIColor colorWithRed:62.0/255.0 green:69.0/255.0 blue:85.0/255.0 alpha:1];
[cell.contentView addSubview:topSplitterBar];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor colorWithRed:196.0/255.0 green:204.0/255.0 blue:218.0/255.0 alpha:1];
cell.textLabel.font = [UIFont systemFontOfSize:18.0f];
cell.textLabel.shadowColor = [UIColor colorWithRed:27.0/255.0 green:31.0/255.0 blue:41.0/255.0 alpha:1];
cell.textLabel.shadowOffset = CGSizeMake(0, 1);
UIView *selectedBg = [[UIView alloc] initWithFrame:cell.frame];
selectedBg.backgroundColor = [UIColor colorWithRed:50.0/255.0 green:56.0/255.0 blue:73.0/255.0 alpha:1];
cell.selectedBackgroundView = selectedBg;
What would be the best way to show a cell as the selectedBg if that is the currently displayed controller?
I can access the following for example:
if ([self.slidingViewController.topViewController isKindOfClass:[MESHomeViewController class]]) {
However, I am not sure where would be best practice to set this up? I can do it in the switch case for the cell label setup... For example:
switch ( indexPath.row ) {
case 0: {
if ([self.slidingViewController.topViewController isKindOfClass:[MESHomeViewController class]]) {
cell.contentView.backgroundColor = [UIColor colorWithRed:50.0/255.0 green:56.0/255.0 blue:73.0/255.0 alpha:1];
}
cell.textLabel.text = NSLocalizedString(#"LMGames", #"Left Menu - Games");
break ;
However, when a new item is selected from the menu I would need to reload the table each time, is that good? Completing a self.tableView reloadData each time a cell is selected, or is there a better way to approach this?
Two ideas for you:
Set the selectedBg in the didSelectRowAtIndexPath method in order to set the selected cell.
Keep an integer reference to the currently selected row and the previously selected row and then refresh only those rows by using a method similar to: How to reload and animate just one UITableView cell/row?
I hope that helps!
I have a custom TableViewCell that I've created. Each cell has two UIlabels that I want to be different colors. Also, I have one section of my tableView that I want to have a different background color and text color.
Right now, I can't get the text color to change using my UILabel.textColor; although I did get it to work with cell.textLabel.textColor. Also cell.background color isn't working.
I was able to get the cell property changes to work if I did it inside my switch statement at the bottom, but the cell properties wouldn't deque properly so as I scrolled up and down random cells would become white text on red background.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"MyIdentifier";
UILabel* itemLabel;
UILabel* amountLabel;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
itemLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 220.0, 35.0)] autorelease];
itemLabel.tag = ITEMLABEL_TAG;
itemLabel.font = [UIFont systemFontOfSize:14.0];
itemLabel.textAlignment = UITextAlignmentLeft;
itemLabel.autoresizingMask = UIViewAutoresizingNone;
itemLabel.backgroundColor = [UIColor clearColor];
itemLabel.textColor = [UIColor blueColor]; //This doesn't work
if (indexPath.section == 4)
{
cell.textLabel.textColor = [UIColor whiteColor]; //This works
cell.backgroundColor = [UIColor redColor]; //This doesn't work
}
[cell.contentView addSubview:itemLabel];
cell.backgroundColor = [UIColor whiteColor];
amountLabel = [[[UILabel alloc]initWithFrame:CGRectMake(225.0, 0.0, 55.0, 35.0)] autorelease];
amountLabel.tag = AMOUNTLABEL_TAG;
amountLabel.font = [UIFont systemFontOfSize:14.0];
amountLabel.textAlignment = UITextAlignmentLeft;
amountLabel.textColor = [UIColor blackColor]; //This doesn't work
amountLabel.backgroundColor = [UIColor clearColor];
amountLabel.autoresizingMask = UIViewAutoresizingNone;
[cell.contentView addSubview:amountLabel];
switch (indexPath.section) {
case 0:
cell.textLabel.text = [monthlyExpenses objectAtIndex:indexPath.row];
break;
case 1:
cell.textLabel.text = [oneTimeFees objectAtIndex:indexPath.row];
break;
case 2:
cell.textLabel.text = [renters objectAtIndex:indexPath.row];
break;
case 3:
cell.textLabel.text = [travelExpenses objectAtIndex:indexPath.row];
break;
case 4:
cell.textLabel.text = #"Generate Report";
break;
}
return cell;
}
You will need to change the background color of the cell in a - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
This is where you can do customization just before the table displays the cell.