im having troubles making my UILabel height dynamic. The label is contained in a custom TableViewCell
I have managed to make the TableViewCellHeight dynamic, but i cant make the label multiple lines.
Whenever i set number of lines = 0 (in IB as well as code), the label/text disappears completely. And lineBreakMode doesnt do much either?
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
Book *eventUp = [appDelegate.eventDataUp objectAtIndex:indexPath.row];
NSString *text = eventUp.date;
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
CGFloat height = MAX(size.height, 44.0f);
return height + (CELL_CONTENT_MARGIN * 2);
//return 66;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [appDelegate.eventDataUp count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"EventUpCVCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
self.eventUpCVCell = nil;
[[NSBundle mainBundle] loadNibNamed:#"EventUpCVCell" owner:self options:nil];
cell = self.eventUpCVCell;
label = [[UILabel alloc] initWithFrame:CGRectZero];
[label setLineBreakMode:UILineBreakModeWordWrap];
[label setMinimumFontSize:FONT_SIZE];
[label setNumberOfLines:0];
[label setFont:[UIFont systemFontOfSize:FONT_SIZE]];
[label setTag:1];
[[cell contentView] addSubview:label];
}
Book *eventUp = [appDelegate.eventDataUp objectAtIndex:indexPath.row];
UILabel *lblMonth = (UILabel *) [cell viewWithTag:kLabel1Tag];
UILabel *lblTitle = (UILabel *) [cell viewWithTag:kLabel2Tag];
UILabel *lblDate = (UILabel *) [cell viewWithTag:kLabel3Tag];
//month.text = eventUp.month;
lblTitle.text = eventUp.title;
lblDate.text = eventUp.date;
//lblDate.lineBreakMode = UILineBreakModeWordWrap;
lblDate.numberOfLines = 1;
lblMonth.text = [NSString stringWithFormat:#"%# ", eventUp.month];
//cell.textLabel.text = eventUp.title;
//cell.detailTextLabel.text = eventUp.date;
//NSLog(#"Hello");
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// Set up the cell
return cell;
}
You have to set the label frame after creating it, actually you create it with CGRectZero Frame
try:
label = [[UILabel alloc] initWithFrame:CGRectZero];
[label setLineBreakMode:UILineBreakModeWordWrap];
[label setMinimumFontSize:FONT_SIZE];
[label setNumberOfLines:0];
[label setFont:[UIFont systemFontOfSize:FONT_SIZE]];
[label setTag:1];
Book *eventUp = [appDelegate.eventDataUp objectAtIndex:indexPath.row];
NSString *text = eventUp.date;
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
[label setFrame:CGRectMake(0,0,size.width,size.height)];
Related
I need to create a cell height according to lable's content with orientation support curretly have tried this and working fine for portrait but not in landscape so need solution which works on both so if any one can please help me.
thanks in advance for your efforts.
this is my code in tableview
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSArray *arr = [arrData objectAtIndex:tableView.tag];
NSString *text = [arr objectAtIndex:indexPath.row];
CGSize constraint = CGSizeMake(contentWidth - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:20] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
CGFloat height = MAX(size.height, 44.0f);
return height + (CELL_CONTENT_MARGIN * 2);
}
// Customize the appearance of table view cells.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
UILabel *label = nil;
cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
cell.accessoryView = nil;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
label = [[UILabel alloc] initWithFrame:CGRectZero];
[label setLineBreakMode:NSLineBreakByWordWrapping];
[label setNumberOfLines:0];
[label setFont:[UIFont systemFontOfSize:20]];
[label setTag:1];
[label setBackgroundColor:[UIColor redColor]];
[[cell contentView] addSubview:label];
}
NSArray *arr = [arrData objectAtIndex:tableView.tag];
NSString *text = [arr objectAtIndex:indexPath.row];
CGSize constraint = CGSizeMake(contentWidth - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:20] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
if (!label)
label = (UILabel*)[cell viewWithTag:1];
[label setText:text];
[label setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, contentWidth - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))];
return cell;
}
In heightForRowAtIndexPath you can set height according to orientation.
when orientation change, reload cells. In heightForRowAtIndexPath use this value to check orientation and while this method is called -(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration reload your cells.
So your heightForRowAtIndexPath will be somethign like this
if(UIInterfaceOrientationIsPortrait(orientation)){
}
else
{
}
Make sure you keep your cells subviews as autoresizing flexible.
I'm very much new to iphone development and strucked at a point. Actually my need is I'm displaying some results in a UITableView and I'm displaying then UILables of UITableView's cell..
How can I make the UILabel's to adjust the content according to the text. Actually text is not static .It may be changing at run time.So I need to set dynamic size to a UILabel .Ans secondly suppose if the text is null then it should not show any space and immediately the next UILabel should start .How can I make it possible ?
Here is my code..
self.tableView=[[UITableView alloc] initWithFrame:CGRectMake(0,150,320,800) style:UITableViewStylePlain];
self.tableView.delegate=self;
self.tableView.dataSource=self;
self.tableView.scrollEnabled = NO;
[self.view addSubview:self.tableView];
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(currentHtmlElement==#"3")
{
static NSString *CellIdentifier=#"Cell";
UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
}
NSMutableDictionary *d = (NSMutableDictionary *) [arr2 objectAtIndex:indexPath.row];
cell.accessoryType= UITableViewCellAccessoryDetailDisclosureButton;
UILabel *name1= [[UILabel alloc]initWithFrame:CGRectMake(10, 5, 320, 40)];
name1.font=[UIFont boldSystemFontOfSize:16];
[name1 setTextAlignment:UITextAlignmentLeft];
[name1 setText:[d valueForKey:#"Name"]];
name1.tag = 111;
[cell addSubview:name1];
[name1 release];
UILabel *codetype=[[UILabel alloc]initWithFrame:CGRectMake(10, 45, 320,15)];
codetype .font=[UIFont boldSystemFontOfSize:12];
[codetype setTextAlignment:UITextAlignmentLeft];
[codetype setText:[d valueForKey:#"Code"]];
codetype.tag=112;
[cell addSubview:codetype];
[codetype release];
UILabel *id=[[UILabel alloc]initWithFrame:CGRectMake(10, 68, 320,10)];
id .font=[UIFont fontWithName:#"arial" size:12];
[id setTextAlignment:UITextAlignmentLeft];
[id setText:[d valueForKey:#"ID"]];
id.tag=113;
[cell id ];
[id release];
UILabel *address=[[UILabel alloc]initWithFrame:CGRectMake(10, 85, 320,10)];
address .font=[UIFont fontWithName:#"arial" size:12];
[address setTextAlignment:UITextAlignmentLeft];
[address setText:[d valueForKey:#"address"]];
line2.tag=114;
[cell addSubview: address];
[address release];
UILabel *city = [[UILabel alloc]initWithFrame:CGRectMake(10, 105, 320, 10)];
city .font=[UIFont fontWithName:#"arial" size:12];
[city setTextAlignment:UITextAlignmentLeft];
[city setText:[d valueForKey:#"City"]];
city.tag=115;
[cell addSubview:city ];
[city release];
}
Here I have set all the UILabels to static size.But what I need is dynamic size.and suppose if the address label is null then it should not show any space and next label i.e city should start immediately next after id ..How is it possible?
I did this task in my code.... you can use this code and do changes according to your code....and let me know if you get any problem.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
NSMutableDictionary *dicNotification =[[NSMutableDictionary alloc]initWithDictionary: [myNotification objectAtIndex:indexPath.row]];
//code to get notification text height
NSString *text = [dicNotification objectForKey:#"message"];
CGSize constraint = CGSizeMake(175, 100.0f);
CGSize size = [text sizeWithFont: [UIFont fontWithName:#"Verdana" size:12] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
return size.height;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
//label for message
UILabel *lblMsg = [[UILabel alloc] initWithFrame:CGRectZero];
lblMsg.tag = 12;
lblMsg.backgroundColor = [UIColor clearColor];
[lblMsg setFont:[UIFont fontWithName:#"Verdana" size:12]];
[lblMsg setLineBreakMode:UILineBreakModeWordWrap];
[cell addSubview:lblMsg];
}
NSMutableDictionary *dicNotification =[[NSMutableDictionary alloc]initWithDictionary: [myNotification objectAtIndex:indexPath.row]];
//get the message height set frame height to imageView(BubbleMid & Top) & labelMSG
NSString *txt = [dicNotification objectForKey:#"message"];
CGSize constraint = CGSizeMake(175, 2000.0f);
CGSize size = [txt sizeWithFont: [UIFont fontWithName:#"Verdana" size:12] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
//set frame height to labelMsg
UILabel *lblMsg = (UILabel *)[cell viewWithTag:12];
lblMsg.frame = CGRectMake(15,25, 175, size.height);
[lblMsg setNumberOfLines:size.height/16];
//set labelMsg text
lblMsg.text = [dicNotification objectForKey:#"message"];
return cell;
}
The thing is that you must use heightForRowAtIndexPath. The problem is that it is called prior to cellForRowAtIndexPath, therefore the text is not in the component and you can not simply adjust the component size to the text. So, what you do is calculate the text size using a sizeWithFont.
In case of normal/standard text
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//here you get your text string
NSString* theText = ... ;
//then calculate the size. systemFontOfSize depends on your font size
//yourLabelWidth must be known and usually depends on scree size or if the tableview has an index
CGSize textSize = [theText sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(yourLabelWidth, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
return textSize.height;
}
In the case of attributed text
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//here you get your text string
NSAttributedString* theText = ...;
//then calculate the size.
//yourLabelWidth must be known and usually depends on scree size or if the tableview has an index
CGRect rectSize = [theText boundingRectWithSize:CGSizeMake(yourLabelWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin context:NULL];
return rectSize.size.height;
return textSize.height;
}
you can make UILable adjust content with bellow code
CGSize sizeToMakeLabel = [name1.text sizeWithFont: name1.font];
name1.frame = CGRectMake(name1.frame.origin.x, name1.frame.origin.y,
sizeToMakeLabel.width, sizeToMakeLabel.height);
and here set second lable like bellow..
UPDATE
if([[d valueForKey:#"address"] isEquelToString:#""]){
UILabel *city=[[UILabel alloc]initWithFrame:CGRectMake(10, idLable.frame.origin.y + id.frame.size.height+10 , 320,15)];
city .font=[UIFont fontWithName:#"arial" size:12];
[city setTextAlignment:UITextAlignmentLeft];
[city setText:[d valueForKey:#"City"]];
city.tag=115;
[cell addSubview:city ];
CGSize sizeToMakeLabel = [city.text sizeWithFont: city.font];
city.frame = CGRectMake(city.frame.origin.x, city.frame.origin.y,
sizeToMakeLabel.width, sizeToMakeLabel.height);
}
else{
UILabel * address =[[UILabel alloc]initWithFrame:CGRectMake(10, idLable.frame.origin.y + id.frame.size.height+10 , 320,15)];
address .font=[UIFont fontWithName:#"arial" size:12];
[address setTextAlignment:UITextAlignmentLeft];
[address setText:[d valueForKey:#"address"]];
line2.tag=114;
[cell addSubview: address];
CGSize sizeToMakeLabel = [address.text sizeWithFont: address.font];
address.frame = CGRectMake(address.frame.origin.x, address.frame.origin.y,
sizeToMakeLabel.width, sizeToMakeLabel.height);
}
and so on for every label..
follow this logic for whole label
i hope this help you...
I have been populating a UITable view cell with the data fetched from a database. The length of the data varies for different cells. So i need to expand or Contract the height depend the data length. Presently i am using a custom cell with the following code.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellID= #"customcell4fan";
customcell4fan *cell = (customcell4fan *)[tableView dequeueReusableCellWithIdentifier:cellID];
if(cell==nil)
{
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:#"customcell4fan" owner:nil options:nil];
for(id currentObject in nibObjects)
{
if([currentObject isKindOfClass: [customcell4fan class]])
{
cell = (customcell4fan *)currentObject;
}
}
}
eleme = [xmlElementObjects objectAtIndex:indexPath.section];
NSString *email= [eleme.title stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#" \n\t"]];
NSString *postData= [eleme.description stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#" \n\t"]];
cell.nameLabel.text=email;
cell.postLabel.text=postData;
return cell;
}
try this
http://www.roostersoftstudios.com/2011/04/14/iphone-uitableview-with-animated-expanding-cells/
i find it very helpful
Calculate your cell's height and return in this method:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
Calculate the size of the string ,According to assign the height of the cell.
To calculate the height of label use the following code,
CGSize stringSize = [urResponseStr sizeWithFont:[UIFont boldSystemFontOfSize:15]
constrainedToSize:CGSizeMake(320, 9999)
lineBreakMode:UILineBreakModeWordWrap];//stringSize.height is your label height
after calculating height of your two labels assign respective value to the individual cells in the following delegate.
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
}
try with this answer
#define FONT_SIZE 14.0f
#define CELL_CONTENT_WIDTH 320.0f
#define CELL_CONTENT_MARGIN 10.0f
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
NSString *text = [items objectAtIndex:[indexPath row]];
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
CGFloat height = MAX(size.height, 44.0f);
return height + (CELL_CONTENT_MARGIN * 2);
}
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
UILabel *label = nil;
cell = [tv dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:#"Cell"] autorelease];
label = [[UILabel alloc] initWithFrame:CGRectZero];
[label setLineBreakMode:UILineBreakModeWordWrap];
[label setMinimumFontSize:FONT_SIZE];
[label setNumberOfLines:0];
[label setFont:[UIFont systemFontOfSize:FONT_SIZE]];
[label setTag:1];
[[label layer] setBorderWidth:2.0f];
[[cell contentView] addSubview:label];
}
NSString *text = [items objectAtIndex:[indexPath row]];
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
if (!label)
label = (UILabel*)[cell viewWithTag:1];
[label setText:text];
[label setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))];
return cell;
}
I am trying to get my cell to dynamicly change height depending on how much text is in the cell.. Currently I have the words wrapping.. but as soon as there is to much content for the cell (if it goes to a 3rd line) you cannot see anything past the 2nd line.
This is what I have so far.. hopefully someone can see if I am missing something or doing something wrong... any help would be appreciated.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UILabel *label = nil;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
label = [[UILabel alloc] initWithFrame:CGRectZero];
[label setLineBreakMode:UILineBreakModeWordWrap];
[label setMinimumFontSize:FONT_SIZE];
[label setNumberOfLines:0];
[label setFont:[UIFont systemFontOfSize:FONT_SIZE]];
[label setTag:1];
[[cell contentView] addSubview:label];
}
// //Display cells with data that has been sorted from startSortingTheArray
NSArray *keys = [self.letterDictionary objectForKey:[self.sectionLetterArray objectAtIndex:indexPath.section]];
NSString *key = [keys objectAtIndex:indexPath.row];
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [key sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
if (!label)
label = (UILabel*)[cell viewWithTag:1];
[label setText:key];
[label setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))];
//Applise current key value to cell text label
//cell.textLabel.text = key;
return cell;
}
//Cell size for word wrapping.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
//Display cells with data that has been sorted from startSortingTheArray
NSArray *keys = [self.letterDictionary objectForKey:[self.sectionLetterArray objectAtIndex:indexPath.section]];
NSString *key = [keys objectAtIndex:indexPath.row];
NSLog(#"%#", key);
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [key sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
CGFloat height = MAX(size.height, 44.0f);
return height + (CELL_CONTENT_MARGIN * 2);
}
Update the code above is now a working version.. for me anyway :)
Start over and follow this tutorial word for word and you'll be fine:
Cocoa is My Girlfriend
It actually looks like you looked at this tutorial and only followed part of it. I still don't see you setting the frame of the label though.
hi this is my problem:
i receive some datas from a php script to my tableview. But when i scroll down and back to the top my data is unreadable. The same if i scroll down. the data are superimposed
this is my code :
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [messageArray count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGSize textSize = { 260.0, 20000.0 };
NSMutableDictionary *dict = [messageArray objectAtIndex:indexPath.row];
NSString *aMsg = [dict objectForKey:#"message"];
CGSize size = [aMsg sizeWithFont:[UIFont systemFontOfSize:13.0] constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap];
size.height += 5;
CGFloat height = size.height<36?36:size.height;
return height;
}
// Customize the appearance of table view cells.
- (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] autorelease];
}
NSMutableDictionary *tempMsg = [messageArray objectAtIndex:indexPath.row];
CGFloat result = 20;
CGSize textSize = { 260.0, 20000.0 };
NSMutableDictionary *dict = [messageArray objectAtIndex:indexPath.row];
NSString *aMsg = [[dict objectForKey:#"message"]stringByReplacingOccurrencesOfString:#"\n" withString:#""];
CGSize size = [aMsg sizeWithFont:[UIFont systemFontOfSize:15.0] constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap];
result = MAX(size.height -5.0, 25.0);
// Configure the cell.
CGRect frame = CGRectMake(15, 5, size.width , size.height);
UILabel *label = [[UILabel alloc] initWithFrame:frame];
label.text = [tempMsg objectForKey:#"message"];
label.font = [UIFont systemFontOfSize:13];
label.numberOfLines = 0;
[label sizeToFit];
label.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:label];
[label release];
[cell setFrame:CGRectMake(20.0, 0.0, size.width, result)];
UIImage* balloon = [[UIImage imageNamed:#"grey.png"] stretchableImageWithLeftCapWidth:24 topCapHeight:15];
UIImageView *newImage = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, size.width+35, result+10)];
UIView *newView =[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, cell.frame.size.width, cell.frame.size.height)];
[newImage setImage:balloon];
[newView addSubview:newImage];
[cell setBackgroundView:newView];
return cell;
}
You are adding a new UILabel and a UIImageView each time the tableview-cell is requested. As a result after scrolling down and up again the Table-View-Cells contains multiple labels and images.
You should subclass the UITableViewCell with an additional UILabel and UIImageView.