how can add a label to every row in tableview in iphone - iphone

I want add label to every row in tableview at right side of the row in iphone programmatically.
Can anyone help me?

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
UILabel* lbl = [[UILabel alloc] initWithFrame:CGRectMake(x, y, w, h)];
lbl.font = [UIFont boldSystemFontOfSize:17.0];
lbl.text = [self.arrRows objectAtIndex:indexPath.row];
[cell.contentView addSubview:lbl];
[lbl release];
return cell;

You can achieve it 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:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
UILabel *lblText = [[UILabel alloc] initWithFrame:CGRectMake(200, 3, 100, 15)]; // For right alignment
lblText.text = [self.arrText objectAtIndex:indexPath.row];
lblText.textColor = [UIColor blackColor];
[cell addSubview:lblText];
[lblText release];
return cell;
}

I think you are asking for code.
try
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UILabel *yourLabel = nil;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
yourLabel = [[UILabel alloc] initWithFrame:CGRect……];
[yourLabel setTag:9999];
[[cell contentView] addSubview:addressLabel];
}
if (!yourLabel)
yourLabel = (UILabel*)[cell viewWithTag:9999];
return cell;
}

In your cellForRowAtIndexPath: method :-
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier=#"Cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
//set reuseIdentifier:nil for avoid label overlapping
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
}
//make your label as according to you
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
label.backgroundColor = [UIColor redColor];
// add label content view of cell
[cell.contentView addSubview:label];
return cell;
}

Related

TableView data's are disappear while scrolling?

I tried so many examples But doesn't works.Please check my code and give solutions.Thanks in advance.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier];
}
if (indexPath.section==0)
{
img=[UIButton buttonWithType:UIButtonTypeRoundedRect];
img.frame=CGRectMake(25, 15, 75, 75);
img.layer.cornerRadius=6.0;
img.clipsToBounds=YES;
img.layer.borderColor=[[UIColor grayColor]CGColor];
[img setImage:[UIImage imageNamed:#""] forState:UIControlStateNormal];
[cell.contentView addSubview:img];
[img addTarget:self action:#selector(takePic:) forControlEvents:UIControlEventTouchDown];
img.titleLabel.textColor=[UIColor blackColor];
text=[[UITextField alloc]initWithFrame:CGRectMake(105, 30, 200, 30)];
text.placeholder=#"Name";
[text setBorderStyle:UITextBorderStyleRoundedRect];
[cell addSubview:text];
[cell addSubview:img];
else if (indexPath.section==1)
{
UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(20, 15, 100, 20)];
label.text=[dosageArray objectAtIndex:indexPath.row];
label.backgroundColor=[UIColor clearColor];
label.font=[UIFont boldSystemFontOfSize:15.0];
label.textColor=[UIColor blackColor];
[cell addSubview:label];
return cell;
}}
I have a three section in the tableview.In first Section I have a textfield and imageview If I scroll the textfield and imageview will disappear that is the problem
Note : if you have limited data then use SOLUTION - 1 because this is bad for memory management , Other wise SOLUTION - 2 is good for you. (Solution - 1 may be helpful for u)
SOLUTION - 1
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:#"S%1dR%1d",indexPath.section,indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = #"NameOFData ";
return cell;
}
SOLUTION - 2
- (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.textLabel.text = #"NameOFData ";
return cell;
}
EDITED :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:#"S%1dR%1d",indexPath.section,indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
if (indexPath.section==0)
{
img=[UIButton buttonWithType:UIButtonTypeRoundedRect];
img.frame=CGRectMake(25, 15, 75, 75);
img.layer.cornerRadius=6.0;
img.clipsToBounds=YES;
img.layer.borderColor=[[UIColor grayColor]CGColor];
[img setImage:[UIImage imageNamed:#""] forState:UIControlStateNormal];
[cell.contentView addSubview:img];
[img addTarget:self action:#selector(takePic:) forControlEvents:UIControlEventTouchDown];
img.titleLabel.textColor=[UIColor blackColor];
text=[[UITextField alloc]initWithFrame:CGRectMake(105, 30, 200, 30)];
text.placeholder=#"Name";
[text setBorderStyle:UITextBorderStyleRoundedRect];
[cell addSubview:text];
[cell addSubview:img];
}
}
cell.textLabel.text = #"NameOFData ";
return cell;
}
Try this code add NSMutableArray to all.Here displayArray is NSMutableArray.
text.text=[displayArray objectAtIndex:1];
remove cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier]; from else part & leave it blank
Enjoy Programming!!
Please try to use this one...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier];
}
return cell;
}
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = #"Hello";
return cell;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = #""// value from your datasource
return cell;
}
SOLUTION 1
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier];
//your code
}
SOLUTION 2
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier];
}
cell.textLabel.text= #"YOUR TEXT";
}
Hope this will help you.
All the best !!!

