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.
Related
I'm developing an iPhone Application, and I have a small issue, or so I think. I have a tableview and I got it to set the colors and labels of the cells by returning the cells in a method. Everything seems fine when I run it, the problem is when I scroll the cell's color changes to that of the cell below or above. I'm not sure how to solve this. I believe it's updating the cell when I scroll for some reason. How would I change this to only set the cell properties at first and not when I scroll?
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
tableCellCount ++;
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier];
UIColor * lightRed = [UIColor colorWithRed:0.6 green:0.1 blue:0 alpha:1.0];
UIColor * darkRed = [UIColor colorWithRed:0.7 green:0.1 blue:0 alpha:1.0];
UIColor * darkGray = [UIColor colorWithRed:0.15 green:0.15 blue:0.15 alpha:1.0];
NSLog(#"A");
if (tableCellCount % 2 == 0) {
cell.contentView.backgroundColor = darkRed;
cell.detailTextLabel.backgroundColor = darkRed;
cell.textLabel.backgroundColor = darkRed;
} else {
cell.contentView.backgroundColor = lightRed;
cell.detailTextLabel.backgroundColor = lightRed;
cell.textLabel.backgroundColor = lightRed;
}
cell.textLabel.textColor = [UIColor whiteColor];
UIView *bgColorView = [[UIView alloc] init];
[bgColorView setBackgroundColor:darkGray];
[cell setSelectedBackgroundView:bgColorView];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [listData objectAtIndex:row];
return cell;
}
Update
I figured out the solution to my problem.
Here's the updated code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [[[content objectAtIndex:indexPath.section] objectForKey:#"rowValues"]
objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:#"News_Icon#2x.png"];
UIColor * lightRed = [UIColor colorWithRed:0.7 green:0.1 blue:0 alpha:1.0];
UIColor * darkRed = [UIColor colorWithRed:0.6 green:0.1 blue:0 alpha:1.0];
UIColor * darkGray = [UIColor colorWithRed:0.15 green:0.15 blue:0.15 alpha:1.0];
if (row % 2 == 0) {
cell.contentView.backgroundColor = darkRed;
cell.detailTextLabel.backgroundColor = darkRed;
cell.textLabel.backgroundColor = darkRed;
} else {
cell.contentView.backgroundColor = lightRed;
cell.detailTextLabel.backgroundColor = lightRed;
cell.textLabel.backgroundColor = lightRed;
}
cell.textLabel.textColor = [UIColor whiteColor];
UIView *bgColorView = [[UIView alloc] init];
[bgColorView setBackgroundColor:darkGray];
[cell setSelectedBackgroundView:bgColorView];
for(UIView *view in [tableView subviews]) {
if([[[view class] description] isEqualToString:#"UITableViewIndex"]) {
[view setBackgroundColor:[UIColor clearColor]];
}
}
return cell;
}
For each cell in the table, there are properties you want to set the first time only when the cell gets created and there are other properties you want to set each time.
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:anyId];
if(cell==nil)
{
// Properties to set the first time when the cell is created only
}
// Properties to set each time
return cell;
You are reusing cell views. Check the method in your table view controller tableView:cellForRowAtIndexPath:and you for sure are using dequeueReusableCellWithIdentifier:. You have to set color values to that reused cell.
You have a problem with cells being reused. UITableViews recycle cells. So if you are not correctly handling this in your tableview:cellForRowAtIndex: method you will get strange results when you scroll. Always assume that the cell you are setting up may already be setup as another cell.
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,
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 =
I have added to some rows from table view an TextField like this :
- (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 = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
// cell.accessoryType = UITableViewCellAccessoryNone;
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==1)
{
code=[[UITextField alloc] init];
code.frame = CGRectMake(200,10,80,50);
code.textColor=[UIColor grayColor];
code.text=#"515800";
[cell.contentView addSubview:code];
}
if(indexPath.row==2)
{
ville=[[UITextField alloc] init];
ville.frame = CGRectMake(200,10,80,50);
ville.textColor=[UIColor grayColor];
ville.text=#"Paris";
[cell.contentView addSubview:ville];
}
if(indexPath.row==4)
{
nomdepartenaire=[[UITextField alloc] init];
nomdepartenaire.frame = CGRectMake(200,10,80,50);
nomdepartenaire.textColor=[UIColor grayColor];
nomdepartenaire.text=#"Alliantis Ttttt";
[cell.contentView addSubview:nomdepartenaire];
}
}
// Set up the cell...
[[cell textLabel] setText: [listData objectAtIndex:indexPath.row]] ;
return cell;
}
The problem is that the keyboard doesn't appear if I want to change the text from TextField. Why?
Most probably there is something in the way of the text field and that makes it not receiving the touch event. Adding the entire cellForRowAtIndexPath would probably help us understand better what's happenning. Also, you should add the texfield and other custom elements to cell.contentsView, not directly to the cell.
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.