App is crashing as soon as UITableView gets reloaded - iphone

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.

Related

Slow Scrolling in UITableviewCell

I have a tableview cell in a view which has some text in it,the verses of bible.but the scrolling is not at all smoother i have this code for UITableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [delegate.allSelectedVerseEnglish count];
}
return 0;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
readCell *cell = (readCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier
] autorelease];
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"readCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.malayalamVerse.hidden = YES;
cell.malayalamVerse.backgroundColor = [UIColor clearColor];
cell.textLabel.font = [UIFont fontWithName:#"Georgia" size:18.0];
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 0;
//pinch for serchpage
UIPinchGestureRecognizer *longPressRecognizer =
[[UIPinchGestureRecognizer alloc]
initWithTarget:self
action:#selector(longPressDetected:)];
[self.view addGestureRecognizer:longPressRecognizer];
[longPressRecognizer release];
//longtap for simpklepopupview
UILongPressGestureRecognizer *longPressgesture =
[[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:#selector(longPressDetectedgesture:)];
[self.view addGestureRecognizer:longPressgesture];
[longPressgesture release];
if (imagedarkbackground.hidden == NO) {
hideviewoftab.hidden =YES;
cell.chapterAndVerse.backgroundColor= [UIColor clearColor];
cell.chapterAndVerse.textColor = [UIColor whiteColor];
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.highlightedTextColor = [UIColor whiteColor];
//cell.textLabel.font = [UIFont fontWithName:#"Georgia" size:16];
}
else if (imagedarkbackground.hidden == YES){
hideviewoftab.hidden =NO;
cell.chapterAndVerse.backgroundColor= [UIColor whiteColor];
cell.chapterAndVerse.textColor = [UIColor brownColor];
cell.textLabel.textColor = [UIColor darkGrayColor];
cell.textLabel.highlightedTextColor = [UIColor darkGrayColor];
}
}
if(tableView == table)
{
UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
myBackView.backgroundColor = [UIColor colorWithRed:250.0 green:248.0 blue:192.0 alpha:1.0];
[myBackView setBackgroundColor:[UIColor colorWithRed:1 green:1 blue:0.75 alpha:1]];
cell.selectedBackgroundView = myBackView;
[myBackView release];
table.backgroundColor = [UIColor clearColor];
table.separatorColor = [UIColor clearColor];
cell.chapterAndVerse.text = [NSString stringWithFormat:#"%d",indexPath.row+1];
cell.chapterAndVerse.font = [UIFont fontWithName:#"Georgia" size:18.0];
cell.chapterAndVerse.frame=CGRectMake(0, 10, 30.0, 20.0);
cell.textLabel.text = [NSString stringWithFormat:#" %#",[delegate.allSelectedVerseEnglish objectAtIndex:indexPath.row]];
// cell.textLabel.textColor = [UIColor darkGrayColor];
cell.textLabel.font = [UIFont fontWithName:#"Georgia" size:18.0];
cell.backgroundColor = [UIColor clearColor];
}
return cell;
}
this is the cell height code
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGSize textSize = [[delegate.allSelectedVerseEnglish objectAtIndex:indexPath.row] sizeWithFont:[UIFont fontWithName:#"Georgia" size:18.0 ] constrainedToSize:CGSizeMake(290.0f,MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
CGSize textSizelabel = [[NSString stringWithFormat:#"%d",indexPath.row+1] sizeWithFont:[UIFont fontWithName:#"Georgia" size:17.0 ] constrainedToSize:CGSizeMake(290.0f,MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
return textSize.height +18;
return textSizelabel.height +18;
is there any mistake in my code that cause slow and sluggish scrolling.
Thanks in advance.
Following code has few fixes:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [delegate.allSelectedVerseEnglish count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"readCell";
readCell *cell = (readCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"readCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.malayalamVerse.hidden = YES;
cell.malayalamVerse.backgroundColor = [UIColor clearColor];
cell.textLabel.font = [UIFont fontWithName:#"Georgia" size:18.0];
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 0;
if (imagedarkbackground.hidden == NO)
{
hideviewoftab.hidden =YES;
cell.chapterAndVerse.backgroundColor= [UIColor clearColor];
cell.chapterAndVerse.textColor = [UIColor whiteColor];
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.highlightedTextColor = [UIColor whiteColor];
//cell.textLabel.font = [UIFont fontWithName:#"Georgia" size:16];
}
else if (imagedarkbackground.hidden == YES){
hideviewoftab.hidden =NO;
cell.chapterAndVerse.backgroundColor= [UIColor whiteColor];
cell.chapterAndVerse.textColor = [UIColor brownColor];
cell.textLabel.textColor = [UIColor darkGrayColor];
cell.textLabel.highlightedTextColor = [UIColor darkGrayColor];
}
}
//** If there is only one table then move the following case also inside the cell== nil clause with few minor changes....**/
if(tableView == table)
{
UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
myBackView.backgroundColor = [UIColor colorWithRed:250.0 green:248.0 blue:192.0 alpha:1.0];
[myBackView setBackgroundColor:[UIColor colorWithRed:1 green:1 blue:0.75 alpha:1]];
cell.selectedBackgroundView = myBackView;
[myBackView release];
table.backgroundColor = [UIColor clearColor];
table.separatorColor = [UIColor clearColor];
cell.chapterAndVerse.text = [NSString stringWithFormat:#"%d",indexPath.row+1];
cell.chapterAndVerse.font = [UIFont fontWithName:#"Georgia" size:18.0];
cell.chapterAndVerse.frame=CGRectMake(0, 10, 30.0, 20.0);
cell.textLabel.text = [NSString stringWithFormat:#" %#",[delegate.allSelectedVerseEnglish objectAtIndex:indexPath.row]];
// cell.textLabel.textColor = [UIColor darkGrayColor];
cell.textLabel.font = [UIFont fontWithName:#"Georgia" size:18.0];
cell.backgroundColor = [UIColor clearColor];
}
return cell;
}
Move out following code in more suitable method, as it has nothing to do with cell or tableview.
Also, review your code in if(tableView = table). If there is only one table then move the code where it fits.
//pinch for serchpage
UIPinchGestureRecognizer *longPressRecognizer =
[[UIPinchGestureRecognizer alloc]
initWithTarget:self
action:#selector(longPressDetected:)];
[self.view addGestureRecognizer:longPressRecognizer];
[longPressRecognizer release];
//longtap for simpklepopupview
UILongPressGestureRecognizer *longPressgesture =
[[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:#selector(longPressDetectedgesture:)];
[self.view addGestureRecognizer:longPressgesture];
[longPressgesture release];
I am not sure what you want to do in heightForRow method as it returns two values which is not possible.
Update
If you have two tableviews then you can use following code, as both tableviews will have separate cells that they will use so we need not set/reset other properties.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"readCell";
readCell *cell = (readCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"readCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.malayalamVerse.hidden = YES;
cell.malayalamVerse.backgroundColor = [UIColor clearColor];
cell.textLabel.font = [UIFont fontWithName:#"Georgia" size:18.0];
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 0;
if (imagedarkbackground.hidden == NO)
{
hideviewoftab.hidden =YES;
cell.chapterAndVerse.backgroundColor= [UIColor clearColor];
cell.chapterAndVerse.textColor = [UIColor whiteColor];
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.highlightedTextColor = [UIColor whiteColor];
//cell.textLabel.font = [UIFont fontWithName:#"Georgia" size:16];
}
else if (imagedarkbackground.hidden == YES){
hideviewoftab.hidden =NO;
cell.chapterAndVerse.backgroundColor= [UIColor whiteColor];
cell.chapterAndVerse.textColor = [UIColor brownColor];
cell.textLabel.textColor = [UIColor darkGrayColor];
cell.textLabel.highlightedTextColor = [UIColor darkGrayColor];
}
if(tableView == table1)
{
UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
myBackView.backgroundColor = [UIColor colorWithRed:250.0 green:248.0 blue:192.0 alpha:1.0];
[myBackView setBackgroundColor:[UIColor colorWithRed:1 green:1 blue:0.75 alpha:1]];
cell.selectedBackgroundView = myBackView;
[myBackView release];
table.backgroundColor = [UIColor clearColor];
table.separatorColor = [UIColor clearColor];
cell.chapterAndVerse.text = [NSString stringWithFormat:#"%d",indexPath.row+1];
cell.chapterAndVerse.font = [UIFont fontWithName:#"Georgia" size:18.0];
cell.chapterAndVerse.frame=CGRectMake(0, 10, 30.0, 20.0);
cell.textLabel.font = [UIFont fontWithName:#"Georgia" size:18.0];
cell.backgroundColor = [UIColor clearColor];
}
else if (tableView == table2)
{
// do something if needed... else leave it
}
}
if(tableView == table1)
{
cell.textLabel.text = [NSString stringWithFormat:#" %#",[delegate.allSelectedVerseEnglish objectAtIndex:indexPath.row]];
}
else if(tableView == table2)
{
// set text or changeable properties here... for table 2
}
return cell;
}
heightForRowAtIndexPath:
For two tables use following code(with your changes):
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat height = 0.0;
if (tableView = table1)
{
CGSize textSize = [[delegate.allSelectedVerseEnglish objectAtIndex:indexPath.row] sizeWithFont:[UIFont fontWithName:#"Georgia" size:18.0 ] constrainedToSize:CGSizeMake(290.0f,MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
height = textSize.height +18;
}
else if (tableView = table1)
{
CGSize textSizelabel = [[NSString stringWithFormat:#"%d",indexPath.row+1] sizeWithFont:[UIFont fontWithName:#"Georgia" size:17.0 ] constrainedToSize:CGSizeMake(290.0f,MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
height = textSizelabel.height +18;
}
return height;
}
Thanks,

UITableView content height issue causing application crash

I have two UITableViews in my view which shows same contents(verses of bible) but in diffrent language,top tableview shows english and bottom-table shows hindi.everything works fine,but some chapters the verse loads the data in uitableview the application crashes,the error is in this area
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGSize textSize = [[delegate.allSelectedVerseEnglish objectAtIndex:indexPath.row] sizeWithFont:[UIFont fontWithName:#"Georgia" size:18.0 ] constrainedToSize:CGSizeMake(280.0f,MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
return textSize.height +20;
CGSize textSizehindi = [[tempArray objectAtIndex:indexPath.row] sizeWithFont:[UIFont fontWithName:#"testfont" size:18.0 ] constrainedToSize:CGSizeMake(280.0f,MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
return textSizehindi.height +20;
}
and also after playing too much time in uitableview ,it cause the slow down of scrolling of UITableViewCells.And there is no smooth scrolling occours.Is there any eroor in above code which i get error while loading some chapters.
Thanks in advance.
EDIT:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
readCell *cell = (readCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"readCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 0;
}
if(tableView == table)
{
UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
myBackView.backgroundColor = [UIColor colorWithRed:250.0 green:248.0 blue:192.0 alpha:1.0];
[myBackView setBackgroundColor:[UIColor colorWithRed:1 green:1 blue:0.75 alpha:1]];
cell.selectedBackgroundView = myBackView;
[myBackView release];
table.backgroundColor = [UIColor clearColor];
table.separatorColor = [UIColor clearColor];
cell.chapterAndVerse.text = [NSString stringWithFormat:#"%d",indexPath.row+1];
cell.chapterAndVerse.font = [UIFont fontWithName:#"Georgia" size:17.0];
cell.chapterAndVerse.frame=CGRectMake(0, 10, 30.0, 20.0);
cell.textLabel.text = [NSString stringWithFormat:#" %#",[delegate.allSelectedVerseEnglish objectAtIndex:indexPath.row]];
cell.textLabel.font = [UIFont fontWithName:#"Georgia" size:18];
cell.backgroundColor = [UIColor clearColor];
}
else if(tableView == tab)
{
UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
myBackView.backgroundColor = [UIColor colorWithRed:250.0 green:248.0 blue:192.0 alpha:1.0];
[myBackView setBackgroundColor:[UIColor colorWithRed:1 green:1 blue:0.75 alpha:1]];
cell.selectedBackgroundView = myBackView;
[myBackView release];
tab.backgroundColor = [UIColor clearColor];
tab.separatorColor = [UIColor clearColor];
cell.chapterAndVerse.text = [NSString stringWithFormat:#"%d",indexPath.row+1];
cell.chapterAndVerse.font = [UIFont fontWithName:#"Georgia" size:17.0];
cell.chapterAndVerse.frame=CGRectMake(0, 10, 30.0, 20.0);
cell.textLabel.text = [NSString stringWithFormat:#" %#",[tempArray objectAtIndex:indexPath.row]];
cell.textLabel.font = [UIFont fontWithName:#"testfont" size:18];
cell.backgroundColor = [UIColor clearColor];
}
return cell;
}
EDIT2
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == table) {
return [delegate.allSelectedVerseEnglish count];
}
else if (tableView == tab )
{
return [tempArray count];
}
}
first do one thing. before use of your both array, NSLog the both array before reloading table. And check both array have same number of objects. it may be a reason of crashing.
in the above code first add the conditions for returning the text size for english and hindi verse. because currently you are always returning the text size of first english verse.The main problem you are facing is not in your above code.The problem should be on your another table view delegate call:- CellForRowAtIndexPath.
Can you paste your CellForRowAtIndexPath delegate call here so that i can give you more idea.
2nd part of your heightForRowAtIndexPath method is never called. It allways returning return textSize.height +20; Most possible way to crash is null pointer delegate.allSelectedVerseEnglish or unexisting font.

Send text from tableview to textfield

I need to do this: when a user select a row from an tableview to send the text from that row to a textfield from an other tableview.
For example, if I select Services from
I want to see Services here,near Type de Partenaire :
I tried this :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
NSString *cellText = selectedCell.textLabel.text;
NSLog(#"%#",cellText);
self.recherchePartenaireTableView.partenaire.text=cellText;
}
and when Button ok is pressed :
-(IBAction)OkButtonPressed:(id)sender{
NSLog(#"BTN Ok");
[self.recherchePartenaireTableView.tableviewrecherchepartenaire reloadData];
[self.navigationController popViewControllerAnimated:YES];
}
but this is not working. Can anyone help me? Thanks in advance..
in implementation file for first image
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
cell.backgroundColor = [UIColor clearColor];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
cell.backgroundView.opaque = NO;
//cell.alpha = 0.65;
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.opaque = NO;
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.highlightedTextColor = [UIColor whiteColor];
cell.textLabel.font = [UIFont boldSystemFontOfSize:18];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.opaque = NO;
cell.detailTextLabel.textColor = [UIColor whiteColor];
cell.detailTextLabel.highlightedTextColor = [UIColor whiteColor];
cell.detailTextLabel.font = [UIFont systemFontOfSize:14];
}
// Set up the cell...
[[cell textLabel] setText: [typePartenaireArray objectAtIndex:indexPath.row]] ;
return cell;
}
** in implementation file for second image**
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.backgroundColor = [UIColor clearColor];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
cell.backgroundView.opaque = NO;
//cell.alpha = 0.65;
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.opaque = NO;
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.highlightedTextColor = [UIColor whiteColor];
cell.textLabel.font = [UIFont boldSystemFontOfSize:18];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.opaque = NO;
cell.detailTextLabel.textColor = [UIColor whiteColor];
cell.detailTextLabel.highlightedTextColor = [UIColor whiteColor];
cell.detailTextLabel.font = [UIFont systemFontOfSize:14];
if(indexPath.row==0)
{
partenaire=[[UITextField alloc] init];
partenaire.frame = CGRectMake(200,10,80,50);
partenaire.textColor=[UIColor grayColor];
partenaire.text=#"Tous";
[partenaire setKeyboardAppearance:NO];
[cell.contentView addSubview:partenaire];
}
}
// Set up the cell...
[[cell textLabel] setText: [arraytableview objectAtIndex:indexPath.row]] ;
return cell;
}
Override the init method of the second UITableView to accept an extra argument (in your case the text of the cell selected in the current UITableView). And in the second UITableView where you configure the cells, set their text as you wish with this parameter you just received from the previous tableView which the second tableview object was alloc initted.
You have to insert your text into your array arraytableview in position 1 (arrays start at 0). Make sure it is declared as NSMutableArray otherwise you can't change it.
So you have to do: [arraytableview insertObject: cellText atIndex: 1] in your OK method. For this you might have to introduce a variable that holds the currently selected text to "transfer" it from the selected method to the OK method.
Or you just add it in your - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath, but then it would add the text immediately without pressing OK. really depends what you want
I hope it's clear now.
Code to define UITableViewCell with multiple lines, textfields, etc:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
imageView = [[UIImageView alloc] initWithFrame: CGRectZero];
imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.contentView addSubview: imageView];
titleLabel = [[UILabel alloc] initWithFrame: CGRectZero];
[titleLabel setFont:[UIFont systemFontOfSize:14.0]];
[titleLabel setTextColor:[UIColor blackColor]];
[titleLabel setHighlightedTextColor:[UIColor darkGrayColor]];
[titleLabel setLineBreakMode: UILineBreakModeWordWrap];
titleLabel.numberOfLines = 2;
[self.contentView addSubview: titleLabel];
description = [[UITextField alloc] initWithFrame: CGRectZero];
[description setFont:[UIFont systemFontOfSize:12.0]];
[description setTextColor:[UIColor darkGrayColor]];
[description setHighlightedTextColor:[UIColor darkGrayColor]];
[self.contentView addSubview: description];
}
return self;
}
-(void) layoutSubviews {
[super layoutSubviews];
// D_IN;
[imageView setFrame:CGRectMake(8.0, 10.0, 20.0, 20.0)];
[titleLabel setFrame:CGRectMake(40.0, 1.0, 250.0, 40.0)]; //two lines
[description setFrame:CGRectMake(40.0, 37.0, 250.0, 3.0)];//not used
// D_OUT;
}
Make sure that the layout is correct when specifying the starting x and y for each new component otherwise you won't see anything!!!
Then just set it with
titleLabel.text =
or
description.text =

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];
}

UITableView SectionHeader not displaying custom image background

I'm modifying the look of my TableView's section header. I've managed to get the text working just fine. But the the background image doesn't seem to be showing up at all.
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 30)] autorelease];
UILabel *sectionTitle = [[[UILabel alloc] initWithFrame:CGRectMake(10, 0, 320, 30)] autorelease];
sectionTitle.text = [[tableDataSource objectAtIndex: section] objectForKey: #"Title"];
sectionTitle.font = [UIFont fontWithName:#"Helvetica-Bold" size:14];
//sectionTitle.textColor = [UIColor whiteColor];
sectionTitle.shadowColor = [UIColor colorWithWhite:0 alpha:0.4];
sectionTitle.shadowOffset = CGSizeMake(1, 1);
sectionTitle.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
//headerView.backgroundColor = [UIColor whiteColor];
UIImageView *sectionHeaderBG = [[UIImageView alloc] init];
UIImage *image = [UIImage imageNamed:#"CellBackgroundGrey4.png"];
sectionHeaderBG.image = image;
[headerView addSubview:sectionTitle];
[headerView addSubview:sectionHeaderBG];
[headerView autorelease];
return headerView;
}
Is there something I'm missing?
Give it a try:
headerView.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"CellBackgroundGrey4.png"]];
I think you missed setting the frame of the UIImageView.
//custom sections
- (NSString *)tableView:(UITableView *)tblView titleForHeaderInSection:(NSInteger)section {
NSString *sectionName = nil;
//set the table background to clear so you can see the background view behind it
tableView.backgroundColor = [UIColor clearColor];
//where does this go?
UILabel *sectionHeader = [[UILabel alloc] init];
sectionHeader.backgroundColor = [UIColor clearColor];
sectionHeader.font = [UIFont boldSystemFontOfSize:18];
sectionHeader.textColor = [UIColor whiteColor];
//What is missing here?
switch (section) {
case 0:
sectionName = [NSString stringWithFormat:#"Header Text 1"];
break;
case 1:
sectionName = [NSString stringWithFormat:#"Header Text 2"];
break;
case 2:
sectionName = [NSString stringWithFormat:#"Header Text 3"];
break;
}
return sectionName;