UILabel in TableCell never changes font or colour - ios

I've been trying to find a solution for this for quite some time now but with no luck. I've got a TableViewController with a number of Cells, all of which have a UILabel with some text. I'm trying to change the font and colour with the following code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [menuItems objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.font = [UIFont fontWithName:#"Times New Roman" size:12.0f];
cell.textLabel.textColor = [UIColor redColor];
return cell;
}
This code should be correct to set the font and colour to a UILabel but it just never changes. Maybe there's a setting I need to change from the Interface Builder? Maybe an attribute setting the I need to disable?
The odd part is that even from the Interface Builder the fonts and colours simply never change.

Try the following code:-
#interface TestTableViewController ()<UITableViewDataSource,UITableViewDelegate>
#property (weak, nonatomic) IBOutlet UITableView *tableView;
#end
#implementation TestTableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.delegate=self;
self.tableView.dataSource=self;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 5;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString * reuseIdentifier = nil ;
UITableViewCell* cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
}
cell.textLabel.font = [UIFont fontWithName:#"Times New Roman" size:12.0f];
cell.textLabel.textColor = [UIColor redColor];
cell.textLabel.text = [NSString stringWithFormat:#"Number: %d",indexPath.row];
return cell;
}

Related

iOS TableView Referencing outlet

I can not connect referencing outlet to showTableView on storyboard. I can only connect to view .I was trying to show the tableview with some data. Here are the codes that I am using.Please help me.Thank you.
ViewController.h
#interface ViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>
#property(nonatomic, strong) IBOutlet UITableView *showTableView;
ViewController.m
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [jsonResults count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *simpleIdentifier = #"SimpleIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:simpleIdentifier];
}
// Configure the cell...
NSDictionary *appsdict = [jsonResults objectAtIndex:indexPath.row];
NSString *nameString = [appsdict objectForKey:#"name"];
cell.textLabel.text = [appsdict objectForKey:#"name"];
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.font = [UIFont boldSystemFontOfSize:12];
return cell;
}
Make sure you've set your UIViewController's Custom Class to ViewController in the interface so you can properly link its outlets (including showTableView) to ViewController.

UITableView textlabel frame not showing proper

I developed iPhone app in which i have UITableView.
UITableViewCell textlabel frame is not proper according to text,
when i write this code, UITableView textlabel is not proper.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CategCellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CategCellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CategCellIdentifier];
}
cell.textLabel.backgroundColor=[UIColor redColor];
cell.textLabel.font= [UIFont systemFontOfSize:12.0];
cell.textLabel.textAlignment = NSTextAlignmentCenter;
cell.textLabel.text = [tempArr objectAtIndex:indexPath.row];
return cell;
}
when i pass my array to UITableView it shows like this:
Whats the problem ? where i am doing mistake, please help
I am not sure what text your array contains. But, I have created an example of my own. Here is the code :
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CategCellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CategCellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CategCellIdentifier];
}
cell.textLabel.backgroundColor = [UIColor redColor];
cell.textLabel.font= [UIFont systemFontOfSize:20.0];
cell.textLabel.textAlignment = NSTextAlignmentCenter;
cell.textLabel.text = #"Brand";
return cell;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.textLabel.backgroundColor = [UIColor redColor];
}
#end
Here is the output :
I have used your code. Only difference I made is, I am applying red colour to text label in willDisplayCell: method of UITableViewDelegate.
Coming to the margins that appears on both sides of the tableview are as per Apple's UI guidelines.
Let me know if you need my code.
Try to set Size to fit with content After setting Text.
[cell.textLabel sizeToFit];
Thanks.
Check table row height property in size inspector, may be some thing wrong with it
[tableView setRowHeight: 100.00];
or you can use delegate to set row height
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

Trouble with adding UILabel to UITableViewCell

