Unable to set label colour in iOS [duplicate] - ios

This question already has an answer here:
UITableViewCell textLabel color not changing
(1 answer)
Closed 9 years ago.
I have a tableview that contains two cells. Now these cells contain labels and I want to change the colour of these label.
My code:
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CheckerCell";
CustomCell *cell =[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.titleLabel.text = [titleArray objectAtIndex:indexPath.row];
cell.subTitleLabel.text=[subTitleArray objectAtIndex:indexPath.row];
if (indexPath.row == 0) {
cell.textLabel.textColor = [UIColor colorWithRed:78/255.0 green:157/255.0 blue:19/255.0 alpha:1.0];
}
else if(indexPath.row == 1)
{
cell.textLabel.textColor = [UIColor colorWithRed:167/255.0 green:19/255.0 blue:43/255.0 alpha:1.0];
}
}
What am i missing here? Also this code is in base class. does it matter?
Please guide.

You should first create your cell:
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if( !cell ) {
cell = [[UITableViewCell alloc] init];
if (indexPath.row == 0) {
cell.textLabel.textColor = [UIColor colorWithRed:78/255.0 green:157/255.0 blue:19/255.0 alpha:1.0];
}
else if(indexPath.row == 1)
{
cell.textLabel.textColor = [UIColor colorWithRed:167/255.0 green:19/255.0 blue:43/255.0 alpha:1.0];
}
}
}
Also checkout other functions to dequeue cells as well and see which best fits your need here.
EDIT:
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CheckerCell";
CustomCell *cell =[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// See this missing piece of code to create a cell
if( !cell ) {
cell = [[[NSBundle mainBundle] loadNibNamed:#"SomeTableViewCellDesignedFromXIB" owner:nil options:nil] objectAtIndex:0];
}
// See this missing piece of code
cell.titleLabel.text = [titleArray objectAtIndex:indexPath.row];
cell.subTitleLabel.text=[subTitleArray objectAtIndex:indexPath.row];
if (indexPath.row == 0) {
cell.textLabel.textColor = [UIColor colorWithRed:78/255.0 green:157/255.0 blue:19/255.0 alpha:1.0];
}
else if(indexPath.row == 1)
{
cell.textLabel.textColor = [UIColor colorWithRed:167/255.0 green:19/255.0 blue:43/255.0 alpha:1.0];
}
}

Try this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
if (indexPath.row == 0) {
cell.textLabel.textColor = [UIColor colorWithRed:78/255.0 green:157/255.0 blue:19/255.0 alpha:1.0];
}
else if(indexPath.row == 1)
{
cell.textLabel.textColor = [UIColor colorWithRed:167/255.0 green:19/255.0 blue:43/255.0 alpha:1.0];
}
cell.textLabel.text = #"your text here";
return cell;
}

You are not creating the cell in the first place, which you need to do in order to set the colour
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString static * identifiderPlain = #"CellPlain";
UITableViewCell * cell;
cell = [tableView dequeueReusableCellWithIdentifier:identifiderPlain];
if(!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifiderPlain];
}
if (indexPath.row == 0) {
cell.textLabel.textColor = [UIColor colorWithRed:(78.0f/255.0f) green:(157.0f/255.0f) blue:(19.0f/255.0f) alpha:1.0f];
}
else if(indexPath.row == 1)
{
cell.textLabel.textColor = [UIColor colorWithRed:(167.0f/255.0f) green:(19.0f/255.0f) blue:(43.0f/255.0f) alpha:1.0f];
}
return cell;
}

Use This
cell.textLabel.backgroundColor = [UIColor colorWithRed:78/255.0 green:157/255.0 blue:19/255.0 alpha:1.0];

Related

Expandable UITableViewCell overlapping other cells

