UITableViewCell Font Not Changing - iphone

I am trying to customize the font of a UITableViewCell using the following code for when the tableview is populated.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}
// Set up the cell
int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
NSString *temp = [[NSString alloc] initWithString:[[stories objectAtIndex: storyIndex] objectForKey: #"title"]];
cell.textLabel.text = temp;
[[cell textLabel] setTextColor:[UIColor colorWithRed:154.0/255.0 green:14.0/255.0 blue:2.0/255.0 alpha:1]];
[[cell textLabel] setFont:[UIFont systemFontOfSize:12.0]];
return cell;
}
For the life of me I don't know why it won't change the font! Also the above code works fine if I hard code in what the cell text is such as cell.textLabel.text = #"TEST";
Any suggestions? Thanks!

First, you should autorelease your cell. You are leaking memory like crazy presently.
Second, you should update the font in tableView:willDisplayCellForRow:atIndexPath:. If you are using a standard table view, it will make changes to your cells (at random times) and you will need to do things like font changes, background color, etc in the tableView:willDisplayCellForRow:atIndexPath: instead of in the data source method.
See also this thread: What is -[UITableViewDelegate willDisplayCell:forRowAtIndexPath:] for?

#Jason Coco:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
}

Just you can try with following code.
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellString = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellString];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellString];
}
cell.textLabel.text = values;
cell.textLabel.textColor = [UIColor colorWithRed:154.0/255.0 green:14.0/255.0 blue:2.0/255.0 alpha:1];
cell.textLabel.font = [UIFont systemFontOfSize:12.0];
return cell;
}

Try,
cell.textLabel.font = [UIFont systemFontOfSize:12.0];
cell.detailTextLabel.font = [UIFont systemFontOfSize:10.0];

willDisplayCell:forRowAtIndexPath: can be used for modifying the cell's background color, content view, etc, except the backgroundColor property of textlabel and detailtextlabel
Its always a good practice to do these modifications in the above method

