Programatically Add Image to TableView Cell after Label - xcode

I am beginner in IOS. I have one tableview, Now I want to put one image in the cell exactly right side of label..
Bellow is my code..
- (UITableViewCell *)treeView:(RATreeView *)treeView cellForItem:(id)item treeNodeInfo:(RATreeNodeInfo *)treeNodeInfo
{
//NSInteger numberOfChildren = [treeNodeInfo.children count];
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"cell"];
cell.detailTextLabel.text = #"";//[NSString stringWithFormat:#"Number of children %d", numberOfChildren];
cell.textLabel.text = ((RADataObject *)item).name;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
//cell.textLabel.font = [UIFont fontWithName:#"CalvertMTStd-Bold" fonS];
CGSize stringBoundingBox;
CGRect frameLbl;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
cell.textLabel.font = [UIFont fontWithName:#"CalvertMTStd-Bold" size:17];
UIFont *customFoOnt = [UIFont fontWithName:#"CalvertMTStd-Bold" size:17]; // [UIFont ...]
stringBoundingBox = [cell.textLabel.text sizeWithFont:customFoOnt];
frameLbl = cell.textLabel.frame;
}else{
cell.textLabel.font = [UIFont fontWithName:#"CalvertMTStd-Bold" size:20];
UIFont *customFoOnt = [UIFont fontWithName:#"CalvertMTStd-Bold" size:17]; // [UIFont ...]
stringBoundingBox = [cell.textLabel.text sizeWithFont:customFoOnt];
frameLbl = cell.textLabel.frame;
}
UIImageView *downArrow =[[UIImageView alloc] initWithFrame:CGRectMake(stringBoundingBox.width+frameLbl.origin.x, 12, 20, 20)];
downArrow.image=[UIImage imageNamed:#"down_arrow_beg.png"];
[cell.self addSubview:downArrow];
//self.lbl1.font = [UIFont fontWithName:#"CalvertMTStd-Bold" size:fontSize];
if (treeNodeInfo.treeDepthLevel == 0)
{
cell.detailTextLabel.textColor = [UIColor blackColor];
}
cell.textLabel.textColor = [UIColor whiteColor];
return cell;
}
In above Code. I am not getting exact size of label and its position..
I need like
==============================
| [YOURLABEL] (IMAGE)
-------------------------------
| [YOUR BIGGER LABEL IS HERE] (IMAGE)

Related

Multiline statement is not showing in AlertView-iOS8

I want to show some multiple statement on UIAlertview with some left and center alignment but this piece of code is working for ios 7 even it's working for ios 8 but last label is not showing.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *equipmentString =#"100114 Mica Unit 5 and 6";
NSString *projectString =#"01-130156 FORD-FIVEHUNDREDSEL";
NSString *employeeString =#"00167293 Blake Linfield";
NSString *lastInspectedDate=#"28-Aug-2014";
NSString *lastInspectionDay=#"Thursday";
NSString *title=#"Proceed to inspection?";
UIView *alertContainerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 271, 150)];
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, CGRectGetWidth(alertContainerView.frame) - 20, 20)];
label1.textAlignment = NSTextAlignmentCenter;
label1.font = [UIFont boldSystemFontOfSize:14.0f];
label1.text = title;
[alertContainerView addSubview:label1];
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(label1.frame) + 2, CGRectGetWidth(alertContainerView.frame) - 20, 20)];
label2.textAlignment = NSTextAlignmentLeft;
label2.font = [UIFont systemFontOfSize:14.0f];
label2.text = equipmentString;
label2.lineBreakMode = NSLineBreakByTruncatingTail;
label2.numberOfLines = 1;
[alertContainerView addSubview:label2];
UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(label2.frame) + 2, CGRectGetWidth(alertContainerView.frame) - 20, 20)];
label3.textAlignment = NSTextAlignmentLeft;
label3.font = [UIFont systemFontOfSize:14.0f];
label3.text = projectString;
label3.lineBreakMode = NSLineBreakByTruncatingTail;
label3.numberOfLines = 1;
[alertContainerView addSubview:label3];
UILabel *label4 = [[UILabel alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(label3.frame) + 2, CGRectGetWidth(alertContainerView.frame) - 20, 20)];
label4.textAlignment = NSTextAlignmentLeft;
label4.font = [UIFont systemFontOfSize:14.0f];
label4.text = employeeString;
label4.lineBreakMode = NSLineBreakByTruncatingTail;
label4.numberOfLines = 1;
[alertContainerView addSubview:label4];
UILabel *label5=[[UILabel alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(label4.frame)+2, CGRectGetWidth(alertContainerView.frame)-20, 20)];
UIFont* boldFont = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
label5.textAlignment = NSTextAlignmentCenter;
label5.font = [UIFont systemFontOfSize:14.0f];
label5.text = lastInspectedDate;
[label5 setFont:boldFont];
label5.lineBreakMode = NSLineBreakByTruncatingTail;
label5.numberOfLines = 1;
[alertContainerView addSubview:label5];
UILabel *label6=[[UILabel alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(label5.frame)+2, CGRectGetWidth(alertContainerView.frame)-20, 20)];
UIFont* boldFont1 = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
label6.textAlignment = NSTextAlignmentCenter;
label6.font = [UIFont systemFontOfSize:14.0f];
label6.text = lastInspectionDay;
[label6 setFont:boldFont1];
label6.lineBreakMode = NSLineBreakByTruncatingTail;
label6.numberOfLines = 1;
[alertContainerView addSubview:label6];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:nil cancelButtonTitle:#"NO" otherButtonTitles:#"YES", nil];
[alertView setValue:alertContainerView forKey:#"accessoryView"];
[alertView show];
}
The last label is not showing in ios 8 but same code is working with ios 7 please some one help me where is the problem.
You can Try This Code
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"Alrt" message:#"This is the Message" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction
actionWithTitle:NSLocalizedString(#"Cancel", #"Cancel action")
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action)
{
NSLog(#"Cancel action");
}];
UIAlertAction *okAction = [UIAlertAction
actionWithTitle:NSLocalizedString(#"OK", #"OK action")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
NSLog(#"OK action");
}];
[alertController addAction:cancelAction];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];

