UITableViewCell textLabel color not changing - ios

I've got a UITableView and as the cellForRowAtIndexPath method I am changing some attributes of the cell (font, size, etc.) Now all of the assignments listed below work just fine except changing the color of the textLabel. I can't figure out why only that specific color attribute won't change. I've looked about everywhere I can think of to figure out why it isn't working and I'm stuck. Any ideas?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *kLocationAttributeCellID = #"bAttributeCellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kLocationAttributeCellID];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:kLocationAttributeCellID] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
cell.detailTextLabel.font = [UIFont fontWithName:#"Helvetica" size:14.0];
cell.detailTextLabel.numberOfLines = 0;
cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.userInteractionEnabled = NO;
cell.textLabel.font = [UIFont fontWithName:#"Courier" size:18.0];
cell.textLabel.textColor = [UIColor redColor]; // this never takes effect...
}
cell.textLabel.text = #"Test Label";
cell.detailTextLabel.text = #"Test Details";
return cell;
}

It's because of this:
cell.userInteractionEnabled = NO;
If you don't want your cells to be selectable, try using this instead:
cell.selectionStyle = UITableViewCellSelectionStyleNone;

Related

Labels do not fit into TableView

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;
}

How to set the text and detail text of cell in tableview?

I am getting this type of data inside cell. How to set the text and detail text of cell so that both text may appear in readable format.
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.font=[UIFont fontWithName: #"Arial" size: 14.0 ];
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
[cell setBackgroundColor:[UIColor whiteColor]];
UILabel *idLabel = (UILabel*)[cell.contentView viewWithTag:201];
if(!idLabel)
{
idLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 50)];
cell.textLabel.textColor = [UIColor blackColor];
idLabel.tag = 201;
[cell.contentView addSubview:idLabel];
}
idLabel.text = [arrayFiltered objectAtIndex:indexPath.row];
cell.textLabel.text = [idArray objectAtIndex:indexPath.row];
cell.textLabel.textColor = [UIColor grayColor];
cell.detailTextLabel.text = [descArray objectAtIndex:indexPath.row];
cell.detailTextLabel.textColor = [UIColor blueColor];
cell.detailTextLabel.numberOfLines = 2;
cell.detailTextLabel.adjustsFontSizeToFitWidth = YES;
return cell;
}
The code which i have written is above.
Please help me.
We have 3 or more options to achieve this.I explain you step by step process
I create the tableView and hook up.
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
#property (strong, nonatomic) IBOutlet UITableView *tblView;
#end
ViewController.m
#import "ViewController.h"
#import "CustomCell.h" // For CustomCell using,we have to import.
#interface ViewController ()
{
NSMutableArray *idArray;
NSMutableArray *descArray;
}
#end
#implementation ViewController
In viewDidLoad method I added the objects to id desc array.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
idArray = [[NSMutableArray alloc]initWithObjects:#"INOX",#"Sathyam",#"PVR",#"IMAX",nil];
descArray = [[NSMutableArray alloc]initWithObjects:#"INOX currently operates 107 multiplexes and 420 screens in 57 cities across India",#"SPI Cinemas is an Indian multiplex chain and film production company owned by the SPI Group, headquartered in Chennai.",#"The IMAX cinema process increases the image resolution by using larger film frame;",#"PVR Cinemas is the largest and the most premium film entertainment company in India.",nil];
}
Below tableView datasource methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return idArray.count;
}
Now I have 3 options for achieving your requirement.But all these are implemented in cellForRowAtIndexPath method.
FIRST OPTION:UITableViewCellStyleDefault-If you use this, you can implement the code in cellForRowAtIndexPath like below
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *strCell = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strCell];
if(cell==nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strCell];
}
UILabel *idLabel = [[UILabel alloc]init];
idLabel.frame = CGRectMake(15, 13, 168, 24);
idLabel.tag = indexPath.row;
idLabel.numberOfLines = 1;
idLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:18];
idLabel.textAlignment = NSTextAlignmentLeft;
idLabel.textColor = [UIColor blackColor];
idLabel.text = [NSString stringWithFormat:#"%#",[idArray objectAtIndex:indexPath.row]];
[cell.contentView addSubview:idLabel];
UILabel *descLabel = [[UILabel alloc]init];
descLabel.frame = CGRectMake(15, 47, 348, 34);
descLabel.tag = indexPath.row;
descLabel.numberOfLines = 2;
descLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:12];
descLabel.textAlignment = NSTextAlignmentLeft;
descLabel.textColor = [UIColor blueColor];
descLabel.text = [NSString stringWithFormat:#"%#",[descArray objectAtIndex:indexPath.row]];
[cell.contentView addSubview:descLabel];
return cell;
}
SECOND OPTION:UITableViewCellStyleSubtitle - If you use this, you can implement the code in cellForRowAtIndexPath like below
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *strCell = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strCell];
if(cell==nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:strCell];
}
cell.textLabel.text = [NSString stringWithFormat:#"%#",[idArray objectAtIndex:indexPath.row]];
cell.textLabel.tag = indexPath.row;
cell.textLabel.numberOfLines = 1;
cell.textLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:18];
cell.textLabel.textAlignment = NSTextAlignmentLeft;
cell.textLabel.textColor = [UIColor blackColor];
cell.detailTextLabel.text = [NSString stringWithFormat:#"%#",[descArray objectAtIndex:indexPath.row]];
cell.detailTextLabel.tag = indexPath.row;
cell.detailTextLabel.numberOfLines = 2;
cell.detailTextLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:12];
cell.detailTextLabel.textAlignment = NSTextAlignmentLeft;
cell.detailTextLabel.textColor = [UIColor blueColor];
return cell;
}
THIRD OPTION : CUSTOM CELL - If you use Custom Cell you have to import the CustomCell.h first.After that you have to do the following things in cellForRowAtIndexPath method
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:#"cell"];
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
if(cell == nil){
cell = nib[0];
}
cell.idLabel.text = [NSString stringWithFormat:#"%#",[idArray objectAtIndex:indexPath.row]];
cell.idLabel.tag = indexPath.row;
cell.idLabel.numberOfLines = 1;
cell.idLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:18];
cell.idLabel.textAlignment = NSTextAlignmentLeft;
cell.idLabel.textColor = [UIColor blackColor];
cell.descLabel.text = [NSString stringWithFormat:#"%#",[descArray objectAtIndex:indexPath.row]];
cell.descLabel.tag = indexPath.row;
cell.descLabel.numberOfLines = 2;
cell.descLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:12];
cell.descLabel.textAlignment = NSTextAlignmentLeft;
cell.descLabel.textColor = [UIColor blueColor];
return cell;
}
I attached the screen shot of custom cell.It shows you what I have done there.
SNAPCHAT 1:idLabel
SNAPCHAT 2:descLabel
You can use heightForRowAtIndexPath method for setting row height.It is UITableViewDelegate method
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 90;
}
NOTE:Generally I set the tag for label is idLabel.tag = indexPath.row.According
to your requirement set the tag here.
I created and worked out the sample application for you.All I have done successfully.
Use cell textLable and detailTextLable to show your data like below code-
cell.textLabel.text=#"your text";
cell.detailTextLabel.text=#"your text";
idLabel overrides the position of the textlabel .
If
you want to put the id before the text , You can combine both the string and put into the textlabel.
else
use dont use textlabel and detail textlabel, create all the three labels of your own and align properly using Frame values.
Check the Height of the Cell and calculate it according to the Cell Text and number of lines of Label. And move your Font, Background Color, Line Break settings of label inside cell == nil.
Edit: to adjust three Labels you would need custom Cell or if you can concatenate it with Text Label. It all depends on your requirement.