very simple, `
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
cell.textLabel.textColor = [UIColor grayColor]; //redColor greenColor etc.
}

Related

How to set a color for a Cell on a TableView?

I want the whole cell printed in blue but it only shows a little band.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
[cell.contentView setBackgroundColor:[UIColor blueColor]];
cell.textLabel.text = [NSString stringWithFormat:#"%#", [cat objectAtIndex:indexPath.row]];
return cell;}
My screen capture is this:
add this line after [cell.contentView setBackgroundColor:[UIColor blueColor]]; line:
cell.textLabel.backgroundColor = [UIColor clearColor];
Because of the way UITableView changes a cell's background color during selection, you must implement tableView:willDisplayCell:forRowAtIndexPath: and set the cell background there. You should set the background of the whole cell rather than just its contentView, otherwise the accessory views will not be highlighted.
- (void)tableView:(UITableView *)tableView
willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath
{
// ...
cell.backgroundColor = ...;
}
You may also need to make the various subviews in the cell have transparent background colors.
cell.titleLabel.backgroundColor = [UIColor clearColor];
cell.titleLabel.opaque = NO;
Use this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
[cell.contentView setBackgroundColor:[UIColor blueColor]];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.text = [NSString stringWithFormat:#"%#", [cat objectAtIndex:indexPath.row]];
return cell;}
Answer : tableView:willDisplayCell:forRowAtIndexPath:
- (void)tableView:(UITableView *)tableView
willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.backgroundColor = [UIColor blueColor];
}

ios - tableview Accessory indicator not shown

in my tableview i use a background image for my cells (totally black image) and i also want to use accessory indicator in my cells. in cell's properties window i set AccessoryDisclosureIndicator
in my .m file i put these codes for tableview:
- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellAccessoryDisclosureIndicator;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.backgroundColor = [UIColor clearColor];
}
cell.textLabel.backgroundColor=[UIColor clearColor];
cell.textLabel.text=[arrMagaza objectAtIndex:indexPath.row];
cell.textLabel.textColor=[UIColor whiteColor];
NSString *distance=[arrDistance objectAtIndex:indexPath.row];
cell.detailTextLabel.backgroundColor=[UIColor clearColor];
cell.detailTextLabel.textColor=[UIColor whiteColor];
cell.detailTextLabel.text=[distance stringByAppendingFormat:#" km"];
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"tablecell.png"]];
tableView.backgroundColor=[UIColor blackColor];
tableView.separatorColor= [UIColor clearColor];
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
}
but the accessory indicator not shown on the view when i run the app.
what am i doing wrong? is the problem about background image that i use for my cells?
- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath
This method is deprecated now, so no need to implement this method. Try removing everything else looks fine.

Xcode UITableView Row not highlighted when selected

I have a working table view on my iphone app, and am implementing a second view using navigationController.
But when I try to select a cell on the initial view, it does not respond i.e. no highlighting in blue, no selection.
Can anyone suggest what I might have missed, or what is responsible for actually selecting the cell when the user taps it?
OK thanks for your suggestions - still no luck. here is my code:
- (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]autorelease];
[cell setSelectionStyle: UITableViewCellSelectionStyleBlue];
UIView *selectionView = [[[UIView alloc] init] autorelease];
selectionView.backgroundColor =[UIColor redColor];
cell.selectedBackgroundView = selectionView;
}
// format text
cell.textLabel.font = [UIFont fontWithName:#"Arial" size:12];
cell.textLabel.text = [tableArray objectAtIndex:indexPath.row];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//NSUInteger row = indexPath.row;
//[DetailView selectRowAtIndexPath:indexPath animated:YES];
//UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath.row];
//[cell setSelectionStyle: UITableViewCellSelectionStyleBlue];
DetailView *detailView = [[DetailView alloc] initWithNibName:#"DetailView" bundle:nil];
[self.navigationController pushViewController:detailView animated:YES];
[DetailView release];
}
May be you have done this line of code in cellForRowatIndexPath::
cell.selectionStyle = UITableViewCellSelectionStyleNone;
If yes, then replace it with either of these:
cell.selectionStyle = UITableViewCellSelectionStyleGray;
or
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
Without looking at your code I would say you have to add this line in your cellForRowAtIndexPath delegate method.
[cell setSelectionStyle:UITableViewCellSelectionStyleBlue];
And also you have to implement this delegate method which gets called when you tap on cell of UITableView..
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}

UITableView cell text not showing?

I'm not seeing the text for my UITableView show up.. in fact the TableView doesn't appear to be on the screen at all because I cannot select the empty rows.
Strangely, my log in cellForRowAtIndexPath shows up ok with the expected data - I just don't see any on the screen.
-(void)bindFriendsToTable:(NSArray *)friends{
NSLog(#"friends : %#",friends);
[sections removeAllObjects];
[sectionRows removeAllObjects];
[sections addObject:#"Friends"];
[sectionRows setObject:friends forKey:#"Friends"];
[self.tableView reloadData];
HideActivityIndicator();
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellsection = [sections objectAtIndex:indexPath.section];
NSArray *rowsInSection = [sectionRows valueForKey:cellsection];
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.font = [UIFont fontWithName:#"Helvetica" size:14];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
cell.textLabel.text = [rowsInSection objectAtIndex:indexPath.row];
NSLog(#"%#",cell.textLabel.text);
return cell;
}
I'm inheriting from a base class which is a UITableViewController, as I have for a dozen other screens in the project. Any idea why I'm not seeing the tableView?
Is your font colour the same as your background colour? If you set the accessoryType to a checkmark does it display?
cell.accessoryType = UITableViewCellAccessoryCheckmark;
Do you have any header sections? If so make sure you return a value for heightForHeaderInSection:
-(float)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 66;
}
i think u should remove
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
and then try it.
and display the other static string in the cell to check the problem.

Why don't tablecells get released?

I simplified my code to test with, and still on the phone my memory usage keeps climbing to a point where the table slows way down.
Can someone tell me what I'm doing wrong here?
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellID = #"Cell";
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellID] autorelease];
}
UILabel *l=[[UILabel alloc] initWithFrame:CGRectMake(10,10,300,16)];
l.font=[UIFont boldSystemFontOfSize:15];
l.textColor=[UIColor whiteColor];
l.backgroundColor=[UIColor blackColor];
l.text=#"Just some randoom text here";
[cell.contentView addSubview:l];
[l release];
Oops. That code paste didn't work too well. Here's a straight paste:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellID = #"Cell";
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellID] autorelease];
}
UILabel *l=[[UILabel alloc] initWithFrame:CGRectMake(10,10,300,16)];
l.font=[UIFont boldSystemFontOfSize:15];
l.textColor=[UIColor whiteColor];
l.backgroundColor=[UIColor blackColor];
l.text=#"Just some randoom text here";
[cell.contentView addSubview:l];
[l release];
return cell;
}
You will want to follow a pattern like this:
#define kTagMyLabel 1
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellID = #"Cell1";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellID];
UILabel * l;
if (cell == nil) {
// create the cell
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellID] autorelease];
// perform setup/functions that are common to all cells
l = [[[UILabel alloc] initWithFrame:CGRectMake(10,10,300,16)] autorelease];
l.font=[UIFont boldSystemFontOfSize:15];
l.textColor=[UIColor whiteColor];
l.backgroundColor=[UIColor blackColor];
l.tag = kTagMyLabel ;
[cell.contentView addSubview:l];
}
else
{
// find the label we previously added.
l = (UILabel*)[cell viewWithTag:kTagMyLabel];
}
// now set up the cell specific to this indexPath
l.text=#"Just some random text here";
return cell;
}
You're recycling UITableViewCell instances, but you're still creating a new UILabel instance for every row, and adding it to each and every cell. This is where the memory usage comes from. Try something like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellID = #"Cell";
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellID] autorelease];
UILabel *l=[[UILabel alloc] initWithFrame:CGRectMake(10,10,300,16)];
l.font=[UIFont boldSystemFontOfSize:15];
l.textColor=[UIColor whiteColor];
l.backgroundColor=[UIColor blackColor];
l.text=#"Just some randoom text here";
[cell.contentView addSubview:l];
[l release];
}
return cell;
}
Since every UITableViewCell has its own standard UILabel (cell.textLabel), do you really need that extra label added to the contentView? If you need custom cells, you might want to consider subclassing UITableViewCell.