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;
}
Related
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.
I'm using below code to add a text label into a static table with 4 sections and 3 row in each.
for some reason, when I first run the app its fine, but when I scroll up and down, the UItable doesn't show up properly. Can someone tell why?
NSString *identifer = #"net";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer] ;
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
UILabel *Net= [[UILabel alloc] initWithFrame:CGRectMake(15, 15, 220.0, 15.0)];
Net.tag = indexPath.row;
Net.text = [NSString stringWithFormat:#"%ld",(long)indexPath.row];
Net.textColor= [UIColor blackColor];
Net.font=[UIFont systemFontOfSize:14.0];
[cell addSubview:Net];
}else {
UILabel *Label1 =(UILabel*) [cell viewWithTag:indexPath.row];
Label1.text =[NSString stringWithFormat:#"%d",indexPath.row];
Label1.font=[UIFont systemFontOfSize:14.0];
}
return cell;
I don't think you need and else statement while building your cell, this is an edited code that may work out for you:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 4;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifer = #"net";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer] ;
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
UILabel *Net= [[UILabel alloc] initWithFrame:CGRectMake(15, 15, 220.0, 15.0)];
// Net.tag = indexPath.row;
Net.text = [NSString stringWithFormat:#"%d",indexPath.row];
Net.textColor= [UIColor blackColor];
Net.font=[UIFont systemFontOfSize:14.0];
[cell addSubview:Net];
}
/*else {
UILabel *Label1 =(UILabel*) [cell viewWithTag:indexPath.row];
Label1.text =[NSString stringWithFormat:#"cell nr. %d",indexPath.row];
Label1.font=[UIFont systemFontOfSize:14.0];
}
*/
return cell;
}
I've commented your last lines of code so you won't lose them. If you also set the style of your TabelView as "Grouped", the result you'll get is the following:
http://s2.postimg.org/820thcmy1/i_OS_Simulator_Screen_shot_26_giu_2014_14_46_41.png
Hope this helps!
SOLUTION
Just read the answer of #Kjuly
Thanks alot
QUESTION
I used tableView with sections, each section has 4 rows, the first row it must show image from website which I used HJCache class to cache the image and avoid leaking/memory issues.
now, this code it works well and while I am scrolling fast it doesn't leak or make memory issue
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//UITableViewCell *cell=nil;
if (indexPath.row == 0) {
static NSString *CellIdentifier = #"Cell";
HJManagedImageV* mi;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
mi = [[HJManagedImageV alloc] initWithFrame:CGRectMake(0,0,cell.frame.size.width, 310)];
mi.tag = 999;
[cell addSubview:mi];
} else {
mi = (HJManagedImageV*)[cell viewWithTag:999];
[mi clear];
}
if (indexPath.row == 0) {
mi.image = [UIImage imageNamed:#"placeholder"];
mi.url = [NSURL URLWithString:[pictures objectAtIndex:indexPath.section]];
[objMan manage:mi];
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(likeTappedDouble:)];
tapped.numberOfTapsRequired = 2;
[mi setUserInteractionEnabled:YES];
[mi addGestureRecognizer:tapped];
}
return cell;
}
}
But when I try to configure other rows it leaked and while scrolling the application make memory issue and be very slowly.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//UITableViewCell *cell=nil;
if (indexPath.row == 0) {
static NSString *CellIdentifier = #"Cell";
HJManagedImageV* mi;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
mi = [[HJManagedImageV alloc] initWithFrame:CGRectMake(0,0,cell.frame.size.width, 310)];
mi.tag = 999;
[cell addSubview:mi];
} else {
mi = (HJManagedImageV*)[cell viewWithTag:999];
[mi clear];
}
if (indexPath.row == 0) {
mi.image = [UIImage imageNamed:#"placeholder"];
mi.url = [NSURL URLWithString:[pictures objectAtIndex:indexPath.section]];
[objMan manage:mi];
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(likeTappedDouble:)];
tapped.numberOfTapsRequired = 2;
[mi setUserInteractionEnabled:YES];
[mi addGestureRecognizer:tapped];
}
return cell;
}
static NSString *CellIdentifier = #"CellS";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
if (indexPath.row == 1) {
// configure row 1
}
if (indexPath.row == 2) {
// configure row 2
}
// etc for the others ..
return cell;
}
Where is the problem, Thanks..
UPDATE
This code doesn't work well, it add the subview in other row while scrolling
static NSString *CellIdentifier = #"CellS";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (indexPath.row == 1) {
UIImage* likesimage = [UIImage imageNamed:#"likespic"];
CGRect frameimg = CGRectMake(7, 5, likesimage.size.width, likesimage.size.height);
likesbutton = [[UIButton alloc] initWithFrame:frameimg];
[likesbutton setBackgroundImage:likesimage forState:UIControlStateNormal];
likesbutton.backgroundColor = [UIColor clearColor];
[cell addSubview:likesbutton];
label3 = [[UILabel alloc] initWithFrame:CGRectMake(20, 2, 100, 20)];
label3.textColor = [UIColor colorWithRed:61.0/255.0 green:113.0/255.0 blue:154.0/255.0 alpha:1.0];
label3.backgroundColor = [UIColor clearColor];
label3.font = [UIFont fontWithName:#"Helvetica-Bold" size:12];
label3.adjustsFontSizeToFitWidth = YES;
[cell addSubview:label3];
}
}
For your other cells, you need to reuse the cell either:
static NSString *CellIdentifier = #"CellS";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
EDIT for Updated Question:
You need to know how "cell reusing" works. As for your updated code, it said only row 1 needs the subviews like image, right? So you need add it outside of the if (cell == nil){} snippet, like the code below:
static NSString *CellIdentifier = #"CellS";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
if (indexPath.row == 1) {
UIImage* likesimage = [UIImage imageNamed:#"likespic"];
CGRect frameimg = CGRectMake(7, 5, likesimage.size.width, likesimage.size.height);
likesbutton = [[UIButton alloc] initWithFrame:frameimg];
[likesbutton setBackgroundImage:likesimage forState:UIControlStateNormal];
likesbutton.backgroundColor = [UIColor clearColor];
[cell addSubview:likesbutton];
label3 = [[UILabel alloc] initWithFrame:CGRectMake(20, 2, 100, 20)];
label3.textColor = [UIColor colorWithRed:61.0/255.0 green:113.0/255.0 blue:154.0/255.0 alpha:1.0];
label3.backgroundColor = [UIColor clearColor];
label3.font = [UIFont fontWithName:#"Helvetica-Bold" size:12];
label3.adjustsFontSizeToFitWidth = YES;
[cell addSubview:label3];
}
Note: It's better to create a new cell instance like what you did for row 0, cause it's only need to be created once.
try this ...
man you are doing 2 mistakes here
not checking for dequeued cells.
after allocating the cell , you are not releasing it's memory.
i suggest this code for you .
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"cellID";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}
}
don't forget to autorelease cells .
remove all add subviews, then add new subviews
NSArray *subviews = [[NSArray alloc] initWithArray:cell.contentView.subviews];
for (UIView *subview in subviews) {
[subview removeFromSuperview];
}
hello I am beginner in iOS I have created custom table view and I have Two array then I want to set both array data together in simultaneously one by one cell content in table view I show image which i want..
Please refer : http://i.stack.imgur.com/WIkaf.png
NSMutableArray *SearchPatientCode;
NSMutableArray *SearchPatientName;
UITableView *table_SearchPatient;
SearchPatientCode=[[NSMutableArray alloc]initWithObjects:#"PH230130420",#"PH230420321",#"Ph450362120", nil];
SearchPatientName=[[NSMutableArray alloc]initWithObjects:#"Rahul Sharma",#"Amit kumar",#"anil sharma", nil];
table_SearchPatient=[[UITableView alloc]initWithFrame:CGRectMake(130,40,170,250)style:UITableViewStylePlain];
table_SearchPatient.delegate=self;
table_SearchPatient.dataSource=self;
table_SearchPatient.layer.borderWidth = 2.0;
table_SearchPatient.layer.borderColor = [UIColor grayColor].CGColor;
[self.view addSubview:table_SearchPatient];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if(tableView==table_SearchPatient)
{
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] ;
}
cell.textLabel.text =[NSString stringWithFormat:#"%# /n %#",[SearchPatientCode objectAtIndex:indexPath.row],[SearchPatientName objectAtIndex:indexPath.row]];
cell.textLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:10.0f];
// cell.detailTextLabel.text=[SearchPatientName objectAtIndex:indexPath.row];
}
return cell;
}
I am using this But this is not show as I want ...Solve this problem!!
Add New UILable with numberOfLines = 2 like bellow...
if you want to unlimited line with your content data then you can do it with set numberOfLines = 0 and set lineBreakMode = UILineBreakModeWordWrap
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if(tableView==table_SearchPatient)
{
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] ;
}
UILabel *lbl = [[UILabel alloc]init];
lbl.text = [NSString stringWithFormat:#"%# /n %#",[SearchPatientCode objectAtIndex:indexPath.row],[SearchPatientName objectAtIndex:indexPath.row]];
lbl.font = [UIFont fontWithName:#"Helvetica-Bold" size:10.0f];
[lbl setFrame:CGRectMake(20, 2, 250, 35)];// set frame which you want
[lbl setBackgroundColor:[UIColor clearColor]];// set any color here
// lbl.lineBreakMode = UILineBreakModeWordWrap; // set it if you want to height of UILable with its content (Multiple line)
lbl.numberOfLines = 2;// Add this line // Set 0 if you want to multiple line.
[cell.contentView addSubview:lbl];
}
return cell;
}
You can use detail cell.detailTextLabel.text.
Or You can create a custom cell and add the labels to the cell like this
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] ;
}
cell.textLabel.text = #"";
label_name_one = [[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 90, 20)]autorelease];
[label_name_one setText:[SearchPatientCode objectAtIndex:indexPath.row]];
label_name_two = [[[UILabel alloc]initWithFrame:CGRectMake(92, 30, 170, 80)]autorelease];
[label_name_two setText:[SearchPatientCode objectAtIndex:indexPath.row]];
set the RectFrame according to your requirements.
Firstly change your cell style as UITableViewCellStyleValue2 then Use this code for multiline text in cell :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * cellIdentifier = #"MyIdentifier";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2
reuseIdentifier:cellIdentifier];
[[cell textLabel] setFont:[UIFont fontWithName:#"Helvetica-Bold" size:10.0f]];
}
[[cell detailTextLabel] setText:[NSString stringWithFormat:#"%# %#",[SearchPatientCode objectAtIndex:indexPath.row],[SearchPatientName objectAtIndex:indexPath.row]]];
cell.detailTextLabel.numberOfLines = 2;
cell.detailTextLabel.lineBreakMode = NSLineBreakByWordWrapping;
return cell;
}
Hope it helps you.
Also make sure you check out the free Sensible TableView framework. Given the arrays, the framework will automatically display all the values in the table view, and will even generate detail views where applicable. Saves me tons of time.
I need to display the cell data. Actually if I debug the code I can see that the cell.label.text does have correct value, but nothing is getting displayed on the simulator. I have set the row count in numberOfRowsInSection method.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = #"Cell";
LogTableCell *cell = (LogTableCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
CGRect frame1 = [ self.view frame];
frame1.origin.x=22.0f;
frame1.origin.y=55.0f;
frame1.size.width = 298.0f;
frame1.size.height= 425.0f;
[self.view.superview setFrame:frame1];
int btnWidth = ((self.view.frame.size.width-16)/3);
if(cell == nil)
{
cell = [[LogTableCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
LogItem *lg = [[LogItem alloc]init];
lg = [arrLogs objectAtIndex:indexPath.row ];
NSLog(#"%d",indexPath.row);
cell.lblname.textColor=[UIColor whiteColor];
cell.lblsubtype.textColor=[UIColor whiteColor];
cell.lbltime.textColor=[UIColor whiteColor];
cell.lblname.font=[UIFont fontWithName:#"Merriweather" size:15.0];
cell.lblsubtype.font=[UIFont fontWithName:#"Merriweather-Light" size:13.0];
cell.lbltime.font=[UIFont fontWithName:#"Merriweather-Light" size:13.0];
UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake(1, cell.contentView.frame.size.height, 298.0,2)] ;
imageview.image = [UIImage imageNamed: #"lineseperator.png"];
[cell.contentView addSubview:imageview];
cell.lblname.text = lg.name;
NSLog(#"%#",lg.name);
[[cell imgtype] setImage:[UIImage imageNamed:lg.imagetype]];
if([lg.subtype length] == 0){
NSLog(#"%#",lg.subtype);
cell.lblsubtype.text = lg.time;
cell.lbltime.text = #"";
}
else{
NSLog(#"%#",lg.subtype);
cell.lblsubtype.text = lg.subtype;
cell.lbltime.text = lg.time;
}
return cell;
}
The imageview you're adding to the cell is covering all other content. You'll need to either resize/reposition it, or just use the already existing cell.imageView instead.