When TableView scroll than value changed - iphone

I have created a table view that display data from array.but now when i scroll table view than my data change unacceptably.
So how can i prevent tableview to refresh every time when scroll and set all the data first time when table view created.
here is my code:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
ExampleAppDataObject* theDataObject = [self theAppDataObject]; // Return the number of sections.
return theDataObject.vendorTableAry.count; //this is return value of array and it will be more than 2 which help to create section
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 4; // i want only 4 row in each section
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ExampleAppDataObject* theDataObject = [self theAppDataObject];
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
UITableViewCell * cell = [tableView
dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if(cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier];
/*cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:SimpleTableIdentifier] autorelease];
*/
if (indexPath.row==0) {
UILabel * vendorLbl = [[UILabel alloc] initWithFrame:CGRectMake(10, 2, 60, 39)];
[[vendorLbl layer] setCornerRadius:5.0f];
//[vendorLbl setFont:[UIFont fontWithName:#"Helvetica-Bold" size:13.0]];
vendorLbl.backgroundColor = [UIColor clearColor];
[vendorLbl setText:#"vender"];
[cell.contentView addSubview:vendorLbl];
UILabel * columnLbl = [[UILabel alloc] initWithFrame:CGRectMake(80, 2, 10, 39)];
columnLbl.backgroundColor = [UIColor clearColor];
[columnLbl setText:#":"];
[cell.contentView addSubview:columnLbl];
UILabel * vendorValueLbl = [[UILabel alloc] initWithFrame:CGRectMake(98, 2, 195, 39)];
[[vendorValueLbl layer] setCornerRadius:5.0f];
//[vendorLbl setFont:[UIFont fontWithName:#"Helvetica-Bold" size:13.0]];
vendorValueLbl.backgroundColor = [UIColor clearColor];
[vendorValueLbl setText:[theDataObject.vendorTableAry objectAtIndex:indexPath.section]];
[cell.contentView addSubview:vendorValueLbl];
}
if (indexPath.row==1) {
UILabel * vendorLbl = [[UILabel alloc] initWithFrame:CGRectMake(10, 2, 60, 39)];
[[vendorLbl layer] setCornerRadius:5.0f];
//[vendorLbl setFont:[UIFont fontWithName:#"Helvetica-Bold" size:13.0]];
vendorLbl.backgroundColor = [UIColor clearColor];
[vendorLbl setText:#"Gallons"];
[cell.contentView addSubview:vendorLbl];
UILabel * columnLbl = [[UILabel alloc] initWithFrame:CGRectMake(80, 2, 10, 39)];
columnLbl.backgroundColor = [UIColor clearColor];
[columnLbl setText:#":"];
[cell.contentView addSubview:columnLbl];
UILabel * vendorValueLbl = [[UILabel alloc] initWithFrame:CGRectMake(98, 2, 195, 39)];
[[vendorValueLbl layer] setCornerRadius:5.0f];
//[vendorLbl setFont:[UIFont fontWithName:#"Helvetica-Bold" size:13.0]];
vendorValueLbl.backgroundColor = [UIColor clearColor];
[vendorValueLbl setText:[theDataObject.gallonsTableAry objectAtIndex:indexPath.section]];
[cell.contentView addSubview:vendorValueLbl];
}
if (indexPath.row==2) {
UILabel * vendorLbl = [[UILabel alloc] initWithFrame:CGRectMake(10, 2, 60, 39)];
[[vendorLbl layer] setCornerRadius:5.0f];
//[vendorLbl setFont:[UIFont fontWithName:#"Helvetica-Bold" size:13.0]];
vendorLbl.backgroundColor = [UIColor clearColor];
[vendorLbl setText:#"Route"];
[cell.contentView addSubview:vendorLbl];
UILabel * columnLbl = [[UILabel alloc] initWithFrame:CGRectMake(80, 2, 10, 39)];
columnLbl.backgroundColor = [UIColor clearColor];
[columnLbl setText:#":"];
[cell.contentView addSubview:columnLbl];
UILabel * vendorValueLbl = [[UILabel alloc] initWithFrame:CGRectMake(98, 2, 195, 39)];
[[vendorValueLbl layer] setCornerRadius:5.0f];
//[vendorLbl setFont:[UIFont fontWithName:#"Helvetica-Bold" size:13.0]];
vendorValueLbl.backgroundColor = [UIColor clearColor];
[vendorValueLbl setText:[theDataObject.routeTableAry objectAtIndex:indexPath.section]];
[cell.contentView addSubview:vendorValueLbl];
}
if (indexPath.row==3) {
UILabel * vendorLbl = [[UILabel alloc] initWithFrame:CGRectMake(10, 2, 60, 39)];
[[vendorLbl layer] setCornerRadius:5.0f];
//[vendorLbl setFont:[UIFont fontWithName:#"Helvetica-Bold" size:13.0]];
vendorLbl.backgroundColor = [UIColor clearColor];
[vendorLbl setText:#"Date"];
[cell.contentView addSubview:vendorLbl];
UILabel * columnLbl = [[UILabel alloc] initWithFrame:CGRectMake(80, 2, 10, 39)];
columnLbl.backgroundColor = [UIColor clearColor];
[columnLbl setText:#":"];
[cell.contentView addSubview:columnLbl];
UILabel * vendorValueLbl = [[UILabel alloc] initWithFrame:CGRectMake(98, 2, 195, 39)];
[[vendorValueLbl layer] setCornerRadius:5.0f];
//[vendorLbl setFont:[UIFont fontWithName:#"Helvetica-Bold" size:13.0]];
vendorValueLbl.backgroundColor = [UIColor clearColor];
[vendorValueLbl setText:[theDataObject.dateTableAry objectAtIndex:indexPath.section]];
[cell.contentView addSubview:vendorValueLbl];
}
}
return cell;
}

Just set dequeueReusableCellWithIdentifier to nil like below..
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: nil];

Related

iOS- mimic system default tableView header

I want to mimic programatically the exact look of this header:
So far, my best try was:
UILabel* header = [[UILabel alloc] init] ;
header.backgroundColor = [UIColor clearColor];
header.textAlignment = NSTextAlignmentCenter;
header.textColor = [[UIColor alloc] initWithRed:86 green:92 blue:112 alpha:0.1];
header.shadowColor = [UIColor darkGrayColor];
header.shadowOffset = CGSizeMake(1,0);
header.font = [UIFont boldSystemFontOfSize:18];
But it's looks like this:
Can someone please help me with the exact way to do that?
Thanks in advance!
A Norvegian guy has written a blog post about this a while ago: http://www.gersh.no/posts/view/default_styling_of_sections_headers_in_uitableview_grouped
All credits go to him for the code.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
containerView.backgroundColor = [UIColor groupTableViewBackgroundColor];
CGRect labelFrame = CGRectMake(20, 2, 320, 30);
if(section == 0) {
labelFrame.origin.y = 13;
}
UILabel *label = [[UILabel alloc] initWithFrame:labelFrame];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:17];
label.shadowColor = [UIColor colorWithWhite:1.0 alpha:1];
label.shadowOffset = CGSizeMake(0, 1);
label.textColor = [UIColor colorWithRed:0.265 green:0.294 blue:0.367 alpha:1.000];
label.text = [self tableView:tableView titleForHeaderInSection:section];
[containerView addSubview:label];
return containerView;
}
EITHER
You need to set header.frame = CGRectMake(10,1, 100, 18 );
OR
Use Datasource method of UITableView
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return #"Header"; // just pass name of your hearder
}
EDITED:
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 2, 100, 18)];
label.text= [self.listOfHeader objectAtIndex:section];
label.backgroundColor=[UIColor clearColor];
label.textAlignment=NSTextAlignmentLeft;
[view addSubview:label];
return view;
}
And also add
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 20;
}
What you can do it calculate the width of your UILabel dynamically/programmatically. Using this code :
CGSize maximumLabelSize = CGSizeMake(296,9999);
CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font
constrainedToSize:maximumLabelSize
lineBreakMode:yourLabel.lineBreakMode];
Then add 10 pixle to the width you get. Set the frame of your UILabel. And, set it following way :
header.frame = CGRectMake(0.0,5.0,expectedLabelSize.width + 10.0,expectedLabelSize.height);
header.textAlignment = NSTextAlignmentRight;