I have some trouble with adding UILabel as subview to UITableViewCell. My code:
bookMarkTable=[[UITableView alloc]initWithFrame:CGRectMake(0, 50, 480, 270)];
bookMarkTable.rowHeight = 38.0f;
bookMarkTable.backgroundColor=[UIColor colorWithRed:247/255.0 green:246/255.0 blue:242/255.0 alpha:1];
[bookMarkTable setDelegate:self];
[bookMarkTable setDataSource:self];
[bookMarkMainView addSubview:bookMarkTable];
and UITableView delegate methods:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return bookMarksArray.count;
}
- (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];
}
// Configure the cell...
NSMutableDictionary* cellDictionary =[bookMarksArray objectAtIndex:indexPath.row];
UILabel* bookMarkTitle =[[UILabel alloc]initWithFrame:CGRectMake(50, 0, 370, 40)];
bookMarkTitle.text=[cellDictionary valueForKey:#"bm_title"];
bookMarkTitle.backgroundColor=[UIColor clearColor];
bookMarkTitle.font=[UIFont fontWithName:#"Georgia" size:20.0f];
[cell.contentView addSubview:bookMarkTitle];
return cell;
}
- (CGFloat) tableView: (UITableView *) tableView heightForRowAtIndexPath: (NSIndexPath *) indexPath{
return 38.0f;
}
I add rows via this method
-(void)addBookmark{
NSMutableDictionary* infoDict=[[NSMutableDictionary alloc]init];
NSString* bookmarkTitle=[NSString stringWithFormat:#"This is :[%i]",bookMarksArray.count];
[infoDict setValue:bookmarkTitle forKey:#"bm_title"];
[bookMarksArray addObject:infoDict];
[bookMarkTable reloadData];
}
But when i invoke this method multiply times it seems that all UILabel are added more than once. Here is a result
Any help is appreciated !
What's happening is that each time you move the table, the method is called and adds a new UILabel to the cell.
You should create a subclass of UITableViewCell and add the label there.
Other option, since you are just using one label, is to use the default label of UITableViewCell.
Change
UILabel* bookMarkTitle =[[UILabel alloc]initWithFrame:CGRectMake(50, 0, 370, 40)];
bookMarkTitle.text=[cellDictionary valueForKey:#"bm_title"];
bookMarkTitle.backgroundColor=[UIColor clearColor];
bookMarkTitle.font=[UIFont fontWithName:#"Georgia" size:20.0f];
[cell.contentView addSubview:bookMarkTitle];
to
cell.textLabel.text = [cellDictionary valueForKey:#"bm_title"];
At the begin from - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath you must remove the subviews.
for(UIView* vista in cell.contentView){
[vista removeFromSuperview];
}
And that's all.
PD: Check if the removed view is the correct.
Try to change:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
with:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];

CustomCell without customCell class with storyboard

I would to create a custom cell without create a custom cell class. I know that this is possible. I've created in storyboard a tableviewcontroller with a prototype cell with a label. In the attibutes I've set the cell name "mycell". And the code that I used is:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ static NSString *CellIdentifier = #"mycell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{ cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];}
UILabel *title = (UILabel*) [cell viewWithTag:1];
title.text = #"Hi";
return cell;
}
But when my app run I see only a empty table without a cell with my label.
In the tableView methods I've used:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1;}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5; }
Thank you.
If you are adding UILabel programmatically it needs to be alloc init, should be given a frame and added to the cell as sub view.
UILabel *title = (UILabel*) [cell viewWithTag:1];
title.text = #"Hi";
return cell;
Should be
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(50, 10, 50, 20)];
title.text = #"Hi";
[cell addSubview:title];
return cell;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath is not being called

So its really weird... I have a ViewController called ListEventsViewController
in the header file:
#interface ListEventsViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
#property (weak, nonatomic) IBOutlet UITableView *eventsTable;
#property (strong, nonatomic) NSArray *events;
#end
In the Implementation I am calling the pretty much mandatory functions below:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [events count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = nil;
cell = [eventsTable dequeueReusableCellWithIdentifier:#"eventCell"];
if(!cell){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"eventCell"];
}
NSString *textLabelTitle = [[events objectAtIndex:indexPath.row] objectForKey:#"name"];
NSString *textLabelDetail = [[events objectAtIndex:indexPath.row] objectForKey:#"date"];
//cell.imageView.image = someimage;
NSLog(#"Text Label Title: %#", textLabelTitle);
NSLog(#"Text Label Detail: %#", textLabelDetail);
cell.textLabel.text = textLabelTitle;
cell.detailTextLabel.text = textLabelDetail;
return cell;
}
The problem is the function - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ is not being called so the cells arent being populated. also the number of cells isnt being allocated either. What could be causing this?
It looks wired up to me heres a picture of the h and IB.
You illustrated in your screenshot that you have linked the table to its IBOutlet, but you haven't linked the table view's delegate or datasource to your controller and therefore your tables delegate methods such as cellForRowAtIndex will not be called. Linking these should fix your problem.
I have this & this must b your solution
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
mysampleimage = [[UIImageView alloc]initWithFrame:CGRectMake(3,3, 40, 40)];
mysampleimage.image=[UIImage imageNamed:#"a.png"];
mysampleimage.tag=13;
[cell.contentView addSubview:mysampleimage];
myButton = [[UIButton alloc]init];
myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(165, 10, 65, 30.);
[myButton setTitle:#"Show" forState:UIControlStateNormal];
myButton.tag = 14;
[cell addSubview:myButton];
lbl = [[UILabel alloc]initWithFrame:CGRectMake(50, 6, 200, 43)];
lbl.tag=12;
[cell.contentView addSubview:lbl];
}
else
{
lbl=(UILabel *)[cell.contentView viewWithTag:12];
mysampleimage=(UIImageView *)[cell.contentView viewWithTag:13];
myButton = (UIButton *)[cell.contentView viewWithTag:14];
}
myButton.accessibilityLabel = lbl.text;
[myButton addTarget:self action:#selector(myShow:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
You can use the following lines in your coding:
tableview.delegate = self;
tableview.datasource = self;
What helped me was to comment out
numberofsectionsintableview

Resources