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.
Related
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)
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];
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";
}
i use the following code for adding more images on scroll
UIImageView* imageshow=[[UIImageView alloc] initWithFrame:CGRectZero];
NSString* pathright = [[NSBundle mainBundle] pathForResource:#"images0" ofType:#"png"];
imageshow.image = [UIImage imageWithContentsOfFile:pathright];
[imageshow setFrame:CGRectMake(0, 0, 890, 430)];
UIImageView* imageshow1=[[UIImageView alloc] initWithFrame:CGRectZero];
NSString* pathright1 = [[NSBundle mainBundle] pathForResource:#"images1" ofType:#"png"];
imageshow1.image = [UIImage imageWithContentsOfFile:pathright1];
[imageshow1 setFrame:CGRectMake(890, 0, 890, 430)];
UIScrollView* scrollViewright = [[UIScrollView alloc] initWithFrame:CGRectMake(67,169,890, 430)];
[scrollViewright setContentSize:CGSizeMake(890*2,430)];
scrollViewright.pagingEnabled = YES;
[scrollViewright addSubview:imageshow];
[scrollViewright addSubview:imageshow1];
[self.view addSubview:scrollViewright];
but if i have more images,such as i have imagei should add more code ,so can i use loop (for?) to do this function? thanks
Try something like this:
NSUInteger tot = 5;
UIScrollView* scrollViewright = [[UIScrollView alloc] initWithFrame:CGRectMake(67,169,890, 430)];
[scrollViewright setContentSize:CGSizeMake(890*tot,430)];
scrollViewright.pagingEnabled = YES;
for (NSUInteger i = 0; i < tot; ++i) {
UIImageView* imageshow=[[UIImageView alloc] initWithFrame:CGRectZero];
NSString* pathright = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"images%d", i + 1] ofType:#"png"];
imageshow.image = [UIImage imageWithContentsOfFile:pathright];
[imageshow setFrame:CGRectMake(i * 890, 0, 890 * (i + 1), 430)];
[scrollViewright addSubview:imageshow];
[imageshow release];
}
[self.view addSubview:scrollViewright];
i have implemented the following viewDidLoad() Method:
- (void)viewDidLoad {
[super viewDidLoad];
// loading images into the queue
loadImagesOperationQueue = [[NSOperationQueue alloc] init];
AFOpenFlowView *openFlow = self.view;
theControl = [[UIControl alloc] initWithFrame:self.view.frame];
NSString *imageName;
btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(10, 10, 40, 40);
[btn addTarget:self action:#selector(testMethode) forControlEvents:UIControlEventTouchUpInside];
[btn setTitle:#"<<" forState:UIControlStateNormal];
[btn setHidden:YES];
[theControl addSubview:btn];
for (int i=0; i < 10; i++) {
imageName = [[NSString alloc] initWithFormat:#"cover_%d.jpg", i];
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
UIImage *aktuellesImage = imageView.image;
UIImage *scaledImage = [aktuellesImage scaleToSize:CGSizeMake(100.0f, 100.0f)];
[openFlow setImage:scaledImage forIndex:i];
// [(AFOpenFlowView *)self.view setImage:scaledImage forIndex:i];
[imageName release];
[aktuellesImage release];
}
[theControl addSubview:openFlow];
[theControl setUserInteractionEnabled:YES];
[(AFOpenFlowView *)self.view setNumberOfImages:10];
}
And the following touchesEnded() Method:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[btn setHidden:NO];
}
But the touchesEnded doesn't work, and the Button will not shown when i touch the picture, does anybody know what is the Problem???
Greetings Marco
Make sure your view has user interaction enabled in order to register touches. This can be set in your View Controller thusly:
self.view.userInteractionEnabled = YES;