TextField Custom for Validate

Hello everyone I'm trying to embed images for each textfield ...
When I go to insert the text into the first textfield, the image also has an impact on the second textfield. I would that images visualizzassero each textfield that the user selects and not directly to all ... Where am I doing wrong?
Thanks to all
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField {
NSString *nomecognome = self.FF_NomeCognomeTexfield.text ;
NSString *user = self.FF_UsernameTextField.text ;
if ([nomecognome length] == 0 || [user length] == 0 ){
FF_NomeCognomeTexfield.leftViewMode = UITextFieldViewModeAlways;
UIImageView* imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 32, 32)];
imageView.image = [UIImage imageNamed:#"FF_CheckOFF"];
imageView.contentMode = UIViewContentModeScaleAspectFit;
FF_NomeCognomeTexfield.leftView = imageView;
FF_UsernameTextField.leftViewMode = UITextFieldViewModeAlways;
UIImageView* imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 32, 32)];
imageView1.image = [UIImage imageNamed:#"FF_CheckOFF"];
imageView1.contentMode = UIViewContentModeScaleAspectFit;
FF_UsernameTextField.leftView = imageView1;
} else {
FF_NomeCognomeTexfield.leftViewMode = UITextFieldViewModeAlways;
UIImageView* imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 32, 32)];
imageView.image = [UIImage imageNamed:#"FF_CheckON"];
imageView.contentMode = UIViewContentModeScaleAspectFit;
FF_NomeCognomeTexfield.leftView = imageView;
FF_UsernameTextField.leftViewMode = UITextFieldViewModeAlways;
UIImageView* imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 32, 32)];
imageView1.image = [UIImage imageNamed:#"FF_CheckON"];
imageView1.contentMode = UIViewContentModeScaleAspectFit;
FF_UsernameTextField.leftView = imageView1;
}
return YES;
}
All textFields trigger this method. You should use the argument 'textField' in an if-statement to determine the identity of the sender.
if( [textField isEqual: FF_NomeCognomeTexfield] == YES){
...
}

Collapse UITable sections

