UILabel in UITableViewCell not drawing until cell is at top of screen - iphone

I have strange problem with UILabel in a UITableViewCell. It does not appear until I pull the screen down so that the UITableViewCell is at the top. Here is a video of the problem.
http://youtu.be/apTJd1Y4RZk
HEre is the code when the cell is created.
TVC_Location_View_Text *cell = (TVC_Location_View_Text *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"TVC_Location_View_Text" owner:nil options: nil];
for(id currentObject in topLevelObjects) {
if([currentObject isKindOfClass:[UITableViewCell class]]) {
cell = ((TVC_Location_View_Text *) currentObject);
break;
}
}
}
UIFont *cellFont = [UIFont fontWithName:#"Helvetica" size:15.0];
CGSize constraintSize = CGSizeMake(300.0f, MAXFLOAT);
CGSize labelSize = [self.sLocationText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
CGRect rect = CGRectMake(10, 10, labelSize.width, labelSize.height);
UILabel* labelText = [[UILabel alloc] initWithFrame:rect];
[labelText setFont:cellFont];
[labelText setLineBreakMode:UILineBreakModeWordWrap];
[labelText setNumberOfLines:0];
[labelText setBackgroundColor:[UIColor clearColor]];
[labelText setTextColor:appDelegate.colorDarkText];
[cell addSubview:labelText];
[labelText setText:self.sLocationText];
return cell;

Creation of the UILabel should be inside the creation of the cell itself
This is an issue, i dont know if it will resolve your bug, but it still is a bug,
Please change your code to
TVC_Location_View_Text *cell = (TVC_Location_View_Text *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel* labelText;
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"TVC_Location_View_Text" owner:nil options: nil];
for(id currentObject in topLevelObjects) {
if([currentObject isKindOfClass:[UITableViewCell class]]) {
cell = ((TVC_Location_View_Text *) currentObject);
break;
}
}
labelText = [[UILabel alloc] initWithFrame:rect];
[labelText setFont:cellFont];
[labelText setLineBreakMode:UILineBreakModeWordWrap];
[labelText setNumberOfLines:0];
[labelText setBackgroundColor:[UIColor clearColor]];
[labelText setTextColor:appDelegate.colorDarkText];
labelText.tag = 111;
[cell addSubview:labelText];
}
labelText = (UILabel*)[cell viewWithTag:111];
UIFont *cellFont = [UIFont fontWithName:#"Helvetica" size:15.0];
CGSize constraintSize = CGSizeMake(300.0f, MAXFLOAT);
CGSize labelSize = [self.sLocationText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
CGRect rect = CGRectMake(10, 10, labelSize.width, labelSize.height);
[labelText setText:self.sLocationText];
return cell;
Try this code, and see if it works out

You should use -[UITableViewDelegate willDisplayCell:forRowAtIndexPath:] delegate method.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TVC_Location_View_Text *cell = (TVC_Location_View_Text *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel* labelText;
if (cell == nil)
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"TVC_Location_View_Text" owner:nil options: nil];
for(id currentObject in topLevelObjects)
{
if([currentObject isKindOfClass:[UITableViewCell class]])
{
cell = ((TVC_Location_View_Text *) currentObject);
break;
}
}
labelText = [[UILabel alloc] initWithFrame:rect];
[labelText setFont:cellFont];
[labelText setLineBreakMode:UILineBreakModeWordWrap];
[labelText setNumberOfLines:0];
[labelText setBackgroundColor:[UIColor clearColor]];
[labelText setTextColor:appDelegate.colorDarkText];
labelText.tag = 111;
[cell addSubview:labelText];
}
return cell;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
UILabel* labelText = (UILabel*)[cell viewWithTag:111];
UIFont *cellFont = [UIFont fontWithName:#"Helvetica" size:15.0];
CGSize constraintSize = CGSizeMake(300.0f, MAXFLOAT);
CGSize labelSize = [self.sLocationText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
CGRect rect = CGRectMake(10, 10, labelSize.width, labelSize.height);
[labelText setText:self.sLocationText];
}

Related

UITableView Scrolling slowness issues when loading cell

I am having issues with the speed in which my tableview Cells scroll. You can see that it hangs up when dequeuing and reusing a cell. Below is my code that I use to create the cells. I am using 2 custom cells and one cell is if there is a image and the other cell is if the user didn't attach an image. Any insight would be greatly appreciated. I should also add that I wanted to add a space between the cells so basically I create a new section containing a single cell and that is why you will always see in my code indexPath.section.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *imageCell = #"ShameImage";
static NSString *noImageCell = #"ShameNoImageCell";
static NSString *noCell = #"NoCell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:noCell];
if ([[blobID objectAtIndex:indexPath.section] isEqualToNumber:[NSNumber numberWithInt:1]])
{
ShameViewCell *cell = (ShameViewCell *)[self.tableView dequeueReusableCellWithIdentifier:imageCell];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ShameImageCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
CALayer *cellImageView = [cell.imageView layer];
[cellImageView setMasksToBounds:YES];
[cellImageView setCornerRadius:10.0];
IndicatorImageView *iiv = [[IndicatorImageView alloc] initWithFrame:CGRectMake(0, 0, 88, 88)];
[iiv setShameID:[[shameID objectAtIndex:indexPath.section] stringValue]];
iiv.tag = 999;
[iiv loadImageFromURL];
[cell.imageView addSubview:iiv];
cell.imageView.userInteractionEnabled = YES;
UITapGestureRecognizer *tapImage = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(openPicture:)];
tapImage.numberOfTapsRequired = 1;
[cell.imageView addGestureRecognizer:tapImage];
[cell.contentView.layer setCornerRadius:10];
[cell.contentView setBackgroundColor:[UIColor blackColor]];
[cell.contentView setAlpha:0.7f];
UIColor *insideColor = [UIColor colorWithRed:color_red green:color_green blue:color_blue alpha:1];
cell.lastShame.contentInset = UIEdgeInsetsMake(-11, -8, 0, 0);
[cell.lastShame setTextColor:insideColor];
[cell.lastShame setFont:[UIFont fontWithName:text_font_name size:14]];
cell.lastShame.text = [userShame objectAtIndex:indexPath.section];
[cell.shameDate setTextColor:insideColor];
[cell.shameDate setFont:[UIFont fontWithName:text_font_name size:11]];
cell.shameDate.text = [createDt objectAtIndex:indexPath.section];
[cell.userLabel setTextColor:insideColor];
[cell.userLabel setFont:[UIFont fontWithName:text_font_name size:11]];
cell.userLabel.text = [#"Post By: " stringByAppendingString:[userName objectAtIndex:indexPath.section]];
[cell.lastShame setBackgroundColor:[UIColor clearColor]];
return cell;
}
}else
{
ShameNoImage *cell = (ShameNoImage *)[self.tableView dequeueReusableCellWithIdentifier:noImageCell];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ShameNoImageCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
CALayer *cellImageView = [cell.imageView layer];
[cellImageView setMasksToBounds:YES];
[cellImageView setCornerRadius:10.0];
[cellImageView setBorderWidth:1.0];
[cellImageView setBorderColor:[[UIColor whiteColor] CGColor]];
[cell.contentView.layer setCornerRadius:10];
[cell.contentView setBackgroundColor:[UIColor blackColor]];
[cell.contentView setAlpha:0.7f];
UIColor *insideColor = [UIColor colorWithRed:color_red green:color_green blue:color_blue alpha:1];
cell.shameLabel.contentInset = UIEdgeInsetsMake(-11, -8, 0, 0);
[cell.shameLabel setTextColor:insideColor];
[cell.shameLabel setFont:[UIFont fontWithName:text_font_name size:14]];
cell.shameLabel.text = [userShame objectAtIndex:indexPath.section];
[cell.dateLabel setTextColor:insideColor];
[cell.dateLabel setFont:[UIFont fontWithName:text_font_name size:11]];
cell.dateLabel.text = [createDt objectAtIndex:indexPath.section];
[cell.userLabel setTextColor:insideColor];
[cell.userLabel setFont:[UIFont fontWithName:text_font_name size:11]];
cell.userLabel.text = [#"Post By: " stringByAppendingString:[userName objectAtIndex:indexPath.section]];
[cell.shameLabel setBackgroundColor:[UIColor clearColor]];
return cell;
}
}
return cell;
}
The first thing that jumps out are the calls to setCornerRadius. A quick test would be to cut out that corner radius stuff to see if that helps your speed. If that's you're problem you'll have to switch to drawing with CG and a UIBezierPath.
The next thing to look at would be transparency and the number of subviews you are using. It's hard to tell with what is posted above but the more subviews, especially with alpha are going to mean more work compositing. But that can be hard to get around depending on your design but drawing with CG or using images can help.
I figured it out. I was recreating and drawing every cell. I needed to set the reuseIdentifier in the Cell's Subclass and then change the building the the table to be like this.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *imageCell = #"ShameImage";
static NSString *noImageCell = #"ShameNoImageCell";
ShameViewCell *iCell = (ShameViewCell *)[self.tableView dequeueReusableCellWithIdentifier:imageCell];
ShameNoImage *niCell = (ShameNoImage *)[self.tableView dequeueReusableCellWithIdentifier:noImageCell];
if ([[blobID objectAtIndex:indexPath.section] isEqualToNumber:[NSNumber numberWithInt:1]])
{
if (iCell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ShameImageCell" owner:self options:nil];
iCell = [nib objectAtIndex:0];
CALayer *cellImageView = [iCell.imageView layer];
[cellImageView setMasksToBounds:YES];
IndicatorImageView *iiv = [[IndicatorImageView alloc] initWithFrame:CGRectMake(0, 0, 88, 88)];
[iiv setShameID:[[shameID objectAtIndex:indexPath.section] stringValue]];
iiv.tag = 999;
[iiv loadImageFromURL];
[iCell.imageView addSubview:iiv];
iCell.imageView.userInteractionEnabled = YES;
UITapGestureRecognizer *tapImage = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(openPicture:)];
tapImage.numberOfTapsRequired = 1;
[iCell.imageView addGestureRecognizer:tapImage];
[iCell.contentView setBackgroundColor:[UIColor blackColor]];
[iCell.contentView setAlpha:0.7f];
UIColor *insideColor = [UIColor colorWithRed:color_red green:color_green blue:color_blue alpha:1];
iCell.lastShame.contentInset = UIEdgeInsetsMake(-11, -8, 0, 0);
[iCell.lastShame setTextColor:insideColor];
[iCell.lastShame setFont:[UIFont fontWithName:text_font_name size:14]];
iCell.lastShame.text = [userShame objectAtIndex:indexPath.section];
[iCell.shameDate setTextColor:insideColor];
[iCell.shameDate setFont:[UIFont fontWithName:text_font_name size:11]];
iCell.shameDate.text = [createDt objectAtIndex:indexPath.section];
[iCell.userLabel setTextColor:insideColor];
[iCell.userLabel setFont:[UIFont fontWithName:text_font_name size:11]];
iCell.userLabel.text = [#"Post By: " stringByAppendingString:[userName objectAtIndex:indexPath.section]];
return iCell;
}else
{
[[iCell.imageView viewWithTag:999] removeFromSuperview];
IndicatorImageView *iiv = [[IndicatorImageView alloc] initWithFrame:CGRectMake(0, 0, 88, 88)];
[iiv setShameID:[[shameID objectAtIndex:indexPath.section] stringValue]];
iiv.tag = 999;
[iiv loadImageFromURL];
[iCell.imageView addSubview:iiv];
iCell.lastShame.text = [userShame objectAtIndex:indexPath.section];
iCell.shameDate.text = [createDt objectAtIndex:indexPath.section];
iCell.userLabel.text = [#"Post By: " stringByAppendingString:[userName objectAtIndex:indexPath.section]];
return iCell;
}
}else
{
if (niCell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ShameNoImageCell" owner:self options:nil];
niCell = [nib objectAtIndex:0];
[niCell.contentView setBackgroundColor:[UIColor blackColor]];
[niCell.contentView setAlpha:0.7f];
UIColor *insideColor = [UIColor colorWithRed:color_red green:color_green blue:color_blue alpha:1];
niCell.shameLabel.contentInset = UIEdgeInsetsMake(-11, -8, 0, 0);
[niCell.shameLabel setTextColor:insideColor];
[niCell.shameLabel setFont:[UIFont fontWithName:text_font_name size:14]];
niCell.shameLabel.text = [userShame objectAtIndex:indexPath.section];
[niCell.dateLabel setTextColor:insideColor];
[niCell.dateLabel setFont:[UIFont fontWithName:text_font_name size:11]];
niCell.dateLabel.text = [createDt objectAtIndex:indexPath.section];
[niCell.userLabel setTextColor:insideColor];
[niCell.userLabel setFont:[UIFont fontWithName:text_font_name size:11]];
niCell.userLabel.text = [#"Post By: " stringByAppendingString:[userName objectAtIndex:indexPath.section]];
return niCell;
}else
{
niCell.shameLabel.text = [userShame objectAtIndex:indexPath.section];
niCell.dateLabel.text = [createDt objectAtIndex:indexPath.section];
niCell.userLabel.text = [#"Post By: " stringByAppendingString:[userName objectAtIndex:indexPath.section]];
return niCell;
}
}
return niCell;
}

text displaying out of UiTableViewCell section

My UITableview is displaying the data, but the text in one section is moving out of the box and displaying in other section..i don't know why?
Here is my code:
if(indexPath.section == 0){
cell.textLabel.font = [UIFont boldSystemFontOfSize:14];
cell.textLabel.textColor = [UIColor blackColor];
cell.detailTextLabel.numberOfLines=3;
cell.detailTextLabel.textColor=[UIColor colorWithRed:0.40 green:0.40 blue:0.40 alpha:1.0];
cell.textLabel.text=[NSString stringWithFormat:#"School Hours:"];
cell.detailTextLabel.text=[NSString stringWithFormat:#"Take in time: %# \nDissmiss time:%# \nPhone No: %# ",[array objectAtIndex:indexPath.row],[array1 objectAtIndex:indexPath.row],[array2 objectAtIndex:indexPath.row]];
cell.textLabel.backgroundColor=[UIColor clearColor];
cell.detailTextLabel.backgroundColor=[UIColor clearColor];
}
else if(indexPath.section == 1){cell.textLabel.font = [UIFont boldSystemFontOfSize:14];
cell.textLabel.textColor = [UIColor blackColor];
cell.detailTextLabel.numberOfLines=3;
cell.detailTextLabel.textColor=[UIColor colorWithRed:0.40 green:0.40 blue:0.40 alpha:1.0];
cell.textLabel.text=[NSString stringWithFormat:#"Principal"];
NSString *thumbs = [NSString stringWithFormat:#"%#", [imagearray objectAtIndex:indexPath.row]];
UIImage *thumbs1 = [UIImage imageWithData:[NSData dataWithContentsOfURL:
[NSURL URLWithString:thumbs]]];
// cell.imageView.image = [UIImage imageNamed:th];
cell.detailTextLabel.text=[NSString stringWithFormat:#" %# ",[imagearray objectAtIndex:indexPath.row]] ;
cell.textLabel.backgroundColor=[UIColor clearColor];
cell.detailTextLabel.backgroundColor=[UIColor clearColor];}
Can any one let me know what went wrong. i even set header and footer for the sections.
set the height of cell For example...
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *titleString = yourTitleString;///here just write your string here
NSString *detailString = [NSString stringWithFormat:#"Take in time: %# \nDissmiss time:%# \nPhone No: %# ",[array objectAtIndex:indexPath.row],[array1 objectAtIndex:indexPath.row],[array2 objectAtIndex:indexPath.row]];
CGSize titleSize = [titleString sizeWithFont:[UIFont boldSystemFontOfSize:18] constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
CGSize detailSize = [detailString sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
return detailSize.height+titleSize.height;
}
And i just set the code for the common cell for Dynamic height of cell see bellow code of cellForRowAtIndexPath method...
- (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] autorelease];
}
cell.textLabel.text = #"YourTitle";
cell.textLabel.font = [UIFont boldSystemFontOfSize:18];
cell.textLabel.numberOfLines = ceilf([#"YourTitle" sizeWithFont:[UIFont boldSystemFontOfSize:18] constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap].height/20.0);
cell.detailTextLabel.text = [NSString stringWithFormat:#"Take in time: %# \nDissmiss time:%# \nPhone No: %# ",[array objectAtIndex:indexPath.row],[array1 objectAtIndex:indexPath.row],[array2 objectAtIndex:indexPath.row]];
cell.detailTextLabel.font = [UIFont systemFontOfSize:14];
cell.detailTextLabel.numberOfLines = ceilf([[NSString stringWithFormat:#"Take in time: %# \nDissmiss time:%# \nPhone No: %# ",[array objectAtIndex:indexPath.row],[array1 objectAtIndex:indexPath.row],[array2 objectAtIndex:indexPath.row]]; sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap].height/20.0);
return cell;
}
For such a huge data you need to truncate the data to display it.
For this you can use:
[[cell detailTextLabel] setLineBreakMode:UILineBreakModeTailTruncation];
cell.detailTextLabel.numberOfLines = 0;
Another option is you need to adjust your tableViewCell's height using the heightForRowAtIndexPath delegate.

tableview refreshing while searching on iphone

I have a table view that shows 3 data side by side in table ,lets say A(name) , B(roll no) , C(id).
Now if user do search based on A's (search by name) (table get refreshed with name while searching only) value I want to either hide the content of B & C or they should show the correct value while search is on , not the old value which they got before searching.
I couldn't find the solution how to deal with remaining B & C.kindly suggest
here is my code
- (void) searchTableView
{
NSLog(#"search button is down %#",searchBar.text);
NSString *searchText = searchBar.text;
NSMutableArray *searchArray = [[NSMutableArray alloc] init];
if(SearchQuery==1)
{
[searchArray addObjectsFromArray:[entityArray valueForKey:#"Name"]];
}
if(SearchQuery==2)
{
[searchArray addObjectsFromArray:[entityArray valueForKey:#"IC"]];
}
for (NSString *sTemp in searchArray)
{
NSLog(#"copyListOfItems in search -%#",copyListOfItems);//final array for names
NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (titleResultsRange.length > 0)
[copyListOfItems addObject:sTemp];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSManagedObject *object = (NSManagedObject *)[entityArray objectAtIndex:indexPath.row];
NSString *CellIdentifier = #"Cell";
int indicator = UITableViewCellAccessoryNone;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
mainLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 50.0)] autorelease];//220
mainLabel.tag = MAINLABEL_TAG;
[mainLabel setFont:[UIFont fontWithName:#"Helvetica-Bold" size:18]];//Palatino-BoldItalic
mainLabel.textAlignment = UITextAlignmentLeft;
mainLabel.textColor = [UIColor blackColor];
secondLabel = [[[UILabel alloc] initWithFrame:CGRectMake(313.0, 9.0, 200.0, 25.0)] autorelease];//83
secondLabel.tag = SECONDLABEL_TAG;
smallSystemFontSize]]];
[secondLabel setFont:[UIFont fontWithName:#"Helvetica" size:18]];
secondLabel.textAlignment = UITextAlignmentLeft;
secondLabel.textColor = [UIColor blackColor];
[cell.contentView addSubview:secondLabel];
//// 3rd labl display
thirdLabel = [[[UILabel alloc] initWithFrame:CGRectMake(560.0, 9.0, 250.0, 25.0)] autorelease];//83
thirdLabel.tag = THIRDLABEL_TAG;
//thirdLabel.font = [UIFont systemFontOfSize:13.0];
[thirdLabel setFont:[UIFont fontWithName:#"Helvetica" size:18]];//Palatino-BoldItalic
thirdLabel.textAlignment = UITextAlignmentLeft;
thirdLabel.textColor = [UIColor blackColor];
[cell.contentView addSubview:thirdLabel];
}
if(searching)
{
NSLog(#" seacrh List in table view =%#",[copyListOfItems objectAtIndex:indexPath.row]);
// NSString *s=[object valueForKey:#"Name"];
NSLog(#"copyListOfItems length ---------%d",[copyListOfItems count]);
cell.textLabel.text =[copyListOfItems objectAtIndex:indexPath.row] ;
if(SearchQuery==2)
{
secondLabel.text=#"kk";// not working
secondLabel.alpha=1; // this is not working i cant upadate
}
}
else
{
if(self.entityName == #"Parent")
{
{
indicator = UITableViewCellAccessoryDisclosureIndicator;
CellIdentifier = #"CellWithDisclosure";
}
}
if(SearchQuery==2)
{
cell.textLabel.text =[object valueForKey:#"IC"] ;
secondLabel = (UILabel *)[cell.contentView viewWithTag:SECONDLABEL_TAG];
thirdLabel = (UILabel *)[cell.contentView viewWithTag:THIRDLABEL_TAG];
NSLog(#"sex is %# ",[object valueForKey:#"Name"]);
secondLabel.text=[object valueForKey:#"Name"] ;
secondLabel.alpha=0;
thirdLabel.text=[object valueForKey:#"roll"] ;
}

How to fit three labels in a single cell using custom UITableViewCell?

I am customizing my cells and trying to add three labels in a single cell, one on the top and other two in bottom in single line. I am facing some problem wrapping the labels and display the neatly.
Here's the code:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGSize titleSize = [[[self.titleArray objectAtIndex:indexPath.row] valueForKey:#"display_title"] sizeWithFont:[UIFont systemFontOfSize:[UIFont smallSystemFontSize]]
constrainedToSize:CGSizeMake(200, 9999999)
lineBreakMode:UILineBreakModeWordWrap];
CGSize detailSize = [#"APR 21, 2011" sizeWithFont:[UIFont systemFontOfSize:[UIFont smallSystemFontSize]]
constrainedToSize:CGSizeMake(200, 999999)
lineBreakMode:UILineBreakModeWordWrap];
CGSize thirdDetailSize = [#"This is the project type." sizeWithFont:[UIFont systemFontOfSize:[UIFont smallSystemFontSize]]
constrainedToSize:CGSizeMake(200, 999999)
lineBreakMode:UILineBreakModeWordWrap];
return titleSize.height + MAX (detailSize.height, thirdDetailSize.height) + 15.0;
}
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UILabel * mainLabel;
UILabel * secondLabel;
UILabel * thirdLabel;
UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
NSArray *subviews = cell.contentView.subviews;
for (UIView *vw in subviews)
{
if ([vw isKindOfClass:[UILabel class]])
{
[vw removeFromSuperview];
}
}
mainLabel = [[UILabel alloc] initWithFrame:CGRectMake(5,0,300,22)] ;
mainLabel.font = [UIFont systemFontOfSize:[UIFont smallSystemFontSize]];
mainLabel.numberOfLines = 20;
mainLabel.lineBreakMode = UILineBreakModeWordWrap;
mainLabel.backgroundColor = [UIColor clearColor];
mainLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight;
mainLabel.adjustsFontSizeToFitWidth = YES;
mainLabel.tag = kCellMainLabelTag;
[cell.contentView addSubview:mainLabel];
[mainLabel release];
secondLabel = [[UILabel alloc] initWithFrame:CGRectMake(5,30,140,22)];
secondLabel.font = [UIFont systemFontOfSize:[UIFont smallSystemFontSize]];
secondLabel.numberOfLines = 20;
secondLabel.textColor = [UIColor grayColor];
secondLabel.lineBreakMode = UILineBreakModeWordWrap;
secondLabel.backgroundColor = [UIColor clearColor];
secondLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
secondLabel.adjustsFontSizeToFitWidth = YES;
secondLabel.tag = kCellSecondLabelTag;
[cell.contentView addSubview:secondLabel];
[secondLabel release];
thirdLabel = [[UILabel alloc] initWithFrame:CGRectMake(145,30,140,22)];
thirdLabel.font = [UIFont systemFontOfSize:[UIFont smallSystemFontSize]];
thirdLabel.numberOfLines = 20;
thirdLabel.textColor = [UIColor grayColor];
thirdLabel.lineBreakMode = UILineBreakModeWordWrap;
thirdLabel.backgroundColor = [UIColor clearColor];
thirdLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
thirdLabel.adjustsFontSizeToFitWidth = YES;
thirdLabel.tag = kCellSecondLabelTag;
[cell.contentView addSubview:thirdLabel];
[thirdLabel release];
}
else
{
mainLabel = (UILabel *)[cell.contentView viewWithTag:kCellMainLabelTag];
secondLabel = (UILabel *)[cell.contentView viewWithTag:kCellSecondLabelTag];
thirdLabel = (UILabel *)[cell.contentView viewWithTag:kCellThirdLabelTag];
}
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
NSString * date = [[self.titleArray objectAtIndex:indexPath.row]valueForKey:#"docdt"];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
NSDate * intime = [dateFormat dateFromString:date];
NSString * userVisibleDateTimeString;
if (intime != nil) {
// Convert the NSDate to a user-visible date string.
NSDateFormatter * userVisibleDateFormatter = [[[NSDateFormatter alloc] init] autorelease];
assert(userVisibleDateFormatter != nil);
[userVisibleDateFormatter setDateStyle:NSDateFormatterShortStyle];
userVisibleDateTimeString = [userVisibleDateFormatter stringFromDate:intime];
}
NSString * signDate = userVisibleDateTimeString;
NSArray * parts = [signDate componentsSeparatedByString:#"/"];
NSString * exactFormat = [self displayDate:(NSArray *)parts];
NSString * type = [[self.titleArray objectAtIndex:indexPath.row]valueForKey:#"docty"];
CGSize titleSize = [[[self.titleArray objectAtIndex:indexPath.row] valueForKey:#"display_title"] sizeWithFont:[UIFont systemFontOfSize:[UIFont smallSystemFontSize]]
constrainedToSize:CGSizeMake(280, 9999999)
lineBreakMode:UILineBreakModeWordWrap];
CGSize detailSize = [exactFormat sizeWithFont:[UIFont systemFontOfSize:[UIFont smallSystemFontSize]]
constrainedToSize:CGSizeMake(200, 999999)
lineBreakMode:UILineBreakModeWordWrap];
CGSize thirdDetailSize = [type sizeWithFont:[UIFont systemFontOfSize:[UIFont smallSystemFontSize]]
constrainedToSize:CGSizeMake(200, 999999)
lineBreakMode:UILineBreakModeWordWrap];
mainLabel.text = [[self.titleArray objectAtIndex:indexPath.row]valueForKey:#"display_title"];
mainLabel.frame = CGRectMake(mainLabel.frame.origin.x,
mainLabel.frame.origin.y, titleSize.width, titleSize.height);
secondLabel.text = exactFormat;
secondLabel.frame = CGRectMake(secondLabel.frame.origin.x,
mainLabel.frame.origin.y + titleSize.height, detailSize.width, detailSize.height);
thirdLabel.text = type;
thirdLabel.frame = CGRectMake(thirdLabel.frame.origin.x,
mainLabel.frame.origin.y + titleSize.height, thirdDetailSize.width, thirdDetailSize.height);
return cell;
}
please help !!!
Thanks,
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 3;}
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 100;}
enter code- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Create
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
//cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
}
// Configure
switch (indexPath.row) {
case 0:
cell.textLabel.textColor=[UIColor whiteColor];
CGRect myImageRec =CGRectMake(20,20, 50,21 );
UILabel *lbl1=[[UILabel alloc]initWithFrame:myImageRec];
lbl1.text=#"label1";
[ cell addSubview: lbl1];
CGRect frame1=CGRectMake(20,49, 50,21 );
UILabel *lbl2=[[UILabel alloc]initWithFrame:frame1];
lbl2.text=#"label2";
[ cell addSubview: lbl2];
CGRect frame2=CGRectMake(20,73, 50,21 );
UILabel *lbl3=[[UILabel alloc]initWithFrame:frame2];
lbl3.text=#"label3";
[ cell addSubview: lbl3];
break;
default: break;
}
return cell;
}
I have use this and i display three label in one cell i show also image of output.

iPhone Problem with custom cell

I try this code but it's a mistake because i have a repetition of title and description...
Someone can help me please?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
CustomCell *cell=(CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil){
cell=[[[CustomCell alloc]initWithFrame:CGRectZero reuseIdentifier:CellIdentifier]autorelease];
cell.accessoryType = UITableViewCellAccessoryNone;
}
else{
AsyncImageView* oldImage = (AsyncImageView*)
[cell.contentView viewWithTag:999];
[oldImage removeFromSuperview];
}
NSString *mediaUrl = [[[[self rssParser]rssItems]objectAtIndex:indexPath.row]mediaUrl];
NSString *noimage=#"http://www.notizie-informatiche.com/wp-content/themes/arthemia/images/imagelogo.png";
if([mediaUrl isEqualToString:noimage] == FALSE){
//DISEGNO IL FRAME PER L'IMMAGINE
CGRect frameimmagine;
frameimmagine.size.width=100; frameimmagine.size.height=120;
frameimmagine.origin.x=0; frameimmagine.origin.y=0;
AsyncImageView* asyncImage = [[[AsyncImageView alloc] initWithFrame:frameimmagine] autorelease];
UIImage *myImage = [UIImage imageNamed:#"uknown.jpg"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:myImage];
[cell setAccessoryView:imageView];
//SCARICO L'IMMAGINE
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
asyncImage.tag = 999;
NSURL *url = [NSURL URLWithString: mediaUrl];
[asyncImage loadImageFromURL:url];
[cell.contentView addSubview:asyncImage];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
//OTTENGO IL TITOLO
//cell.lbltitolo = [[[[self rssParser]rssItems]objectAtIndex:indexPath.row]title];
NSString *titolo = [[[[self rssParser]rssItems]objectAtIndex:indexPath.row]title];
//OTTENFO LA DESCRIZIONE
//cell.lblsottotitolo.text = [[[[self rssParser]rssItems]objectAtIndex:indexPath.row]description];
NSString *descrizione = [[[[self rssParser]rssItems]objectAtIndex:indexPath.row]description];
UILabel *cellatitoloconimmagine=[[UILabel alloc]init];
[cellatitoloconimmagine removeFromSuperview];
UILabel *cellatitolo=[[UILabel alloc]init];
[cellatitolo removeFromSuperview];
UILabel *celladescrizione=[[UILabel alloc]init];
[celladescrizione removeFromSuperview];
UILabel *celladescrizioneconimmagine=[[UILabel alloc]init];
[celladescrizioneconimmagine removeFromSuperview];
/*
[celladescrizioneconimmagine removeFromSuperview];
[celladescrizione removeFromSuperview];
*/
if([mediaUrl isEqualToString:noimage] == FALSE){
CGRect frametitoloconimmagine = CGRectMake(105.0f, 5.0f, 210, 40);
cellatitoloconimmagine.frame=frametitoloconimmagine;
cellatitoloconimmagine.text=titolo;
cellatitoloconimmagine.textAlignment = UITextAlignmentLeft;
cellatitoloconimmagine.font = [UIFont boldSystemFontOfSize:16];
cellatitoloconimmagine.numberOfLines = 2;
[cell.contentView addSubview:cellatitoloconimmagine];
CGRect framedescrizioneconimmagine = CGRectMake(105.0f, 55.0f, 210, 50);
celladescrizioneconimmagine.frame=framedescrizioneconimmagine;
celladescrizioneconimmagine.text=descrizione;
celladescrizioneconimmagine.textAlignment = UITextAlignmentLeft;
celladescrizioneconimmagine.font = [UIFont systemFontOfSize:14];
celladescrizioneconimmagine.numberOfLines = 3;
[cell.contentView addSubview:celladescrizioneconimmagine];
}
else {
CGRect frametitolo = CGRectMake(5.0f, 5.0f, 310, 40);
cellatitolo.frame=frametitolo;
cellatitolo.text=titolo;
cellatitolo.textAlignment = UITextAlignmentLeft;
cellatitolo.font = [UIFont boldSystemFontOfSize:16];
cellatitolo.numberOfLines = 2;
[cell.contentView addSubview:cellatitolo];
CGRect framedescrizione = CGRectMake(5.0f, 55.0f, 310, 50);
celladescrizione.frame=framedescrizione;
celladescrizione.textAlignment = UITextAlignmentLeft;
celladescrizione.font = [UIFont systemFontOfSize:14];
celladescrizione.numberOfLines = 3;
celladescrizione.text=descrizione;
[cell.contentView addSubview:celladescrizione];
}
return cell;
}
the cell identifier must be unique for unique cells. It seems you have repeated cells because they are all dequed with name #"cell". Also remove the "static" word.