Changing a TableView cell colour will not work

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];

IOS cell.textLabel.textColor doesn't change cell text color [duplicate]

I've got a UITableView and as the cellForRowAtIndexPath method I am changing some attributes of the cell (font, size, etc.) Now all of the assignments listed below work just fine except changing the color of the textLabel. I can't figure out why only that specific color attribute won't change. I've looked about everywhere I can think of to figure out why it isn't working and I'm stuck. Any ideas?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *kLocationAttributeCellID = #"bAttributeCellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kLocationAttributeCellID];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:kLocationAttributeCellID] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
cell.detailTextLabel.font = [UIFont fontWithName:#"Helvetica" size:14.0];
cell.detailTextLabel.numberOfLines = 0;
cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.userInteractionEnabled = NO;
cell.textLabel.font = [UIFont fontWithName:#"Courier" size:18.0];
cell.textLabel.textColor = [UIColor redColor]; // this never takes effect...
}
cell.textLabel.text = #"Test Label";
cell.detailTextLabel.text = #"Test Details";
return cell;
}
It's because of this:
cell.userInteractionEnabled = NO;
If you don't want your cells to be selectable, try using this instead:
cell.selectionStyle = UITableViewCellSelectionStyleNone;

Having trouble overriding uitableviewcell properties

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.

Resources