titleForHeaderSection from NSTableViewCell - ios

How can I retrieve the current header section name from an NSTableViewCell item?
Currently I have a method called configureCell which determines how to style the custom cell I have. This data comes from a pList file.
-(void)configureCell:(UITableViewCell *)tableViewCell forIndexPath:(NSIndexPath *)indexPath{
UILabel *label;
UIView *backView;
// NSString *country = [self tableView: tableViewCell titleForHeaderInSection:indexPath.section];
NSString *fatPercent = [[self.milkTypes valueForKey:#"Finland"] objectAtIndex:indexPath.row];
label = (UILabel *)[tableViewCell viewWithTag:1];
label.text = #"Fat Percent test";
backView = (UIView *)[tableViewCell viewWithTag:2];
backView.backgroundColor = [self colorWithHexString: fatPercent];
}
Where I've commented out the line for *country I need to retrieve the current section I'm in. Currently it's statically set to Finland which is the array name from the pList.
For a better understanding on how my code is laid out, here is the majority of the Table Controller.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [self.milkTypes count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [[self.milkTypes allKeys] objectAtIndex:section];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSString *country = [self tableView:tableView titleForHeaderInSection:section];
return [[self.milkTypes valueForKey:country] count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellIdentifier;
UITableViewCell *cell;
cellIdentifier = [NSString stringWithFormat:#"MilkCell"];
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell==nil) {
cell = [self tableViewCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
}
[self configureCell:cell forIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
-(UITableViewCell *)tableViewCellWithReuseIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath{
CGRect rect;
UILabel *label;
UIView *backView;
UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
float center = (float) (40-20)/2;
rect = CGRectMake(15, center, 270, 20);
label = [[UILabel alloc] initWithFrame:rect];
label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
label.tag = 1;
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor blackColor];
[cell.contentView addSubview:label];
[label release];
rect = CGRectMake(280, 10, 20, 40-20);
backView = [[UIView alloc] initWithFrame:rect];
backView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth;
backView.backgroundColor = [UIColor clearColor];
backView.tag = 2;
[cell.contentView addSubview:backView];
[backView release];
return cell;
}
-(void)configureCell:(UITableViewCell *)tableViewCell forIndexPath:(NSIndexPath *)indexPath {
UILabel *label;
UIView *backView;
// NSString *country = [self tableView: tableViewCell titleForHeaderInSection:indexPath.section];
NSString *fatPercent = [[self.milkTypes valueForKey:#"Sweden"] objectAtIndex:indexPath.row];
label = (UILabel *)[tableViewCell viewWithTag:1];
label.text = #"Fat Percent test";
backView = (UIView *)[tableViewCell viewWithTag:2];
backView.backgroundColor = [self colorWithHexString: fatPercent];
}

It seems you already have it anyway, in [[self.milkTypes allKeys] objectAtIndex:section] - just use that instead, so
NSString *country = [[self.milkTypes allKeys] objectAtIndex: indexPath.section];

Related

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.

Expand and collapse the tableview row

I am developing an iOS application. I have one expandable and collapsed tableview. On click the cell of tableview, the tableview cell expands properly but when I click on a second cell I want first cell to collapse and the second to expand. This would leave only one cell expanded at a time.
I am using class HVTableview:-
Code:-
-(void)tableView:(UITableView *)tableView expandCell: (UITableViewCell*)cell withIndexPath:(NSIndexPath*) indexPath;
{
UILabel *detail = (UILabel *)[cell viewWithTag:3];
UIButton *button = (UIButton *)[cell viewWithTag:10];
button.alpha = 0;
button.hidden = NO;
[UIView animateWithDuration:.5 animations:^{
detail.text = #"This is Expand.";
button.alpha = 1;
[cell.contentView viewWithTag:7].transform = CGAffineTransformMakeRotation(3.14);
}];
}
-(void)tableView:(UITableView *)tableView collapseCell: (UITableViewCell*)cell withIndexPath:(NSIndexPath*) indexPath;
{
UILabel *detail = (UILabel *)[cell viewWithTag:3];
UIButton *button = (UIButton *)[cell viewWithTag:10];
button.alpha = 0;
[UIView animateWithDuration:.5 animations:^{
detail.text = #"This is Collapse.";
button.alpha = 0;
[cell.contentView viewWithTag:7].transform = CGAffineTransformMakeRotation(-3.14);
} completion:^(BOOL finished) {
button.hidden = YES;
}];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_cites count];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath isExpanded: (BOOL)isExpanded;
{
if (isExpanded) {
return 150;
}else
{
return 100;
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath isExpanded:(BOOL)isExpanded;
{
static NSString *CellIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UILabel *title = (UILabel *)[cell viewWithTag:2];
UILabel *detail = (UILabel *)[cell viewWithTag:3];
UIImageView *image = (UIImageView *)[cell viewWithTag:1];
UIButton *button = (UIButton *)[cell viewWithTag:10];
[button addTarget:self action:#selector(goToTop) forControlEvents:UIControlEventTouchUpInside];
title.text = [_cites objectAtIndex:indexPath.row % 9];
NSString* bundlePath = [[NSBundle mainBundle] bundlePath];
NSString* imageFileName = [NSString stringWithFormat:#"%d.jpg", indexPath.row % 9 + 1];
image.image = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:#"%#/%#", bundlePath, imageFileName]];
if (indexPath.row %2 ==1)
cell.backgroundColor = [UIColor colorWithRed:.9 green:.9 blue:.9 alpha:1];
else
cell.backgroundColor = [UIColor colorWithRed:.8 green:.8 blue:.8 alpha:1];
if (!isExpanded)
{
detail.text = #"This is collapse";
[cell.contentView viewWithTag:7].transform = CGAffineTransformMakeRotation(0);
button.hidden = YES;
}
else
{
detail.text = #"This is Expand";
[cell.contentView viewWithTag:7].transform = CGAffineTransformMakeRotation(3.14);
button.hidden = NO;
}
return cell;
}
- (void) collapseExpandButtonTap:(id) sender
{
UIButton* aButton = (UIButton*)sender; //It's actually a button
NSIndexPath* aPath = [self indexPathForCellWithButtonTag:aButton.tag]; //Let's assume that you have only one section and button tags directly correspond to rows of your cells.
//expandedCells is a mutable set declared in your interface section or private class extensiont
if ([expandedCells containsObject:aPath])
{
[expandedCells removeObject:aPath];
}
else
{
[expandedCells addObject:aPath];
}
[myTableView beginEditing];
[myTableView endEditing]; //Yeah, that old trick to animate cell expand/collapse
}
In UITableViewDelegate method:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([expandedCells containsObject:indexPath])
{
return kExpandedCellHeight; //It's not necessary a constant, though
}
else
{
return kNormalCellHeigh; //Again not necessary a constant
}
}
Key thing here is to determine if your cell should be expanded/collapsed and return right height in delegate method. So accordingly design your requirements.

How to change the position of a row in UITableView in ios

I used the following tutorial to create a expandable tableview:
Expandable-Collapsable-TableView
My Image:
I want to reduce the gap between every row from left side. The Distance is too much.
Anyone can help me to position the row.
- (UITableViewCell*)createCellWithTitle:(NSString *)title image:(UIImage *)image indexPath:(NSIndexPath*)indexPath
{
NSString *CellIdentifier = #"Cell";
ExpandableTableViewCell* cell = [self.menuTableView dequeueReusableCellWithIdentifier:CellIdentifier];
UIView *bgView = [[UIView alloc] init];
bgView.backgroundColor = [UIColor grayColor];
cell.selectedBackgroundView = bgView;
cell.lblTitle.text = title;
cell.lblTitle.textColor = [UIColor blackColor];
[cell setIndentationLevel:[[[self.itemsInTable objectAtIndex:indexPath.row] valueForKey:#"level"] intValue]];
//**replace your x position**
cell.indentationWidth = 10;
float indentPoints = cell.indentationLevel * cell.indentationWidth;
cell.contentView.frame = CGRectMake(indentPoints,cell.contentView.frame.origin.y,cell.contentView.frame.size.width - indentPoints,cell.contentView.frame.size.height);
NSDictionary *d1=[self.itemsInTable objectAtIndex:indexPath.row] ;
if([d1 valueForKey:#"SubItems"])
{
cell.btnExpand.alpha = 1.0;
[cell.btnExpand addTarget:self action:#selector(showSubItems:) forControlEvents:UIControlEventTouchUpInside];
}
else
{
cell.btnExpand.alpha = 0.0;
}
return cell;
}
That's called "indentation".
If you're using a prototype cell in a storyboard/xib, you can directly change its indentation from the cell's Attributes Inspector.
(You don't have to change the width to 0, if your indentation level is 0).
If not, you can change it programmatically, like:
-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.indentationLevel = 0; // No indentation level.
cell.indentationWidth = 0; // It's redundant if the level is 0.
}
open the createCellWithTitle method and change the cell.indentationWidth as your self
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *Title= [[self.itemsInTable objectAtIndex:indexPath.row] valueForKey:#"Name"];
return [self createCellWithTitle:Title image:[[self.itemsInTable objectAtIndex:indexPath.row] valueForKey:#"Image name"] indexPath:indexPath];
}
- (UITableViewCell*)createCellWithTitle:(NSString *)title image:(UIImage *)image indexPath:(NSIndexPath*)indexPath
{
[cell setIndentationLevel:[[[self.itemsInTable objectAtIndex:indexPath.row] valueForKey:#"level"] intValue]];
cell.indentationWidth = 40; //change the width as your self
}

UITableView dequeue not working as expected

As I scroll down the list, all the rows are there, but they keep adding more subviews the more they appear on the visible frame
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *reuse = #"RuleCell";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:reuse];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier:reuse];
}
NSUInteger row = indexPath.row;
[self createCell: cell onRow: row];
return cell;
}
- (void) createCell: (UITableViewCell*)cell onRow: (NSUInteger)row
{
UIImageView* bgImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"cell_background_spade_active.png"]];
cell.backgroundView = bgImage;
cell.textLabel.hidden = YES;
UILabel* titleLabel = [[UILabel alloc] initWithFrame: CGRectMake(100, CGRectGetHeight(cell.frame) / 2, 200, 50)];
titleLabel.text = [[self.ruleList objectAtIndex: row] objectForKey: TitleKey];
titleLabel.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview: titleLabel];
}
I think you need to execute almost all of the logic that's in createCell: only within the if (cell == nil){ segment of your code. The part that should execute where you're currently calling createCell: is just getting a reference to the titleLabel and setting its text value.
To clarify, here's the kind of modification I'm suggesting (not tested, but should give the right idea):
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *reuse = #"RuleCell";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:reuse];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier:reuse];
[self setUpCell: cell];
}
NSUInteger row = indexPath.row;
UILabel *titleLabel = (UILabel *)[cell.contentView viewWithTag:42];
titleLabel.text = [[self.ruleList objectAtIndex: row] objectForKey: TitleKey];
return cell;
}
- (void) setUpCell: (UITableViewCell*)cell
{
UIImageView* bgImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"cell_background_spade_active.png"]];
cell.backgroundView = bgImage;
cell.textLabel.hidden = YES;
UILabel* titleLabel = [[UILabel alloc] initWithFrame: CGRectMake(100, CGRectGetHeight(cell.frame) / 2, 200, 50)];
titleLabel.tag = 42;
titleLabel.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview: titleLabel];
}

strike on the uilabel disappears in uitableview did select cell

1)i want to create a striked text so i have used thet ext frame and i have created it and keep on the lbal it works fine when i have used it in the table cell when i am selecting the cell the strike on the label disaperared
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
#pragma mark TableView delegate method.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView1
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView1 numberOfRowsInSection:(NSInteger)section
{
return 4;
}
- (CGFloat)tableView:(UITableView *)tableView1 heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 120;
}
- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:CellIdentifier];
tableView1.backgroundColor = [UIColor clearColor];
cell=nil;
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
//cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.backgroundColor = [UIColor clearColor];
// cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"strip_11C.png"]];
//cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"strip_11C_h.png"]];
UILabel *price1_lbl = [[UILabel alloc] initWithFrame:CGRectMake(2,80,100,26)];
price1_lbl.text = #"label";
price1_lbl.textColor = [UIColor grayColor];
price1_lbl.font = [UIFont fontWithName:#"Arial-BoldMT" size:(13.0)];
price1_lbl.backgroundColor = [UIColor clearColor];
NSString *string =price1_lbl.text;
CGSize stringSize = [string sizeWithFont:price1_lbl.font];
CGRect buttonFrame = price1_lbl.frame;
CGRect labelFrame = CGRectMake(buttonFrame.origin.x ,
4+buttonFrame.origin.y + stringSize.height/2,
stringSize.width, 1);
UILabel *lineLabel = [[UILabel alloc] initWithFrame:labelFrame];
lineLabel.backgroundColor = [UIColor blackColor];
[cell.contentView addSubview:price1_lbl];
[cell.contentView addSubview:lineLabel];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
secondviewcontroller *obj=[[secondviewcontroller alloc]initWithNibName:#"secondviewcontroller" bundle:nil];
[self.navigationController pushViewController:obj animated:YES];
}
Try to add the strikethrough line directly to the label with the text (price1_lbl). If that doesn't work you may want to give this lib a try.
UPDATE:
You can use this code:
CGSize size = [price1_lbl.text sizeWithFont:[UIFont systemFontOfSize:16.0f]];
UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(0 , label.frame.size.height / 2, size.width, 1)];
line.backgroundColor = [UIColor blackColor];
[price1_lbl addSubview:line];

Resources