I'm working on a UITableView with expandable cells and I got it working with:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if(_selectedCellIndexPath != nil
&& [_selectedCellIndexPath compare:indexPath] == NSOrderedSame)
return 160;
return 40;
}
and
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
_selectedCellIndexPath = indexPath;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
And the UITableViewCell set up in Storyboard as:
But when I change it to this:
to add the description and button, it scrambles up everything like this:
How come the description textview and more info button are showing even though the heightForRowAtIndexPath is 40 and they should be 'hidden'?
Or am I taking a wrong approach to this?
Also, the tapping and expanding is not applied to the searchDisplayController.searchResultsTableView. How can I do this?
Edit: cellForRowAtIndexPath method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"IngredientCell";
UITableViewCell *cell= [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.backgroundColor = [UIColor colorWithRed:1.000 green:1 blue:1 alpha:0.333];
cell.tintColor = [UIColor whiteColor];
UILabel *title = (UILabel*) [cell viewWithTag:1];
UILabel *summary = (UILabel*) [cell viewWithTag:2];
UIButton *moreInfo = (UIButton*) [cell viewWithTag:3];
summary.hidden = YES;
moreInfo.hidden = YES;
[title setTextColor: [UIColor whiteColor]];
// Display recipe in the table cell
if (tableView == self.searchDisplayController.searchResultsTableView) {
[title setText: [searchResults objectAtIndex:indexPath.row]];
//set summary
} else {
[title setText: [ingredients objectAtIndex:indexPath.row]];
//set summary
}
// Change color if dissallowed
if ([[title text] rangeOfString:#"Egg"].location != NSNotFound) {
cell.backgroundColor = [UIColor colorWithRed:1.000 green:0.350 blue:0.485 alpha:0.333];
}
// Change color when selected
UIView *customColorView = [[UIView alloc] init];
customColorView.backgroundColor = [UIColor clearColor];
cell.selectedBackgroundView = customColorView;
return cell;
}

Redraw issue with UITableView using Objective C and Xcode 6

I have a tableView which lists student names and highlights the row green for present or red for absent. If you click the row, the color is changed to reflect and change in status for the student.
However, when a student is marked absent (highlighted red) and then I scroll the row out of view the row turns grey. I would love for the row to remain red (ugh).
Here are the tableView methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [kidsNames count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"myCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [kidsNames objectAtIndex:(indexPath.row)];
NSString * status=[kidsAbsent objectAtIndex:indexPath.row];
if([status isEqualToString: #"Present"]==1){
cell.contentView.backgroundColor = [UIColor greenColor];
}else{
cell.contentView.backgroundColor = [UIColor redColor];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString * ab=[kidsAbsent objectAtIndex:indexPath.row];
static NSString *CellIdentifier = #"myCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [tableView cellForRowAtIndexPath:indexPath];
NSLog(#"CLICKED!");
if([ab isEqualToString: #"Present"]==1){
[kidsAbsent replaceObjectAtIndex:indexPath.row withObject:#"Absent"];
cell.contentView.backgroundColor = [UIColor redColor];
NSLog(#"Now Absent %#",[kidsNames objectAtIndex:(indexPath.row)]);
}else{
cell.contentView.backgroundColor = [UIColor greenColor];
[kidsAbsent replaceObjectAtIndex:indexPath.row withObject:#"Present"];
NSLog(#"Now Present %#",[kidsNames objectAtIndex:(indexPath.row)]);
}
stuID=[kidsID objectAtIndex:indexPath.row];
stuAB=[kidsAbsent objectAtIndex:indexPath.row];
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString * ab=[kidsAbsent objectAtIndex:indexPath.row];
static NSString *CellIdentifier = #"myCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [tableView cellForRowAtIndexPath:indexPath];
NSLog(#"CLICKED!");
if([ab isEqualToString: #"Present"]==1){
[kidsAbsent replaceObjectAtIndex:indexPath.row withObject:#"Absent"];
cell.contentView.backgroundColor = [UIColor redColor];
NSLog(#"Now Absent %#",[kidsNames objectAtIndex:(indexPath.row)]);
}else{
cell.contentView.backgroundColor = [UIColor greenColor];
[kidsAbsent replaceObjectAtIndex:indexPath.row withObject:#"Present"];
NSLog(#"Now Present %#",[kidsNames objectAtIndex:(indexPath.row)]);
}
//send info to the web===============================================================================
stuID=[kidsID objectAtIndex:indexPath.row];
stuAB=[kidsAbsent objectAtIndex:indexPath.row];
}
Try to set cell.backgroundView not cell.contentView.backgroundColor
Or use cell.contentView.backgroundColor but you should note that the backgroundColor must be set in the tableView:willDisplayCell:forRowAtIndexPath: method (from UITableViewCell reference):

How to set a Tableview cell alpha value

I have a UITableView. And i have 4 cell in that Tableview.
My Tableview alpha value is 0.8.
But my requirement is i want to first 3 cell will be alpha value 0.8 and 4th cell will be 1.0 alpha value.
So how can i set these alpha value according to cell?
Please help me. I am facing this prob from last 2 days.
This is my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
NSString *simpleTableIdentifier = [NSString stringWithFormat:#"%#%d",#"Cell",indexPath.row];
if (indexPath.row == 0)
{
cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
cell.alpha = 0.8;
}
}
else if (indexPath.row == 1)
{
cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
cell.alpha = 0.8;
}
}
else if(indexPath.row == 2)
{
cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
cell.alpha = 0.8;
}
}
else if (indexPath.row == 3)
{
cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
cell.alpha = 1.0;
}
}
return cell;
}
I hope this single line of code solve your issue
cell.contentView.alpha = 0.8
How about setting the cell background color with an alpha value?
cell.backgroundColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:0.8];
clearColor's grayscale and alpha = 0, so no matter what alpha you set, it won`t work, you have to have an actual colour so alpha to have visible efects
You should change the cell in this delegate method
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.alpha = indexPath.row < 3 ? 0.8 : 1.0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
NSString *simpleTableIdentifier = [NSString stringWithFormat:#"Cell"];
cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
UIView *lView_ = [[UIView alloc] init];
lView_ = [[UIView alloc] init];
lView_.backgroundColor = [UIColor colorWithRed:255.0 green:255.0 blue:255.0 alpha:0.6];
cell.backgroundView = lView_;
}
UIView *backview = (UIView *)cell.backgroundView;
if (indexPath.row == 3)
{
backview.backgroundColor = [UIColor colorWithRed:255.0 green:255.0 blue:255.0 alpha:1];
}
else
{
backview.backgroundColor = [UIColor colorWithRed:255.0 green:255.0 blue:255.0 alpha:0.2];
}
return cell;
}
Use the above and check wheather it is working or not.I hope this reolve your issue.

Change color of the text cell based on its value

I want to change the color of the text of the cells in the UITableView.
When I'm trying to do conditional formatting using if, it doesn't work.
Here is the code I'm trying:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return nomearray.count;
}
- (void)tableView: (UITableView*)tableView willDisplayCell: (UITableViewCell*)cell forRowAtIndexPath: (NSIndexPath*)indexPath
{
cell.textLabel.textAlignment = UITextAlignmentRight;
if(indexPath.row % 2 == 0) {
cell.backgroundColor = [UIColor colorWithRed:(240/255.0) green:(240/255.0) blue:(240/255.0) alpha:1];}
else {
cell.backgroundColor = [UIColor whiteColor];}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
if (risco == 1) {[cell.textLabel setTextColor: [UIColor colorWithRed:(60/255.0) green:(169/255.0) blue:(133/255.0) alpha:1]];} else {
if (risco == 2) {[cell.textLabel setTextColor: [UIColor colorWithRed:(255/255.0) green:(200/255.0) blue:(0/255.0) alpha:1]];} else {
if (risco == 3) {[cell.textLabel setTextColor: [UIColor colorWithRed:(150/255.0) green:(50/255.0) blue:(50/255.0) alpha:1]];}}}
cell.textLabel.text = [nomearray objectAtIndex:indexPath.row];
return cell;
}
I think the problem is your if condition is not satisfying Because integer values is not setting up properly. So just set the property of risco integer and then check, Did you try?

edit control and delete button overlap separator line in UITableView

I wanna keep separator line in edit mode of UITableView but don't know how to.
When edit control and edit button active, their frames are overlap the separator line like this:
I change background color for odd and even cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
TableCell *cell = (TableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[TableCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.text = [self.tableData objectAtIndex:indexPath.row];
if ((indexPath.row % 2) == 0) {
cell.contentView.backgroundColor = [UIColor grayColor];
cell.backgroundColor = [UIColor grayColor];
cell.selectedBackgroundView.backgroundColor = [UIColor grayColor];
}
cell.imageView.image = [UIImage imageNamed:#"noteCon"];
return cell;
}
And change background color for edit control and delete button in custom cell
- (void)willTransitionToState:(UITableViewCellStateMask)state{
[super willTransitionToState:state];
if (state == UITableViewCellStateShowingEditControlMask) {
[[self.subviews objectAtIndex:2] setBackgroundColor:self.contentView.backgroundColor];
}
if (state == 3) {
[[self.subviews objectAtIndex:3] setBackgroundColor:self.contentView.backgroundColor];
}
}
How to display separator line when tableView in edit mode? Thanks!

Resources