Repeating and Overlapping UITableView cells in iPhone

The UITableView cells are repeating on scroll and overlapping each other.
How can i fix this issue
here is my code
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 11;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UIButton *buyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImageView *img;
UILabel *lbl;
UIImageView *backImage;
UILabel *textLabel;
UILabel *detailTextLabel;
NSInteger val = [indexPath row] * 3;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
backImage = [[UIImageView alloc] initWithFrame:CGRectMake(2, 4, 316, 62)];
[buyBtn setFrame:CGRectMake(257, 35, 57, 24)];
img = [[UIImageView alloc] initWithFrame:CGRectMake(257, 10, 57, 24)];
lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 57, 24)];
textLabel = [[UILabel alloc] initWithFrame:CGRectMake(4, 4, 230, 25)];
detailTextLabel = [[UILabel alloc] initWithFrame:CGRectMake(4, 25, 230, 30)];
backImage.image = [UIImage imageNamed:#"shop-bg.png"];
[buyBtn setImage:[UIImage imageNamed:#"buy_button"] forState:UIControlStateNormal];
[img setImage:[UIImage imageNamed:#"price_button.png"]];
}
else
{
backImage = [[UIImageView alloc] initWithFrame:CGRectMake(2, 4, 316, 62)];
[buyBtn setFrame:CGRectMake(257, 35, 57, 24)];
img = [[UIImageView alloc] initWithFrame:CGRectMake(257, 10, 57, 24)];
lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 57, 24)];
textLabel = [[UILabel alloc] initWithFrame:CGRectMake(4, 4, 230, 25)];
detailTextLabel = [[UILabel alloc] initWithFrame:CGRectMake(4, 25, 230, 30)];
backImage.image = [UIImage imageNamed:#"shop-bg.png"];
[buyBtn setImage:[UIImage imageNamed:#"buy_button"] forState:UIControlStateNormal];
[img setImage:[UIImage imageNamed:#"price_button.png"]];
}
lbl.center = img.center;
lbl.textAlignment = UITextAlignmentCenter;
lbl.text = [self.original_List objectAtIndex:val+2];
lbl.backgroundColor = [UIColor clearColor];
textLabel.font = [UIFont fontWithName:#"Arial Rounded MT Bold" size:14];
textLabel.backgroundColor = [UIColor clearColor];
textLabel.text = [self.original_List objectAtIndex:val];
detailTextLabel.text = [self.original_List objectAtIndex:val+1];
detailTextLabel.backgroundColor = [UIColor clearColor];
detailTextLabel.font = [UIFont fontWithName:#"Arial Rounded MT Bold" size:10];
textLabel.textColor = [UIColor blackColor];
detailTextLabel.textColor = [UIColor whiteColor];
detailTextLabel.numberOfLines = 2;
[buyBtn setTag:[indexPath row]];
[buyBtn addTarget:self action:#selector(buyBtnPressed:) forControlEvents:UIControlEventTouchDown];
static NSString *myIdentifier = #"myIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myIdentifier];
NSLog(#"%d" , [[cell subviews] count]);
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myIdentifier];
}
cell.accessoryType = UITableViewCellAccessoryNone;
[cell setSelectionStyle:UITableViewCellEditingStyleNone];
cell.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:backImage];
[cell.contentView addSubview:detailTextLabel];
[cell.contentView addSubview:textLabel];
[cell.contentView addSubview:buyBtn];
[cell.contentView addSubview:img];
[cell.contentView addSubview:lbl];
return cell;
}
Try this code :-
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UIButton *buyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImageView *img;
UILabel *lbl;
UIImageView *backImage;
UILabel *textLabel;
UILabel *detailTextLabel;
NSInteger val = [indexPath row] * 3;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
backImage = [[UIImageView alloc] initWithFrame:CGRectMake(2, 4, 316, 62)];
[buyBtn setFrame:CGRectMake(257, 35, 57, 24)];
img = [[UIImageView alloc] initWithFrame:CGRectMake(257, 10, 57, 24)];
lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 57, 24)];
textLabel = [[UILabel alloc] initWithFrame:CGRectMake(4, 4, 230, 25)];
detailTextLabel = [[UILabel alloc] initWithFrame:CGRectMake(4, 25, 230, 30)];
backImage.image = [UIImage imageNamed:#"shop-bg.png"];
[buyBtn setImage:[UIImage imageNamed:#"buy_button"] forState:UIControlStateNormal];
[img setImage:[UIImage imageNamed:#"price_button.png"]];
}
else
{
backImage = [[UIImageView alloc] initWithFrame:CGRectMake(2, 4, 316, 62)];
[buyBtn setFrame:CGRectMake(257, 35, 57, 24)];
img = [[UIImageView alloc] initWithFrame:CGRectMake(257, 10, 57, 24)];
lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 57, 24)];
textLabel = [[UILabel alloc] initWithFrame:CGRectMake(4, 4, 230, 25)];
detailTextLabel = [[UILabel alloc] initWithFrame:CGRectMake(4, 25, 230, 30)];
backImage.image = [UIImage imageNamed:#"shop-bg.png"];
[buyBtn setImage:[UIImage imageNamed:#"buy_button"] forState:UIControlStateNormal];
[img setImage:[UIImage imageNamed:#"price_button.png"]];
}
lbl.center = img.center;
lbl.textAlignment = UITextAlignmentCenter;
lbl.text = [self.original_List objectAtIndex:val+2];
lbl.backgroundColor = [UIColor clearColor];
textLabel.font = [UIFont fontWithName:#"Arial Rounded MT Bold" size:14];
textLabel.backgroundColor = [UIColor clearColor];
textLabel.text = [self.original_List objectAtIndex:val];
detailTextLabel.text = [self.original_List objectAtIndex:val+1];
detailTextLabel.backgroundColor = [UIColor clearColor];
detailTextLabel.font = [UIFont fontWithName:#"Arial Rounded MT Bold" size:10];
textLabel.textColor = [UIColor blackColor];
detailTextLabel.textColor = [UIColor whiteColor];
detailTextLabel.numberOfLines = 2;
[buyBtn setTag:[indexPath row]];
[buyBtn addTarget:self action:#selector(buyBtnPressed:) forControlEvents:UIControlEventTouchDown];
static NSString *myIdentifier = #"myIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myIdentifier];
NSLog(#"%d" , [[cell subviews] count]);
//if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myIdentifier];
}
cell.accessoryType = UITableViewCellAccessoryNone;
[cell setSelectionStyle:UITableViewCellEditingStyleNone];
cell.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:backImage];
[cell.contentView addSubview:detailTextLabel];
[cell.contentView addSubview:textLabel];
[cell.contentView addSubview:buyBtn];
[cell.contentView addSubview:img];
[cell.contentView addSubview:lbl];
return cell;
}
You need to check height for cell in
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
It should be greater then max height used in your code may be 65
Don't use reuseIdentifier and set to nil.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
}
What is happening here is that you use
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myIdentifier];
This code makes sure that when you are scrolling in a UITableView, the cell gets reused to save on memory. Since you are using the same identifier for all cells, it will just reuse a cell that already has a 'backImage', 'detailTextLabel' and so forth, and add the new view components on top of the already existing ones.
You could fix this by using a code similar to this:
static NSString *myIdentifier = #"myIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myIdentifier];
// Create backImage, detailTextLabel, etc. here
...
// Give view components tags
backImage.tag = 1;
detailTextLabel.tag = 2;
...
}
UIImageView* retrievedBackImage = (UIImageView*)[cell.contentView viewWithTag:1];
UILabel* retrievedDetailTextLabel = (UILabel *)[cell.contentView viewWithTag:2];
...
// Initialise values
retrievedBackImage.image = [UIImage imageNamed:#"someImage.png"];
retrievedDetailTextLabel.text = #"something";
...
Hope this was helpful.
Nicolas

Add textfield and checkbox on tableview

In my Iphone app I have to add textfield at two rows of tableview and a checkbox to one row. Can anyone provide me an example that can help me. I am new on Iphone and I don't know how to start with this.
Thanks in advance..
Hey try this one in your cellForRowAtindexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:#"CellID%d%d",indexPath.section,indexPath.row]];
if (!cell){
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:[NSString stringWithFormat:#"CellID%d%d",indexPath.section,indexPath.row]] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
if(indexPath.row == 0){
// NSString *daytime = [[NSUserDefaults standardUserDefaults] objectForKey:#"day"];
UITextField *text0 = [[UITextField alloc] initWithFrame:CGRectMake(20, 12, 120, 22)];
text0.textAlignment = UITextAlignmentLeft;
text0.backgroundColor = [UIColor whiteColor];
text0.borderStyle = UITextBorderStyleNone;
text0.userInteractionEnabled = FALSE;
text0.font = [UIFont boldSystemFontOfSize:17];
text0.textColor = [UIColor blackColor];
text0.delegate = self;
text0.text = #"Type de Parteneria";
[cell addSubview:text0];
UITextField *text1 = [[UITextField alloc] initWithFrame:CGRectMake(120, 12, 160, 20)];
text1.textAlignment = UITextAlignmentRight;
text1.borderStyle = UITextBorderStyleNone;
text1.backgroundColor = [UIColor whiteColor];
text1.userInteractionEnabled = TRUE;
text1.font = [UIFont boldSystemFontOfSize:16];
text1.textColor = [UIColor colorWithRed:114.0f/255.0f green:136.0f/255.0f blue:165.0f/255.0f alpha:1.0];
text1.delegate = self;
[cell addSubview:text1];
}
else if(indexPath.row == 1){
// NSString *daytime = [[NSUserDefaults standardUserDefaults] objectForKey:#"day"];
UITextField *text0 = [[UITextField alloc] initWithFrame:CGRectMake(20, 12, 120, 22)];
text0.textAlignment = UITextAlignmentLeft;
text0.backgroundColor = [UIColor whiteColor];
text0.borderStyle = UITextBorderStyleNone;
text0.userInteractionEnabled = FALSE;
text0.font = [UIFont boldSystemFontOfSize:17];
text0.textColor = [UIColor blackColor];
text0.delegate = self;
text0.text = #"Audi R8";
[cell addSubview:text0];
UIButton *signBtn = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
signBtn.frame = CGRectMake(15, 170, 290, 35);
[signBtn setTitle:#"Sign Up" forState:UIControlStateNormal];
[signBtn setBackgroundImage:[UIImage imageNamed:#"CheckBox.png"] forState:UIControlStateNormal];
[signBtn setFont:[UIFont boldSystemFontOfSize:17]];
[signBtn addTarget:self action:#selector(checkPressed) forControlEvents:UIControlEventTouchDown];
[cell addSubview:signBtn];
}
return cell;
}
If you get problem than message me
as per your comment try this :
if(indexPath.row==1)
{
UITextField *labl=[[UITextField alloc] init];
// where is your text field's frame ????
labl.frame = CGRectMake(10,10,50,50);
labl.text=#"data";
[cell addSubview:labl];
}

UIScrollView with paging not working ok

I have implement a scrollview with paging but it not work ok. For every view of the scrollview I must populate a tableview with certain values. Even if the values are correct received all the views are identical (the last view is the view on every subview of scrollview) Where is my mistake?
Here is my code :
- (void)viewDidLoad
{
[super viewDidLoad];
self.vehiculesenvisagesScrollView.pagingEnabled = YES;
self.vehiculesenvisagesScrollView.contentSize = CGSizeMake(vehiculesenvisagesScrollView.frame.size.width * [vehiculesEnvisages count], vehiculesenvisagesScrollView.frame.size.height);
self.vehiculesenvisagesScrollView.showsHorizontalScrollIndicator = NO;
self.vehiculesenvisagesScrollView.showsVerticalScrollIndicator = NO;
self.vehiculesenvisagesScrollView.scrollsToTop = NO;
vehiculesEnvisagesArray = [[NSMutableArray alloc] initWithObjects:#"Gamme", #"Usage",#"Délai d'echart",#"Type d'achart",#"Financement", nil];
for (int i=0; i<[vehiculesEnvisages count];i++){
[self loadScrollViewWithPage:i];
}
NSLog(#"views %#",[vehiculesenvisagesScrollView subviews]);
self.pageControlVehiculeEnvisages.numberOfPages=[vehiculesEnvisages count];
self.pageControlVehiculeEnvisages.currentPage=0;
}
- (void) loadScrollViewWithPage: (int) page {
NSLog(#"%d",page);
if (page < 0) return;
if (page >= [vehiculesEnvisages count]) return;
CGRect frame;
frame.origin.x = self.vehiculesenvisagesScrollView.frame.size.width * page;
frame.origin.y = 0;
frame.size = self.vehiculesenvisagesScrollView.frame.size;
UIView *oneview = [[UIView alloc] initWithFrame:frame];
tableViewVehiculesEnvisages=[[UITableView alloc] initWithFrame:CGRectMake(3, 0, 320, 233) style:UITableViewStyleGrouped];
tableViewVehiculesEnvisages.bounces=NO;
tableViewVehiculesEnvisages.backgroundColor=[UIColor clearColor];
[tableViewVehiculesEnvisages setDelegate:self];
[tableViewVehiculesEnvisages setDataSource:self];
self.gammeString=[[vehiculesEnvisages objectAtIndex:page]valueForKey:#"gamme"];
self.usageString=[[vehiculesEnvisages objectAtIndex:page]valueForKey:#"usage"];
self.delaiString=[[vehiculesEnvisages objectAtIndex:page]valueForKey:#"delai"];
self.typeString=[[vehiculesEnvisages objectAtIndex:page]valueForKey:#"achat"];
self.financementString=[[vehiculesEnvisages objectAtIndex:page]valueForKey:#"financement"];
[oneview addSubview:tableViewVehiculesEnvisages];
NSLog(#"gamme%#", self.gammeString);
[self.vehiculesenvisagesScrollView addSubview:oneview];
[oneview release];
}
where gammeString,usageString,delaiString,typeString,financementString contains the text that will be set to some labels on a cell of tableview.
Here is my cellForRowAtIndexPath method :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CellRecherchePartenaires"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:#"CellRecherchePartenaires"] autorelease];
cell.backgroundColor = [UIColor clearColor];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
cell.backgroundView.opaque = NO;
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.opaque = NO;
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.highlightedTextColor = [UIColor whiteColor];
cell.textLabel.font = [UIFont boldSystemFontOfSize:15];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.opaque = NO;
cell.detailTextLabel.textColor = [UIColor whiteColor];
cell.detailTextLabel.highlightedTextColor = [UIColor whiteColor];
cell.detailTextLabel.font = [UIFont systemFontOfSize:15];
if (indexPath.row==0)
{
gammeLabel = [[UILabel alloc] init];
gammeLabel.frame = CGRectMake(150, -4, 150, 50);
gammeLabel.textColor=[UIColor grayColor];
gammeLabel.backgroundColor=[UIColor clearColor];
gammeLabel.text = self.gammeString;
[cell.contentView addSubview: [gammeLabel autorelease]];
}
if(indexPath.row==1)
{
usageLabel = [[UILabel alloc] init];
usageLabel.frame = CGRectMake(150, -4, 150, 50);
usageLabel.textColor=[UIColor grayColor];
usageLabel.backgroundColor=[UIColor clearColor];
usageLabel.text = self.usageString;
[cell.contentView addSubview: [usageLabel autorelease]];
}
if(indexPath.row==2)
{
delaiLabel = [[UILabel alloc] init];
delaiLabel.frame = CGRectMake( 150, -4, 150, 50);
delaiLabel.textColor = [UIColor grayColor];
delaiLabel.backgroundColor = [UIColor clearColor];
delaiLabel.text = self.delaiString;
[cell.contentView addSubview: [delaiLabel autorelease]];
}
if(indexPath.row==3)
{
typeLabel = [[UILabel alloc] init];
typeLabel.frame = CGRectMake( 150, -4, 150, 50);
typeLabel.textColor = [UIColor grayColor];
typeLabel.backgroundColor = [UIColor clearColor];
typeLabel.text =self.typeString;
[cell.contentView addSubview: [typeLabel autorelease]];
}
if(indexPath.row==4)
{
financementLabel = [[UILabel alloc] init];
financementLabel.frame = CGRectMake( 150, -4, 150, 50);
financementLabel.textColor = [UIColor grayColor];
financementLabel.backgroundColor = [UIColor clearColor];
financementLabel.text = self.financementString;
[cell.contentView addSubview: [financementLabel autorelease]];
}
}
[[cell textLabel] setText: [vehiculesEnvisagesArray objectAtIndex:indexPath.row]] ;
return cell;
}
- (void) loadScrollViewWithPage: (int) page {
NSLog(#"%d",page);
if (page < 0) return;
if (page >= [vehiculesEnvisages count]) return;
CGRect frame;
frame.origin.x = self.vehiculesenvisagesScrollView.frame.size.width * page;
frame.origin.y = 0;
frame.size = self.vehiculesenvisagesScrollView.frame.size;
UIView *oneview = [[UIView alloc] initWithFrame:frame];
tableViewVehiculesEnvisages=[[UITableView alloc] initWithFrame:CGRectMake(3, 0, 320, 233) style:UITableViewStyleGrouped];
tableViewVehiculesEnvisages.tag=page;
tableViewVehiculesEnvisages.bounces=NO;
tableViewVehiculesEnvisages.backgroundColor=[UIColor clearColor];
[tableViewVehiculesEnvisages setDelegate:self];
[tableViewVehiculesEnvisages setDataSource:self];
[oneview addSubview:tableViewVehiculesEnvisages];
NSLog(#"gamme%#", self.gammeString);
[self.vehiculesenvisagesScrollView addSubview:oneview];
[oneview release];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CellRecherchePartenaires"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:#"CellRecherchePartenaires"] autorelease];
}
[self setTextt:tableView.tag];
cell.backgroundColor = [UIColor clearColor];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
cell.backgroundView.opaque = NO;
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.opaque = NO;
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.highlightedTextColor = [UIColor whiteColor];
cell.textLabel.font = [UIFont boldSystemFontOfSize:15];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.opaque = NO;
cell.detailTextLabel.textColor = [UIColor whiteColor];
cell.detailTextLabel.highlightedTextColor = [UIColor whiteColor];
cell.detailTextLabel.font = [UIFont systemFontOfSize:15];
if (indexPath.row==0)
{
gammeLabel = [[UILabel alloc] init];
gammeLabel.frame = CGRectMake(150, -4, 150, 50);
gammeLabel.textColor=[UIColor grayColor];
gammeLabel.backgroundColor=[UIColor clearColor];
gammeLabel.text = self.gammeString;
[cell.contentView addSubview: [gammeLabel autorelease]];
}
if(indexPath.row==1)
{
usageLabel = [[UILabel alloc] init];
usageLabel.frame = CGRectMake(150, -4, 150, 50);
usageLabel.textColor=[UIColor grayColor];
usageLabel.backgroundColor=[UIColor clearColor];
usageLabel.text = self.usageString;
[cell.contentView addSubview: [usageLabel autorelease]];
}
if(indexPath.row==2)
{
delaiLabel = [[UILabel alloc] init];
delaiLabel.frame = CGRectMake( 150, -4, 150, 50);
delaiLabel.textColor = [UIColor grayColor];
delaiLabel.backgroundColor = [UIColor clearColor];
delaiLabel.text = self.delaiString;
[cell.contentView addSubview: [delaiLabel autorelease]];
}
if(indexPath.row==3)
{
typeLabel = [[UILabel alloc] init];
typeLabel.frame = CGRectMake( 150, -4, 150, 50);
typeLabel.textColor = [UIColor grayColor];
typeLabel.backgroundColor = [UIColor clearColor];
typeLabel.text =self.typeString;
[cell.contentView addSubview: [typeLabel autorelease]];
}
if(indexPath.row==4)
{
financementLabel = [[UILabel alloc] init];
financementLabel.frame = CGRectMake( 150, -4, 150, 50);
financementLabel.textColor = [UIColor grayColor];
financementLabel.backgroundColor = [UIColor clearColor];
financementLabel.text = self.financementString;
[cell.contentView addSubview: [financementLabel autorelease]];
}
[[cell textLabel] setText: [vehiculesEnvisagesArray objectAtIndex:indexPath.row]] ;
return cell;
}
-(void)setTextt:(int)page
{
self.gammeString=[[vehiculesEnvisages objectAtIndex:page]valueForKey:#"gamme"];
self.usageString=[[vehiculesEnvisages objectAtIndex:page]valueForKey:#"usage"];
self.delaiString=[[vehiculesEnvisages objectAtIndex:page]valueForKey:#"delai"];
self.typeString=[[vehiculesEnvisages objectAtIndex:page]valueForKey:#"achat"];
self.financementString=[[vehiculesEnvisages objectAtIndex:page]valueForKey:#"financement"];
}
Replace your code with this code,You defiantly get solution.
I found the solution of my issue : I use a NSMutableArray instead of a NSString and it works ok.

UITableViewCell not showing UILabel within

I have this code and it gives the result of the screenshot below. It's in the tableView:cellForRowAtIndexPath: method. I don't know why the text above the bars isn't showing. Does anyone have any ideas? Thank you.
cell.textLabel.text = #"";
cell.accessoryType = UITableViewCellAccessoryNone;
cell.textAlignment = UITextAlignmentLeft;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UILabel * label = [[UILabel alloc] initWithFrame: cell.textLabel.frame];
label.backgroundColor = [UIColor clearColor];
label.text = [NSString stringWithFormat:#"%.1f%% (%i)",([[options_votes objectAtIndex: [indexPath row]] intValue] * 100.0 / total_votes),[[options_votes objectAtIndex: [indexPath row]] intValue]];
NSLog(#"%#",[NSString stringWithFormat:#"%.1f%% (%i)",([[options_votes objectAtIndex: [indexPath row]] intValue] * 100.0 / total_votes),[[options_votes objectAtIndex: [indexPath row]] intValue]]);
label.textAlignment = UITextAlignmentRight;
[cell.contentView addSubview:label];
[label release];
label = [[UILabel alloc] initWithFrame: cell.textLabel.frame];
label.backgroundColor = [UIColor clearColor];
label.text = [options objectAtIndex:[indexPath row]];
NSLog(#"%#",[options objectAtIndex:[indexPath row]]);
label.textAlignment = UITextAlignmentLeft;
[cell.contentView addSubview:label];
[label release];
UIImageView * image_view = [[UIImageView alloc] initWithImage:nav_delegate->back_bar];
image_view.frame = CGRectMake(10, 44, 280, 10);
[cell.contentView addSubview:image_view];
[image_view release];
image_view = [[UIImageView alloc] initWithImage:nav_delegate->blue_bar];
image_view.frame = CGRectMake(10, 44, 5 + [[options_votes objectAtIndex: [indexPath row]] intValue] * 275.0/total_votes, 10);
[cell.contentView addSubview:image_view];
[image_view release];
Just a stab in the dark, but because you did cell.textLabel.text = #"";, it might have shrank the frame of the textLabel there to a width of 0. Try creating your labels based off of a frame that you know to be of a correct visible size.
To show your 2 labels, you can use this code:
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (indexPath.row == 0) {
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(11, 10, 280, 25)];
label1.backgroundColor = [UIColor clearColor];
label1.text = #"Your Text Here";
label1.textAlignment = UITextAlignmentRight;
[cell.contentView addSubview:label1];
[label1 release];
}
if (indexPath.row == 1) {
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(11, 10, 280, 25)];
label2.backgroundColor = [UIColor clearColor];
label2.text = #"Your Text Here";
label2.textAlignment = UITextAlignmentLeft;
[cell.contentView addSubview:label2];
[label2 release];
}
if (indexPath.row == 2) {
// Put your imageView code here.
}
Note: if you are using a tableViewCell taller than defaults size, 44.0, you can change the 25 number in the initWithFrame method of the labels.
Hope this can help you :)