I have a UITable that can collapse on header section click.
I need the header sections to have unique text in them.
I can use cell.textlabel.text = #""; and this works fine, however I want to add more labels to the cell and position these i.e
lbl123 = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 290, 20)];
lbl123.font = [UIFont fontWithName:#"Trebuchet MS" size:12.0f];
lbl123.textColor = [UIColor blackColor];
lbl123.backgroundColor = [UIColor clearColor];
lbl123.highlightedTextColor = [UIColor whiteColor];
lbl123.opaque = NO;
lbl123.adjustsFontSizeToFitWidth = TRUE;
lbl123.numberOfLines = 0;
lbl123.lineBreakMode = UILineBreakModeWordWrap;
[cell addSubview:lbl123];
lbl123.text = #"test";
but when I add this to the section on "cellforindexpath" It replicates itself on cells further down the table.
any ideas why this is happening??
full code below:-
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellValue = 0;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MyIdentifier"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:#"MyIdentifier"];
}
if (tableView.tag == 2)
{
UILabel *lbl123;
if ([self tableView:tableView canCollapseSection:indexPath.section])
{
cell.textLabel.font = [UIFont fontWithName:#"Trebuchet MS" size:12.0f];
if (!indexPath.row)
{
cell.textLabel.backgroundColor = [UIColor clearColor];
UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];
[bgView setBackgroundColor:[UIColor blackColor]];
[bgView setAlpha:0.3];
[cell setBackgroundView:bgView];
lbl123 = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 290, 20)];
lbl123.font = [UIFont fontWithName:#"Trebuchet MS" size:12.0f];
lbl123.textColor = [UIColor blackColor];
lbl123.backgroundColor = [UIColor clearColor];
lbl123.highlightedTextColor = [UIColor whiteColor];
lbl123.opaque = NO;
lbl123.adjustsFontSizeToFitWidth = TRUE;
lbl123.numberOfLines = 0;
lbl123.lineBreakMode = UILineBreakModeWordWrap;
[cell addSubview:lbl123];
lbl123.text = #"";
if(indexPath.section == 1)
{
cell.textLabel.font = [UIFont fontWithName:#"Trebuchet MS-Bold" size:15.0f];
cell.textLabel.text = [NSString stringWithFormat:#"My Results Overview:"];
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.numberOfLines = 3;
cell.textLabel.opaque = YES;
UIImageView *av = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 294, 80)];
av.backgroundColor = [UIColor clearColor];
av.opaque = NO;
av.image = [UIImage imageNamed:#"Blue bar 720x115.png"];
cell.backgroundView = av;
}
if(indexPath.section == 2)
{
cell.textLabel.font = [UIFont fontWithName:#"Trebuchet MS-Bold" size:15.0f];
cell.textLabel.text = [NSString stringWithFormat:#"Result1:"];
cell.textLabel.textColor = [UIColor blackColor];
cell.textLabel.numberOfLines = 3;
cell.textLabel.opaque = YES;
UIImageView *av = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 294, 120)];
av.backgroundColor = [UIColor clearColor];
av.opaque = NO;
if ([confidenceCOL isEqualToString:#"Red"])
{
av.image = [UIImage imageNamed:#"redPanel.png"];
}
if ([confidenceCOL isEqualToString:#"Green"])
{
av.image = [UIImage imageNamed:#"greenPanel.png"];
}
if ([confidenceCOL isEqualToString:#"Purple"])
{
av.image = [UIImage imageNamed:#"purplePanel.png"];
}
cell.backgroundView = av;
lbl123.text = #"Test";
}
if(indexPath.section == 3)
{
cell.textLabel.font = [UIFont fontWithName:#"Trebuchet MS-Bold" size:15.0f];
cell.textLabel.text = [NSString stringWithFormat:#"Result2:"];
cell.textLabel.textColor = [UIColor blackColor];
cell.textLabel.numberOfLines = 3;
cell.textLabel.opaque = YES;
UIImageView *av = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 294, 120)];
av.backgroundColor = [UIColor clearColor];
av.opaque = NO;
if ([ambitionCOL isEqualToString:#"Red"])
{
av.image = [UIImage imageNamed:#"redPanel.png"];
}
if ([ambitionCOL isEqualToString:#"Green"])
{
av.image = [UIImage imageNamed:#"greenPanel.png"];
}
if ([ambitionCOL isEqualToString:#"Purple"])
{
av.image = [UIImage imageNamed:#"purplePanel.png"];
}
cell.backgroundView = av;
lbl123.text = #"TestING!";
}
if ([expandedSections containsIndex:indexPath.section]){}
else{}
}
else
{
if(indexPath.section == 2)
{
cellValue = Result1Moretext;
}
if(indexPath.section == 3)
{
cellValue = Result2Moretext;
}
static NSUInteger const k1 = 1;
// Declare references to the subviews which will display the data.
UILabel *lbl1 = nil;
static NSString *kCellID = #"CellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellID];
lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 290, 300)];
lbl1.font = [UIFont fontWithName:#"Trebuchet MS" size:12.0f];
lbl1.tag = k1;
lbl1.textColor = [UIColor blackColor];
lbl1.backgroundColor = [UIColor clearColor];
lbl1.highlightedTextColor = [UIColor whiteColor];
lbl1.opaque = NO;
lbl1.adjustsFontSizeToFitWidth = TRUE;
lbl1.numberOfLines = 0;
lbl1.lineBreakMode = UILineBreakModeWordWrap;
[cell.contentView addSubview:lbl1];
}else {
lbl1 = (UILabel *)[cell.contentView viewWithTag:k1];
}
cell.accessoryView.tag = indexPath.row;
lbl1.text = [NSString stringWithFormat:#"%#", cellValue];
return cell;
}
}
else
{
}
}
Thanks
Don't forget that cells are reused in UITableViews to keep memory usage low. So what's happening is you are making a cell, and adding the label to it, then after that cell scrolls of the screen it goes into a reuse pool, gets re-added as a new cell further down and still has the label in it.
What you should do is create all labels etc only when you make a new cell (in the cell==nil section, that's where a cell was not dequeued), and set all the labels in the rest of the code.
You can use view tags to find the label views again later.
eg.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MyIdentifier"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:#"MyIdentifier"];
lbl123 = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 290, 20)];
lbl123.font = [UIFont fontWithName:#"Trebuchet MS" size:12.0f];
lbl123.textColor = [UIColor blackColor];
lbl123.backgroundColor = [UIColor clearColor];
lbl123.highlightedTextColor = [UIColor whiteColor];
lbl123.opaque = NO;
lbl123.adjustsFontSizeToFitWidth = TRUE;
lbl123.numberOfLines = 0;
lbl123.lineBreakMode = UILineBreakModeWordWrap;
[cell addSubview:lbl123];
lbl123.text = #"";
lbl123.tag=123;
} else {
lbl123 = (UILabel *)[cell viewWithTag:123];
}
if(indexPath.section == 2)
{
lbl123.text = #"Test";
}

images duplicating when scrolling the uitableview

I have a uitableview and I have assigned images to each row. The tableView displays chat messages and images of a chat bubble (as in ios messaging app).
The scrolling is very slow and images are seems to be duplicating as scrolling up and down.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"tableView method called AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
//static NSString *CellIdentifier = #"Cell";
//---add this---
UILabel* dateLabel = nil;
UILabel* messageLabel = nil;
UIImageView *imageView_top_left = nil;
UIImageView *imageView_top_middle = nil;
UIImageView *imageView_top_right = nil;
UIImageView *imageView_middle_left = nil;
UIImageView *imageView_middle_right = nil;
UIImageView *imageView_middle_middle = nil;
UIImageView *imageView_bottom_left = nil;
UIImageView *imageView_bottom_middle = nil;
UIImageView *imageView_bottom_right = nil;
/*UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}*/
ChatMessage *chatMessage = [self.chatMessagesList objectAtIndex:indexPath.row];
// Check whether this message is to me
if (![chatMessage.fromId isEqualToString:myId]){
/////////// changed //////////
static NSString *CellIdentifier = #"Cell1";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
////////////////////////////
NSLog(#"AAAAAAAAAAAAAAAAA");
/////////////////// image ////////////////////////////////////////////////////////////
NSURL *avatarUrl = [NSURL URLWithString:
#"http://www.mobyn.com/mobyn_v3/application/upload/profile_images/default.jpg"];
UIImage *avatarImage = [UIImage imageWithData: [NSData dataWithContentsOfURL:avatarUrl]];
UIImageView *avatar = [[UIImageView alloc] initWithImage:avatarImage];
avatar.frame = CGRectMake(0, 23, 60, 60);
[cell.contentView addSubview:avatar];
//[avatarImage release];
//[avatar release];
//////////////////////////////////////////////////////////////////////////////////////
// If YES, then create the row with the corresponding bubble
//---add this---
//---date---
// dateLabel = [[[UILabel alloc] init] autorelease];
//dateLabel.tag = DATELABEL_TAG;
//[cell.contentView addSubview: dateLabel];
//---top left---
imageView_top_left = [[[UIImageView alloc] init] autorelease];
imageView_top_left.tag = IMAGEVIEW_TAG_1;
[cell.contentView addSubview: imageView_top_left];
//---top middle---
imageView_top_middle = [[[UIImageView alloc] init] autorelease];
imageView_top_middle.tag = IMAGEVIEW_TAG_2;
[cell.contentView addSubview: imageView_top_middle];
//---top right---
imageView_top_right = [[[UIImageView alloc] init] autorelease];
imageView_top_right.tag = IMAGEVIEW_TAG_3;
[cell.contentView addSubview: imageView_top_right];
//---middle left---
imageView_middle_left = [[[UIImageView alloc] init] autorelease];
imageView_middle_left.tag = IMAGEVIEW_TAG_4;
[cell.contentView addSubview: imageView_middle_left];
//---middle middle---
imageView_middle_middle = [[[UIImageView alloc] init] autorelease];
imageView_middle_middle.tag = IMAGEVIEW_TAG_5;
[cell.contentView addSubview: imageView_middle_middle];
//---middle right---
imageView_middle_right = [[[UIImageView alloc] init] autorelease];
imageView_middle_right.tag = IMAGEVIEW_TAG_6;
[cell.contentView addSubview: imageView_middle_right];
//---bottom left---
imageView_bottom_left = [[[UIImageView alloc] init] autorelease];
imageView_bottom_left.tag = IMAGEVIEW_TAG_7;
[cell.contentView addSubview: imageView_bottom_left];
//---bottom middle---
imageView_bottom_middle = [[[UIImageView alloc] init] autorelease];
imageView_bottom_middle.tag = IMAGEVIEW_TAG_8;
[cell.contentView addSubview: imageView_bottom_middle];
//---bottom right---
imageView_bottom_right = [[[UIImageView alloc] init] autorelease];
imageView_bottom_right.tag = IMAGEVIEW_TAG_9;
[cell.contentView addSubview: imageView_bottom_right];
//---message---
messageLabel = [[[UILabel alloc] init] autorelease];
messageLabel.tag = MESSAGELABEL_TAG;
[cell.contentView addSubview: messageLabel];
//---set the images to display for each UIImageView---
imageView_top_left.image =
[UIImage imageNamed:#"bubble_top_left2.png"];
imageView_top_middle.image =
[UIImage imageNamed:#"bubble_top_middle2.png"];
imageView_top_right.image =
[UIImage imageNamed:#"bubble_top_right2.png"];
imageView_middle_left.image =
[UIImage imageNamed:#"bubble_middle_left2.png"];
imageView_middle_middle.image =
[UIImage imageNamed:#"bubble_middle_middle2.png"];
imageView_middle_right.image =
[UIImage imageNamed:#"bubble_middle_right2.png"];
imageView_bottom_left.image =
[UIImage imageNamed:#"bubble_bottom_left2.png"];
imageView_bottom_middle.image =
[UIImage imageNamed:#"bubble_bottom_middle2.png"];
imageView_bottom_right.image =
[UIImage imageNamed:#"bubble_bottom_right2.png"];
///////////////////////////////////////////////
//---calculate the height for the label---
int labelHeight = [self labelHeight:messsageForTheRow];
labelHeight -= bubbleFragment_height;
if (labelHeight<0) labelHeight = 0;
//---you can customize the look and feel for the date for each message here---
dateLabel.frame = CGRectMake(0.0, 0.0, 200, 15.0);
dateLabel.font = [UIFont boldSystemFontOfSize: FONTSIZE];
dateLabel.textAlignment = UITextAlignmentLeft;
dateLabel.textColor = [UIColor darkGrayColor];
dateLabel.backgroundColor = [UIColor clearColor];
//---top left---
imageView_top_left.frame =
CGRectMake(bubble_x, bubble_y, bubbleFragment_width, bubbleFragment_height);
//---top middle---
imageView_top_middle.frame =
CGRectMake(bubble_x + bubbleFragment_width, bubble_y,
bubbleFragment_width, bubbleFragment_height);
//---top right---
imageView_top_right.frame =
CGRectMake(bubble_x + (bubbleFragment_width * 2), bubble_y,
bubbleFragment_width, bubbleFragment_height);
//---middle left---
imageView_middle_left.frame =
CGRectMake(bubble_x, bubble_y + bubbleFragment_height,
bubbleFragment_width, labelHeight);
//---middle middle---
imageView_middle_middle.frame =
CGRectMake(bubble_x + bubbleFragment_width, bubble_y + bubbleFragment_height,
bubbleFragment_width, labelHeight);
//---middle right---
imageView_middle_right.frame =
CGRectMake(bubble_x + (bubbleFragment_width * 2),
bubble_y + bubbleFragment_height,
bubbleFragment_width, labelHeight);
//---bottom left---
imageView_bottom_left.frame =
CGRectMake(bubble_x, bubble_y + bubbleFragment_height + labelHeight,
bubbleFragment_width, bubbleFragment_height );
//---bottom middle---
imageView_bottom_middle.frame =
CGRectMake(bubble_x + bubbleFragment_width,
bubble_y + bubbleFragment_height + labelHeight,
bubbleFragment_width, bubbleFragment_height);
//---bottom right---
imageView_bottom_right.frame =
CGRectMake(bubble_x + (bubbleFragment_width * 2),
bubble_y + bubbleFragment_height + labelHeight,
bubbleFragment_width, bubbleFragment_height );
//---you can customize the look and feel for each message here---
messageLabel.frame =
CGRectMake(bubble_x + 10, bubble_y + 5,
(bubbleFragment_width * 3) - 25,
(bubbleFragment_height * 2) + labelHeight - 10);
messageLabel.font = [UIFont systemFontOfSize:FONTSIZE];
messageLabel.textAlignment = UITextAlignmentCenter;
messageLabel.textColor = [UIColor darkTextColor];
messageLabel.numberOfLines = 0; //---display multiple lines---
messageLabel.backgroundColor = [UIColor clearColor];
messageLabel.lineBreakMode = UILineBreakModeWordWrap;
///// changed ///////
messsageForTheRow = chatMessage.messsage;
messageLabel.text = messsageForTheRow ;
return cell;
////////////////////
} else {
/////////// changed //////////
static NSString *CellIdentifier = #"Cell2";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
////////////////////////////
// If NO, then create the row with the bubble which shows it from me
// If YES, then create the row with the corresponding bubble
//---add this---
//---date---
// dateLabel = [[[UILabel alloc] init] autorelease];
//dateLabel.tag = DATELABEL_TAG;
//[cell.contentView addSubview: dateLabel];
/////////////////// image ////////////////////////////////////////////////////////////
NSURL *avatarUrl = [NSURL URLWithString:
#"http://www.mobyn.com/mobyn_v3/application/upload/profile_images/default.jpg"];
UIImage *avatarImage = [UIImage imageWithData: [NSData dataWithContentsOfURL:avatarUrl]];
UIImageView *avatar = [[UIImageView alloc] initWithImage:avatarImage];
avatar.frame = CGRectMake(255, 23, 60, 60);
[cell.contentView addSubview:avatar];
//[avatarImage release];
//[avatar release];
//////////////////////////////////////////////////////////////////////////////////////
NSLog(#"BBBBBBBBBBBBBBBBB");
//---top left---
imageView_top_left = [[[UIImageView alloc] init] autorelease];
imageView_top_left.tag = IMAGEVIEW_TAG_1;
[cell.contentView addSubview: imageView_top_left];
//---top middle---
imageView_top_middle = [[[UIImageView alloc] init] autorelease];
imageView_top_middle.tag = IMAGEVIEW_TAG_2;
[cell.contentView addSubview: imageView_top_middle];
//---top right---
imageView_top_right = [[[UIImageView alloc] init] autorelease];
imageView_top_right.tag = IMAGEVIEW_TAG_3;
[cell.contentView addSubview: imageView_top_right];
//---middle left---
imageView_middle_left = [[[UIImageView alloc] init] autorelease];
imageView_middle_left.tag = IMAGEVIEW_TAG_4;
[cell.contentView addSubview: imageView_middle_left];
//---middle middle---
imageView_middle_middle = [[[UIImageView alloc] init] autorelease];
imageView_middle_middle.tag = IMAGEVIEW_TAG_5;
[cell.contentView addSubview: imageView_middle_middle];
//---middle right---
imageView_middle_right = [[[UIImageView alloc] init] autorelease];
imageView_middle_right.tag = IMAGEVIEW_TAG_6;
[cell.contentView addSubview: imageView_middle_right];
//---bottom left---
imageView_bottom_left = [[[UIImageView alloc] init] autorelease];
imageView_bottom_left.tag = IMAGEVIEW_TAG_7;
[cell.contentView addSubview: imageView_bottom_left];
//---bottom middle---
imageView_bottom_middle = [[[UIImageView alloc] init] autorelease];
imageView_bottom_middle.tag = IMAGEVIEW_TAG_8;
[cell.contentView addSubview: imageView_bottom_middle];
//---bottom right---
imageView_bottom_right = [[[UIImageView alloc] init] autorelease];
imageView_bottom_right.tag = IMAGEVIEW_TAG_9;
[cell.contentView addSubview: imageView_bottom_right];
//---message---
messageLabel = [[[UILabel alloc] init] autorelease];
messageLabel.tag = MESSAGELABEL_TAG;
[cell.contentView addSubview: messageLabel];
//---set the images to display for each UIImageView---
imageView_top_left.image =
[UIImage imageNamed:#"bubble_top_left.png"];
imageView_top_middle.image =
[UIImage imageNamed:#"bubble_top_middle.png"];
imageView_top_right.image =
[UIImage imageNamed:#"bubble_top_right.png"];
imageView_middle_left.image =
[UIImage imageNamed:#"bubble_middle_left.png"];
imageView_middle_middle.image =
[UIImage imageNamed:#"bubble_middle_middle.png"];
imageView_middle_right.image =
[UIImage imageNamed:#"bubble_middle_right.png"];
imageView_bottom_left.image =
[UIImage imageNamed:#"bubble_bottom_left.png"];
imageView_bottom_middle.image =
[UIImage imageNamed:#"bubble_bottom_middle.png"];
imageView_bottom_right.image =
[UIImage imageNamed:#"bubble_bottom_right.png"];
/////////////////////////////////////////////////
//---calculate the height for the label---
int labelHeight = [self labelHeight:messsageForTheRow];
labelHeight -= bubbleFragment_height;
if (labelHeight<0) labelHeight = 0;
//---you can customize the look and feel for the date for each message here---
dateLabel.frame = CGRectMake(0.0, 0.0, 200, 15.0);
dateLabel.font = [UIFont boldSystemFontOfSize: FONTSIZE];
dateLabel.textAlignment = UITextAlignmentLeft;
dateLabel.textColor = [UIColor darkGrayColor];
dateLabel.backgroundColor = [UIColor clearColor];
//---top left---
imageView_top_left.frame =
CGRectMake(bubble_x2, bubble_y, bubbleFragment_width, bubbleFragment_height);
//---top middle---
imageView_top_middle.frame =
CGRectMake(bubble_x2 + bubbleFragment_width, bubble_y2,
bubbleFragment_width, bubbleFragment_height);
//---top right---
imageView_top_right.frame =
CGRectMake(bubble_x2 + (bubbleFragment_width * 2), bubble_y2,
bubbleFragment_width, bubbleFragment_height);
//---middle left---
imageView_middle_left.frame =
CGRectMake(bubble_x2, bubble_y2 + bubbleFragment_height,
bubbleFragment_width, labelHeight);
//---middle middle---
imageView_middle_middle.frame =
CGRectMake(bubble_x2 + bubbleFragment_width, bubble_y2 + bubbleFragment_height,
bubbleFragment_width, labelHeight);
//---middle right---
imageView_middle_right.frame =
CGRectMake(bubble_x2 + (bubbleFragment_width * 2),
bubble_y2 + bubbleFragment_height,
bubbleFragment_width, labelHeight);
//---bottom left---
imageView_bottom_left.frame =
CGRectMake(bubble_x2, bubble_y2 + bubbleFragment_height + labelHeight,
bubbleFragment_width, bubbleFragment_height );
//---bottom middle---
imageView_bottom_middle.frame =
CGRectMake(bubble_x2 + bubbleFragment_width,
bubble_y2 + bubbleFragment_height + labelHeight,
bubbleFragment_width, bubbleFragment_height);
//---bottom right---
imageView_bottom_right.frame =
CGRectMake(bubble_x2 + (bubbleFragment_width * 2),
bubble_y2 + bubbleFragment_height + labelHeight,
bubbleFragment_width, bubbleFragment_height );
//---you can customize the look and feel for each message here---
messageLabel.frame =
CGRectMake(bubble_x2 + 10, bubble_y2 + 5,
(bubbleFragment_width * 3) - 25,
(bubbleFragment_height * 2) + labelHeight - 10);
messageLabel.font = [UIFont systemFontOfSize:FONTSIZE];
messageLabel.textAlignment = UITextAlignmentCenter;
messageLabel.textColor = [UIColor darkTextColor];
messageLabel.numberOfLines = 0; //---display multiple lines---
messageLabel.backgroundColor = [UIColor clearColor];
messageLabel.lineBreakMode = UILineBreakModeWordWrap;
///// changed ///////
messsageForTheRow = chatMessage.messsage;
messageLabel.text = messsageForTheRow ;
return cell;
////////////////////
}
// Then set the data
messsageForTheRow = chatMessage.messsage;
messageLabel.text = messsageForTheRow ;
//--------------
/////////// changed //////////
UITableViewCell *cell = [UITableViewCell alloc];
return cell;
}
you need to put out this code from your IF statement:
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
move right after :
//---add this---
UILabel* dateLabel = nil;
UILabel* messageLabel = nil;
UIImageView *imageView_top_left = nil;
UIImageView *imageView_top_middle = nil;
UIImageView *imageView_top_right = nil;
UIImageView *imageView_middle_left = nil;
UIImageView *imageView_middle_right = nil;
UIImageView *imageView_middle_middle = nil;
UIImageView *imageView_bottom_left = nil;
UIImageView *imageView_bottom_middle = nil;
UIImageView *imageView_bottom_right = nil;
hope it's solve your problem
You're probably re-calling your chatMessage initialization/allocation in cellForRowAtIndexPath.
By default UITableView frees offscreen cell data when you scroll up/down. It calls cellForRowAtIndexPath again when you scroll back.
Try:
set a breakpoint in cellForRowAtIndexPath
disable it
scroll down
enable the breakpoint again
scroll back up again
Your app should break at cellForRowAtIndexPath.
My guess is you keep calling UITableViewCell *cell = [UITableViewCell alloc];.
Every time you scroll up and down the UITableView calls cellForRowAtIndexPath which ends up re-calling the cell allocation, hence, multiple images.

xcode iphone - jerky scroll UITableView CellForRowAtIndexPath

Almost sorted with my 1st app, just a simple news app but when I load it onto my iPhone the scroll seems jerky can someone have a look at my function and see if i'm doing something wrong.
I need the image on the right hand side thats why i'm using custom cells.
Thanks
For any help
#define DATELABEL_TAG 1 #define MAINLABEL_TAG 2 #define PHOTO_TAG 3
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MainNewsCellIdentifier = #"MainNewsCellIdentifier";
UILabel *mainLabel, *dateLabel;
UIImageView *photo;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: MainNewsCellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier: MainNewsCellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
dateLabel = [[[UILabel alloc] initWithFrame:CGRectMake(15.0,15.0,170.0,15.0)] autorelease];
dateLabel.tag = DATELABEL_TAG;
dateLabel.font = [UIFont systemFontOfSize:10.0];
dateLabel.textAlignment = UITextAlignmentLeft;
dateLabel.textColor = [UIColor darkGrayColor];
dateLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin; //| UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:dateLabel];
mainLabel = [[[UILabel alloc] initWithFrame:CGRectMake(15.0,28.0,170.0,60.0)] autorelease];
mainLabel.tag = MAINLABEL_TAG;
mainLabel.font = [UIFont boldSystemFontOfSize:14.0];
mainLabel.textColor = [UIColor blackColor];
mainLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin;
mainLabel.numberOfLines = 0;
//mainLabel.backgroundColor = [UIColor greenColor];
[cell.contentView addSubview:mainLabel];
photo = [[[UIImageView alloc] initWithFrame:CGRectMake(190.0,15.0,85.0,85.0)] autorelease];
photo.tag = PHOTO_TAG;
photo.contentMode = UIViewContentModeScaleAspectFit;//UIViewContentModeScaleAspectFit; //
[cell.contentView addSubview:photo];
}
else {
dateLabel = (UILabel *)[cell.contentView viewWithTag:DATELABEL_TAG];
mainLabel = (UILabel *)[cell.contentView viewWithTag:MAINLABEL_TAG];
photo = (UIImageView *)[cell.contentView viewWithTag:PHOTO_TAG];
}
NSUInteger row = [indexPath row];
NSDictionary *stream = (NSDictionary *) [dataList objectAtIndex:row];
NSString *title = [stream valueForKey:#"title"];
NSString *titleString = #"";
if( ! [title isKindOfClass:[NSString class]] )
{
titleString = #"";
}
else
{
titleString = title;
}
CGSize maximumSize = CGSizeMake(180, 9999);
UIFont *dateFont = [UIFont fontWithName:#"Helvetica" size:14];
CGSize dateStringSize = [titleString sizeWithFont:dateFont
constrainedToSize:maximumSize
lineBreakMode:mainLabel.lineBreakMode];
CGRect dateFrame = CGRectMake(15.0, 28.0, 170.0, dateStringSize.height);
mainLabel.frame = dateFrame;
mainLabel.text = titleString;
dateLabel.text = [stream valueForKey:#"created"];
NSString *i = [NSString stringWithFormat:#"http://www.website.co.uk/images/%#", [stream valueForKey:#"image"]];
NSData *imageURL = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:i]];
UIImage *newsImage = [[UIImage alloc] initWithData:imageURL];
photo.image = newsImage;
[imageURL release];
[newsImage release];
return cell;
}
The problem is this:
NSString *i = [NSString stringWithFormat:#"http://www.website.co.uk/images/%#", [stream valueForKey:#"image"]];
NSData *imageURL = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:i]];
UIImage *newsImage = [[UIImage alloc] initWithData:imageURL];
You effectively say here, that as soon as the cell needs to be displayed, the image must be fetched and presented. This will cost some time - too much for a good user experience.
You should fetch the images before or while you present the table view, and cache them, e.g. in an array. Or you must handle things asynchronously, meaning that you do the loading in the background, and not wait with return cell; until the image is actually downloaded. This will be a little harder to get right.
Even if you asynchronously download images, you'll still have a jerky scroll.
Why? It's lazy image decompression. ios performs decompression at the moment it will be displayed on the screen. You have to manually decompress the images in a background thread.
Decompression does not merely mean instantiating a UIImage object. It can be somewhat complicated. The best solution, is to download SDWebImage and use the image decompressor that's included. SDWebImage will asynchronously download and perform decompression for you.
To read more about the issue see: http://www.cocoanetics.com/2011/10/avoiding-image-decompression-sickness/

Resources