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

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.

Related

How to get rid of overwriting of contents inside the cells of a UITableView in iPhone

//to display the contents in a table
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
}
UIImage *ibnLogo = [[UIImage imageNamed:#"IBN.jpeg"]autorelease];
News *news= [[xmlParser newsArray] objectAtIndex:indexPath.row];
CGRect imageFrame = CGRectMake(2, 8, 40, 40);
self.customImage = [[[UIImageView alloc] initWithFrame:imageFrame] autorelease];
self.customImage.image = ibnLogo;
[cell.contentView addSubview:self.customImage];
CGRect contentFrame = CGRectMake(45, 2, 265, 30);
UILabel *contentLabel = [[[UILabel alloc] initWithFrame:contentFrame] autorelease];
contentLabel.numberOfLines = 2;
contentLabel.font = [UIFont italicSystemFontOfSize:12];
contentLabel.text = [news content];
[cell.contentView addSubview:contentLabel];
CGRect dateFrame = CGRectMake(45, 40, 265, 10);
UILabel *dateLabel = [[[UILabel alloc] initWithFrame:dateFrame] autorelease];
dateLabel.font = [UIFont systemFontOfSize:10];
dateLabel.text = [news dateCreated];
[cell.contentView addSubview:dateLabel];
return cell;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
UIImage *ibnLogo = [[UIImage imageNamed:#"IBN.jpeg"]autorelease];
News *news= [[xmlParser newsArray] objectAtIndex:indexPath.row];
CGRect imageFrame = CGRectMake(2, 8, 40, 40);
self.customImage = [[[UIImageView alloc] initWithFrame:imageFrame] autorelease];
self.customImage.image = ibnLogo;
[cell.contentView addSubview:self.customImage];
CGRect contentFrame = CGRectMake(45, 2, 265, 30);
UILabel *contentLabel = [[[UILabel alloc] initWithFrame:contentFrame] autorelease];
contentLabel.numberOfLines = 2;
contentLabel.font = [UIFont italicSystemFontOfSize:12];
contentLabel.text = [news content];
[cell.contentView addSubview:contentLabel];
CGRect dateFrame = CGRectMake(45, 40, 265, 10);
UILabel *dateLabel = [[[UILabel alloc] initWithFrame:dateFrame] autorelease];
dateLabel.font = [UIFont systemFontOfSize:10];
dateLabel.text = [news dateCreated];
[cell.contentView addSubview:dateLabel];
}
return cell;
}
Add all Element (Component) in between if (cell == nil) Condition.

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

Trouble figuring out the UITableViewCell multiline ability

Got pretty far with this one but am hanging on the part where I read out the string information.
I made a cell that receives data from an external xml file, it all works fine but some cell contain to much text which I want to display over multiple lines. Also no problem. But the tricky part is the dynamic height of my cell. I configured this in the heightForRowAtIndexPath: method, but I need to know the amount of text(rows) the cell contains, and I am stuck on the part how to connect that to my cellText(string) variable.
Any help would be welcome :-)
Here is my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
}
// Set the text in the cell for the section/row.
NSString *endDate = [XMLParser stringFromDate:[XMLParser dateFromString:stage.end]];
int endDateLength = endDate.length;
NSString *endTime = [NSString stringWithFormat:#"%#", [endDate substringFromIndex:endDateLength -7]];
NSString *startDate = [XMLParser stringFromDate:[XMLParser dateFromString:stage.start]];
int startDateLength = startDate.length;
NSString *startTime = [NSString stringWithFormat:#"%#", [startDate substringFromIndex:startDateLength -7]];
NSString *date = [XMLParser stringFromDate:[XMLParser dateFromString:stage.start]];
int dateLength = date.length;
NSString *dateString = [NSString stringWithFormat:#"%#", [date substringToIndex:dateLength -7]];
NSString *cellText = nil;
NSString *cellExplainText = nil;
//Pre title arrays
dateTitleArray = [[NSArray alloc] initWithObjects:#"Dag", #"Start tijd", #"Eind tijd", nil];
nameTitleArray = [[NSArray alloc] initWithObjects:#"Naam", #"Graad",nil];
addressTitleArray = [[NSArray alloc] initWithObjects:#"Dojo", #"Straat", #"Plaats",nil];
infoTitleArray = [[NSArray alloc] initWithObjects:#"Kosten", #"Contact", #"Details", nil];
dateArray = [[NSArray alloc] initWithObjects: dateString, startTime, endTime, nil];
nameArray = [[NSArray alloc] initWithObjects: stage.teacher, stage.grade, nil];
addressArray = [[NSArray alloc] initWithObjects: stage.dojo, stage.street, stage.city, nil];
infoArray = [[NSArray alloc] initWithObjects: stage.cost, stage.contact, stage.details, nil];
switch (indexPath.section)
{
case 0:
cellExplainText = [dateTitleArray objectAtIndex:indexPath.row];
cellText = [dateArray objectAtIndex:indexPath.row];
break;
case 1:
cellExplainText = [nameTitleArray objectAtIndex:indexPath.row];
cellText = [nameArray objectAtIndex:indexPath.row];
break;
case 2:
cellExplainText = [addressTitleArray objectAtIndex:indexPath.row];
cellText = [addressArray objectAtIndex:indexPath.row];
break;
case 3:
cellExplainText = [infoTitleArray objectAtIndex:indexPath.row];
cellText = [infoArray objectAtIndex:indexPath.row];
break;
default:
break;
}
[dateTitleArray release];
[nameTitleArray release];
[addressTitleArray release];
[infoTitleArray release];
[dateArray release];
[nameArray release];
[addressArray release];
[infoArray release];
cell.textLabel.text = cellExplainText;
cell.detailTextLabel.text = cellText;
cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.detailTextLabel.numberOfLines = 0;
cell.detailTextLabel.font = [UIFont fontWithName:#"Helvetica" size:14.0];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellTextSize = ????????????;
UIFont *cellFont = [UIFont fontWithName:#"Helvetica" size:14.0];
CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
CGSize labelSize = [cellTextSize sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
return labelSize.height + 12;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
CGSize maxSize = CGSizeMake(urMaxSize);
CGSize cellSize = [itemName sizeWithFont:[UIFont systemFontOfSize:15]
constrainedToSize:maxSize lineBreakMode:UILineBreakModeWordWrap];
return cellSize.height;
}
itemName is the text u want to fill it with. I guess it is [infoTitleArray objectAtIndex:indexPath.row] and corresponding array information based on index. You can get the section also in this method so you can get the string.
If you want to use your method
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellText;
switch (indexPath.section)
{
case 0:
cellText = [dateTitleArray objectAtIndex:indexPath.row];
break;
case 1:
cellText = [nameTitleArray objectAtIndex:indexPath.row];
break;
case 2:
cellText = [addressTitleArray objectAtIndex:indexPath.row];
break;
case 3:
cellText = [infoTitleArray objectAtIndex:indexPath.row];
break;
default:
break;
}
UIFont *cellFont = [UIFont fontWithName:#"Helvetica" size:14.0];
CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
return labelSize.height + 12;
}
I would suggest you to store in an array your cellText/cellTextExplained values, so that in heightForRowAtIndexPath: you can retrieve them:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
NSString *cellText = [self.cellTextArray:objectAtIndex:indexPath.row];
//-- rest of your code here
UIFont *cellFont = [UIFont fontWithName:#"Helvetica" size:14.0];
CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
CGSize labelSize = [cellTextSize sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
return labelSize.height + 12;
}

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.