How to remove the background image from a UITableViewCell?

In my app I am having UITableView in one of my UIView.I added a background image for all cells. My problem is , when I select a particular cell,I want to change the background image of that particular cell alone and all the other cells should have a old background image.
How can i do this. Please share your ideas.
Here is my cellForRowAtIndexPath 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];
}
// Configure the cell.
UIImageView *bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"MenuItem3"]];
cell.backgroundView = bgView;
cell.textLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:14];
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.text = [arrayValue objectAtIndex:indexPath.row];
[cell setAccessoryType:UITableViewCellAccessoryNone];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
No Need to reload all your table just reload PreviousInxed 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:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"bck.png"]];
UITableViewCell *celld = (UITableViewCell *)[tableView cellForRowAtIndexPath:table.indexPathForSelectedRow];
celld.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"raj_star.png"]];
return cell;
}
NSIndexPath *perviouseIndexPathaf;
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (perviouseIndexPathaf)
{
[table reloadRowsAtIndexPaths:[NSArray arrayWithObject:perviouseIndexPathaf]
withRowAnimation:UITableViewRowAnimationNone];
}
UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"raj_star.png"]];
perviouseIndexPathaf = indexPath;
}
You can use the following code.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView.tag == 3)// We are checking if it's the desire tableview. If you have only one tableview then you can remove this if.
{
[tableView reloadData];
UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
cell.backgroundView = bgView; // Here set the particular image u want.
}
}
Try this code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *objCustomCell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
UIImageView *bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"NewImage"]];
objCustomCell.backgroundView = bgView;
}
in addition to your code, add the following code.
UIImageView *bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"SelectedMenuItem3"]];
cell.selectedBackgroundView = bgView;
For more info, check the documentation for UITableViewCell's selectedBackgroundView

show images of same width and height in cell of a table

I am using a table view(list view) there i am displaying images in cells,Images are displaying but not of same size its all of different size.I want all the images of same height and width.
Can any one give me the code for that.
Tnx..
Do like this,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell=nil;
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
CGRect imageFrame = CGRectMake(2, 8, 40, 40);
UIImage *img = [[[UIImage alloc] initWithData:data] autorelease];
UIImageView *customImage = [[[UIImageView alloc] initWithFrame:imageFrame] autorelease];
customImage.image=img;
[cell.contentView addSubview:customImage];
}
return cell;
}
just add image with frame in cellForRowAtIndexPath: method like bellow..
you can also use UIImageView instead of AsynchImageView
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"ImageCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithFrame:CGRectZero reuseIdentifier:CellIdentifier]
autorelease];
} else {
AsyncImageView* oldImage = (AsyncImageView*)
[cell.contentView viewWithTag:999];
[oldImage removeFromSuperview];
}
CGRect frame;
frame.size.width=75; frame.size.height=75;
frame.origin.x=0; frame.origin.y=0;
AsyncImageView* asyncImage = [[[AsyncImageView alloc]
initWithFrame:frame] autorelease];
asyncImage.tag = 999;
NSURL* url = [imageDownload
thumbnailURLAtIndex:indexPath.row];
[asyncImage loadImageFromURL:url];
[cell.contentView addSubview:asyncImage];
return cell;
}
also See This Demo for your requirement
i hope this help you...
In tableview datasource method -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath provide frame for image view
cell.imageView.frame

Dispalying text and label in custom cell of UItableView

