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];
}
Related
I have a UITableView where I am loading images from the sever. But sometimes there are no images to display on the UITableView and at that I want to display UILabel. Wondering how would I accomplish this. I would appreciate any help or code snippets to achieve this.
Thank you very much!
I tried what you said. Everything works fine for the first time when you load the table, but as soon as you start scrolling all the labels and button go all over the places.
Here is my code.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
if (msgImgFile){
NSLog (#"Image file found!");
lblOne = [[UILabel alloc] initWithFrame:CGRectMake(10, 360, 200, 20)];
lblTwo = [[UILabel alloc] initWithFrame:CGRectMake(10, 378, 150, 20)];
lblThree = [[UILabel alloc] initWithFrame:CGRectMake(10, 398, 150, 20)];
btnPlayStop.frame = CGRectMake(255.0f, 375.0f, 30.0f, 30.0f);
}
else
{
NSLog(#"Image file not found. Simply load the UILabel and UIButton");
lblOne = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, 200, 20)];
lblTwo = [[UILabel alloc] initWithFrame:CGRectMake(10, 68, 150, 20)];
lblThree = [[UILabel alloc] initWithFrame:CGRectMake(10, 88, 150, 20)];
btnPlayStop.frame = CGRectMake(255.0f, 45.0f, 30.0f, 30.0f);
}
lblOne.font = [UIFont fontWithName:#"Arial" size:12];
[lblOne setBackgroundColor:[UIColor clearColor]];
lblOne.tag = 1;
lblTwo.font = [UIFont fontWithName:#"Arial" size:12];
[lblTwo setBackgroundColor:[UIColor clearColor]];
lblTwo.tag = 2;
lblThree.font = [UIFont fontWithName:#"Arial" size:10];
[lblThree setBackgroundColor:[UIColor clearColor]];
lblThree.tag = 3;
lblFour = [[UILabel alloc] initWithFrame:CGRectMake(10, 24, 150, 20)];
lblFour.font = [UIFont fontWithName:#"Arial" size:12];
[lblFour setBackgroundColor:[UIColor clearColor]];
lblFour.tag = 4;
btnPlayStop = [UIButton buttonWithType:UIButtonTypeCustom];
[btnPlayStop setTitle:#"Play" forState:UIControlStateNormal];
[btnPlayStop setImage:[UIImage imageNamed:#"Play Button.png"] forState:UIControlStateNormal];
[btnPlayStop addTarget:self action:#selector(playRecordClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:lblOne];
[cell addSubview:lblTwo];
[cell addSubview:lblThree];
[cell addSubview:lblFour];
[cell.contentView addSubview:btnPlayStop];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
dispatch_async(queue, ^{
msgObjImg = (PFObject *)[self.imageDataMutArray objectAtIndex:indexPath.row];
createdDt = msgObjImg.createdAt;
msgImgFile = [msgObjImg objectForKey:#"siqImage"];
NSData *imgData = [msgImgFile getData];
UIImage *msgImgFound = [UIImage imageWithData:imgData];
UIImage *newImg = [self scaleImage:msgImgFound toSize:CGSizeMake(280.0, 300.0)];
dispatch_sync(dispatch_get_main_queue(), ^{
UILabel *dtTimeLabel = (UILabel *)[cell viewWithTag:3];
NSDateFormatter *dtFormat = [[NSDateFormatter alloc]init];
[dtFormat setDateFormat:#"MM-dd-yyyy HH:mm"];
[dtFormat setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:-18000]];
NSString *createdDtString = [dtFormat stringFromDate:createdDt];
dtTimeLabel.text = [NSString stringWithFormat:#"Received on: %#",createdDtString];
[[cell imageView] setImage:newImg];
[cell setNeedsLayout];
}
return cell;
}
I can see where the problem is, and can tell you the right procedure to solve this.
- (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];
lblOne = [[UILabel alloc]init];
lblOne.tag = 1;
lblTwo = [[UILabel alloc]init];
lblTwo.tag = 2;
lblThree = [[UILabel alloc]init];
lblThree.tag = 3;
lblFour = [[UILabel alloc]init];
lblFour.tag = 4;
btnPlayStop = [[UILabel alloc]init];
btnPlayStop.tag = 5;
// Perform addition functions ( your requirements )
[cell.contentView addSubview:lblOne];
[cell.contentView addSubview:lblTwo];
[cell.contentView addSubview:lblThree];
[cell.contentView addSubview:lblFour];
[cell.contentView addSubview:btnPlayStop];
}
else
{
lblOne = (UILabel*)[cell.contentView viewWithTag:1];
lblTwo = (UILabel*)[cell.contentView viewWithTag:2];
lblThree = (UILabel*)[cell.contentView viewWithTag:3];
lblFour = (UILabel*)[cell.contentView viewWithTag:4];
btnPlayStop = (UILabel*)[cell.contentView viewWithTag:5];
// AND SO ON ...
}
// SET THE VALUES HERE FOR YOUR CONTENT VALUES
return cell;
}
You need to create the cells using dynamic content views.
Try the above snippet, and modify according to your own..
In your cellForRowAtIndexPath method
UIImage *image = [imagesArray objectAtIndex:indexPath.row];
if (image) {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:yourImageViewFrame];//create desired Frame
imageView.image = image;
[cell addSubview:imageView];
} else {
UILabel *label = [[UILabel alloc] initWithFrame:yourLabelFrame];//create desired frame
label.text = #"No Image";
[cell addSubview:label];
}
When your images are finished loading from the server, call [tableView reloadData];
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
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.
I'm developing an app where TableView needs to reload as soon as the login process gets completed. The app crashes with error EXC_BAD_ACCESS when the table data gets reloaded. It doesn't crash when I remove all case instances except case 0:
What could be the reason behind it?
Here's the code:
- (void)viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(loginDone:)
name:#"loginDone" object:nil];
statTableView.backgroundColor = [UIColor clearColor];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)
section {
return 6;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell.
switch (indexPath.row) {
case 0 :
cell.textLabel.text = #"Foo:";
NSLog(#"%#", data7);
UILabel *myLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(200, 10, 20, 30)];
myLabel2.text = (#"%#", data7);
myLabel2.textColor = [UIColor blackColor];
myLabel2.backgroundColor = [UIColor whiteColor];
myLabel2.font = [UIFont fontWithName:#"Trebuchet MS" size:14];
[cell.contentView addSubview:myLabel2];
break;
case 1:
cell.textLabel.text = #"Foo: ";
UILabel *myLabel4 = [[UILabel alloc] initWithFrame:CGRectMake(200, 10, 20, 30)];
myLabel4.text = (#"%#", data11);
myLabel4.textColor = [UIColor blackColor];
myLabel4.backgroundColor = [UIColor whiteColor];
myLabel4.font = [UIFont fontWithName:#"Trebuchet MS" size:14];
[cell.contentView addSubview:myLabel4];
break;
case 2:
cell.textLabel.text = #"Foo: ";
UILabel *myLabel8 = [[UILabel alloc] initWithFrame:CGRectMake(200, 10, 20, 30)];
myLabel8.text = (#"%#", data3);
myLabel8.textColor = [UIColor blackColor];
myLabel8.backgroundColor = [UIColor whiteColor];
myLabel8.font = [UIFont fontWithName:#"Trebuchet MS" size:14];
[cell.contentView addSubview:myLabel8];
break;
case 3:
cell.textLabel.text = #"Foo: ";
UILabel *myLabel10 = [[UILabel alloc] initWithFrame:CGRectMake(200, 10, 20, 30)];
myLabel10.text = [NSString stringWithFormat:#"%#", data4];
if ([data4 isEqualToString:#"0"]) {
myLabel10.text = #"None";
}
myLabel10.textColor = [UIColor blackColor];
myLabel10.backgroundColor = [UIColor whiteColor];
myLabel10.font = [UIFont fontWithName:#"Trebuchet MS" size:14];
[cell.contentView addSubview:myLabel10];
break;
case 4:
cell.textLabel.text = #"Foo: ";
UILabel *myLabel12 = [[UILabel alloc] initWithFrame:CGRectMake(200, 10, 20, 30)];
myLabel12.text = [NSString stringWithFormat:#"%#", data5];
myLabel12.textColor = [UIColor blackColor];
if ([data5 isEqualToString:#"Foo"]) {
myLabel12.textColor = [UIColor redColor];
myLabel12.text = #"Nil";
}
myLabel12.backgroundColor = [UIColor whiteColor];
myLabel12.font = [UIFont fontWithName:#"Trebuchet MS" size:14];
[cell.contentView addSubview:myLabel12];
break;
case 5:
cell.textLabel.text = #"Foo: ";
UILabel *myLabel14 = [[UILabel alloc] initWithFrame:CGRectMake(200, 10, 50, 30)];
if ([data6 isEqualToString:#"Foo"]) {
myLabel14.textColor = [UIColor colorWithRed:(0/255.f) green:(100/255.f) blue:(0/255.f) alpha:1.0];
myLabel14.text = #"No Dues";
} else {
myLabel14.text = [NSString stringWithFormat:#"%#", data6];
myLabel14.textColor = [UIColor redColor];
}
myLabel14.backgroundColor = [UIColor whiteColor];
myLabel14.font = [UIFont fontWithName:#"Trebuchet MS" size:14];
[cell.contentView addSubview:myLabel14];
break;
/*
[myLabel2 release];
[myLabel4 release];
[myLabel8 release];
[myLabel10 release];
[myLabel12 release];
[myLabel14 release];
*/
}
return cell;
}
You need to do some basic debugging here. First add break points and go line by line in the debugger until you find the line where the bad exec is happening. Once you have that you will be able to quickly figure out why.
Your codes have some problems.
First, the following method needs an autorelease UITableViewCell object.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
Second, the cell maybe come from [tableView dequeueReusableCellWithIdentifier:CellIdentifier], it already has a custom label which you has added. And you will add more custom label to this kind cell. You can sub-class this kind cell, and obtain the desired UILabel by property.
What are the dataX variables? If they are getting released and not set to nil, you will be calling methods on deallocated objects, which would cause EXC_BAD_ACCESS.
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 :)