Hi all I've custom cells in which I've UIImageView and UILabel I want to assign Image to UIImageView and text to UILabel which are in array, please can any one help me?? thanks in advance
I tried below 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];
}
cell.textLabel.text =[CurArray objectAtIndex:indexPath.row];
NSString *cellIcon = [[self IconArray] objectAtIndex:indexPath.row];
UIImage *cellIconimage = [UIImage imageNamed:cellIcon];
UIImageView *imageView = [[UIImageView alloc] initWithImage:cellIconimage];
[imageView setFrame:CGRectMake(100.0, 30.0, 30.0, 30.0)];
[imageView setContentMode:UIViewContentModeScaleAspectFill];
[cell addSubview:imageView];
return cell;
}
With this code i can assign image to created imageview But I'm not able to to assign text for UILable
here IBCustomCellProjectList is an example which is UITableViewCell with XIB just follow the logic
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
IBCustomCellProjectList *cell = (IBCustomCellProjectList *) [tableView dequeueReusableCellWithIdentifier:nil];
// yourCustomeCell *cell = (yourCustomeCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];//custom cell
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"IBCustomCellProjectList" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (IBCustomCellProjectList *) currentObject;
break;
}
}
}
// Configure the cell.
cell.cellLabel.text = [yourTextArray objectAtIndex:i];
cell.cellImageView.image = [UIImage imageNamed:[yourImageArray objectAtIndex:i]];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
Also see this bellow tutorial..
Custom cell with advance control and design
http://www.appcoda.com/customize-table-view-cells-for-uitableview/Customize tableview Cell
:)
Use this code. Maybe this is what you want. You will have to use the method cellForRowAtIndexPath .
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:[cell reuseIdentifier]];
if (cell == nil)
{
[[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
cell = customCell;
customCell = nil;
}
// Configure the cell.
cell.Label.text = [[NSNumber numberWithInt:indexPath.row+1]stringValue];
cell.Label.textColor = [UIColor blackColor];
cell.ImageView.image = [UIImage imageNamed:[CountryFlag objectAtIndex:indexPath.row]];
cell.textLabel.textColor=[UIColor whiteColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
Use Following code:
-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:#"cell"];
const NSInteger IMAGE_VIEW_TAG=1001;
const NSInteger LABEL_TAG=1002;
UIImageView *imageView;
UILabel *lbl;
if(cell==nil)
{
cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"] autorelease];
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
imageView =[[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 60, 60)] autorelease];
imageView.tag=IMAGE_VIEW_TAG;
imageView.contentMode=UIViewContentModeScaleAspectFit;
[cell.contentView addSubview:imageView];
lbl=[[UILabel alloc] initWithFrame:CGRectMake(100, 20, 160, 20)];
lbl.font=[UIFont fontWithName:#"Helvetica" size:14.0];
lbl.adjustsFontSizeToFitWidth=YES;
lbl.minimumFontSize=10.0;
lbl.textAlignment=UITextAlignmentLeft;
lbl.textColor=[UIColor colorWithRed:73.0/255.0 green:73.0/255.0 blue:73.0/255.0 alpha:1.0];
lbl.backgroundColor=[UIColor clearColor];
lbl.tag=LABEL_TAG;
[cell.contentView addSubview:lbl];
}
imageView=(UIImageView*)[cell.contentView viewWithTag:IMAGE_VIEW_TAG];
lbl=(UILabel*)[cell.contentView viewWithTag:LABEL_TAG];
imageView.image=[UIImage imageNamed:#"image.png"];
lbl.text=#"Your Text";
return cell;
}
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 65.0;
}

More than one image on UITableView

I need to put two images into a iOS tableview.
I used this code to place the image in a cell:
- (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];
}
UIImage *cellImage = [UIImage imageNamed:#"verde_diversos.png"];
cell.imageView.image = cellImage;
cell.textLabel.text = [self.colorNames objectAtIndex: [indexPath row]];
return cell;
}
Help?
You'll want to create your own UITableViewCell subclass. In its initialiser you would set up two UIImageViews and add them to the contentView as subviews of it. Then in layoutSubviews you would set the frames of the UIImageViews based on how you want them laid out.
- (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];
UIImageView *cellView = [[[UIImageView alloc] initWithFrame:CGRectMake(0,0,100,100)] autorelease];
cellView.tag = 555;
[cell.contentView addSubview:cellView];
}
UIImage *cellImage = [UIImage imageNamed:#"verde_diversos.png"];
cell.imageView.image = cellImage;
cell.textLabel.text = [self.colorNames objectAtIndex: [indexPath row]];
UIImageView *secondImage = [cell.contentView viewWithTag:555];
secondImage.image = [UIImage imageNamed:#"logo.png"];
return cell;
}