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];
Related
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 currently have a scroll view loading images from an array, here's my code:
icons = [[NSMutableArray alloc]initWithObjects:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"one" ofType:#"png"]], [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"two" ofType:#"png"]], [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"three" ofType:#"png"]],nil];
int numberOfItems = [icons count];
NSLog(#"Number of objects: %i",numberOfItems);
for (int i = 0; i < numberOfItems; i++) {
if (i <= 3) {
finalFrame = CGRectMake( i*(80), 12, 80, 80);
}
UIButton *iconButton = [UIButton buttonWithType:UIButtonTypeCustom];
iconButton = [UIButton buttonWithType:UIButtonTypeCustom];
iconButton.frame = CGRectMake(0, 0, 57, 57);
iconButton.center = CGPointMake(40, 29);
iconButton.imageView.contentMode = UIViewContentModeScaleToFill;
[iconButton addTarget:self action:#selector(selected:) forControlEvents:UIControlEventTouchUpInside];
iconButton.tag = i;
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
UIImage *icon = [icons objectAtIndex:i];
dispatch_sync(dispatch_get_main_queue(), ^{
[iconButton setImage:icon forState:UIControlStateNormal];
});
});
[sub addSubview:iconButton];
sub = [[UIView alloc] initWithFrame:finalFrame];
[scrollView addSubview:sub];
}
The NSLog reads 3 objects, but my scroll view is only showing 2 of them, what am I doing wrong?
I think in it is in for loop for it should be like this
(int i = 0; i <= numberOfItems; i++)
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.
I am trying to change the desktop image; the procedure I've come up with is below. The first time this code is run, the resized image is displayed on screen as wallpaper, but the next time there is no reaction. What am I doing wrong?
-(IBAction)click:(id)sender
{
NSData *sourceData;
NSError *error;
NSFileManager *filemgr;
filemgr = [NSFileManager defaultManager];
screenArray = [NSScreen screens];
screenCount = [screenArray count];
unsigned index = 0;
for (index; index < screenCount; index++)
{
screenz = [screenArray objectAtIndex: index];
screenRect = [screenz visibleFrame];
}
NSLog(#"%fx%f",screenRect.size.width, screenRect.size.height);
arrCatDetails = [strCatDetails componentsSeparatedByString:appDelegate.strColDelimiter];
NSString *imageURL = [NSString stringWithFormat:#"upload/product/image/%#_%#_%d.jpg",[arrCatDetails objectAtIndex:0],appDelegate.str104by157Name,iSelectedImgIndex];
NSString *ima = [imageURL lastPathComponent];
NSString *str = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents"];
NSString *dataFilePath = [str stringByAppendingPathComponent:ima];
NSString *imagePath = [NSString stringWithFormat:#"file://localhost%#",dataFilePath];
NSURL *url = [[NSURL alloc] init];
url = [NSURL URLWithString:imagePath];
sourceData = [NSData dataWithContentsOfURL:url];
sourceImage = [[NSImage alloc] initWithData: sourceData];
resizedImage = [[NSImage alloc] initWithSize: NSMakeSize(screenRect.size.width, screenRect.size.height)];
NSSize originalSize = [sourceImage size];
[resizedImage lockFocus];
[sourceImage drawInRect: NSMakeRect(0, 0, screenRect.size.width, screenRect.size.height) fromRect: NSMakeRect(0, 0, originalSize.width, originalSize.height) operation: NSCompositeSourceOver fraction: 1.0];
[resizedImage unlockFocus];
NSData *resizedData = [resizedImage TIFFRepresentation];
NSBitmapImageRep* theImageRepresentation = [NSBitmapImageRep imageRepWithData:resizedData];
newimage = #"editwall.jpg";
newFilePath = [str stringByAppendingPathComponent:newimage];
NSData* theImageData = [theImageRepresentation representationUsingType:NSJPEGFileType properties:nil];
[theImageData writeToFile: newFilePath atomically: YES];
if([filemgr fileExistsAtPath:newFilePath] == YES)
{
imagePath1 = [NSString stringWithFormat:#"file://localhost%#",newFilePath];
urlz = [NSURL URLWithString:imagePath1];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:nil, NSWorkspaceDesktopImageFillColorKey, [NSNumber numberWithBool:NO], NSWorkspaceDesktopImageAllowClippingKey, [NSNumber numberWithInteger:NSImageScaleProportionallyUpOrDown], NSWorkspaceDesktopImageScalingKey, nil];
[[NSWorkspace sharedWorkspace] setDesktopImageURL:urlz forScreen:[[NSScreen screens] lastObject] options:options error:&error];
}
else
{
NSLog(#"No");
}
[sourceImage release];
[resizedImage release];
}
Why not try -[NSWorkspace setDesktopImageURL:forScreen:options:error:]? Apple has a sample project called DesktopImage to give you some idea how to use it.
Edit (after reading your code more carefully):
The problem you're having may be because of your call to +[NSDictionary dictionaryWithObjectsAndKeys:] See the nil at the end of the list of arguments? That's how you tell NSDictionary that your argument list is done. You can't put nil in the list, because it will stop reading the list at that point. If you want to specify a key that has no value, you have to use [NSNull null].
An aside: you've got a memory management issue in your code:
// allocates memory for an NSURL
NSURL * url = [[NSURL alloc] init];
// allocates more memory for an NSURL, and leaks
// the earlier allocation
url = [NSURL URLWithString:imagePath];
Just do one or the other:
// If you do it this way, you will have to call
// [url release] later
NSURL * url = [[NSURL alloc] initWithString:imagePath];
// This memory will be released automatically
NSURL * otherUrl = [NSURL URLWithString:imagePath];
enter code here- (void)viewDidLoad {
NSMutableArray *imageViewArray= [[NSMutableArray alloc] init];
for ( int i = 1; i < 10 ; i++ ) {
NSString *imgName = [NSString stringWithFormat:#"pic%i.jpg", i];
UIImage *img = [UIImage imageNamed:imgName];
[imageViewArray addObject:img];
}
self.gallery = imageViewArray;
[imageViewArray release];
NSString *p1 = [NSString stringWithFormat:#"p1.jpg"];
NSString *p2 = [NSString stringWithFormat:#"p2.jpg"];
NSString *p3 = [NSString stringWithFormat:#"p3.jpg"];
NSString *p4 = [NSString stringWithFormat:#"p4.jpg"];
NSString *p5 = [NSString stringWithFormat:#"p5.jpg"];
NSString *p6 = [NSString stringWithFormat:#"p6.jpg"];
NSString *p7 = [NSString stringWithFormat:#"p7.jpg"];
NSString *p8 = [NSString stringWithFormat:#"p8.jpg"];
NSString *p9 = [NSString stringWithFormat:#"p9.jpg"];
UIImage *P1V = [UIImage imageNamed:p1];
UIImage *P2V = [UIImage imageNamed:p2];
UIImage *P3V = [UIImage imageNamed:p3];
UIImage *P4V = [UIImage imageNamed:p4];
UIImage *P5V = [UIImage imageNamed:p5];
UIImage *P6V = [UIImage imageNamed:p6];
UIImage *P7V = [UIImage imageNamed:p7];
UIImage *P8V = [UIImage imageNamed:p8];
UIImage *P9V = [UIImage imageNamed:p9];
NSArray *imageViewArray2 = [[NSArray alloc] initWithObjects:P1V,P2V,P3V,P4V,P5V,P6V,P7V,P8V,P9V,nil];
self.gallery2 = imageViewArray2;
[imageViewArray2 release];
NSArray *array = [[NSArray alloc] initWithObjects:#"A",#"B",#"C",#"D",#"E",#"F",#"G",#"H",#"I",nil];
self.list = array;
[array release];
[super viewDidLoad];
}
What you should be doing is putting the filenames in an array and simply replacing that variable inside a loop.
Here's some code to demonstrate that:
NSArray *dayStrings = [NSArray arrayWithObjects: #"Monday", #"Tuesday", #"Wednesday", #"Thursday", #"Friday", nil];
NSArray *timeStrings = [NSArray arrayWithObjects: #"9 AM", #"10 AM", #"11 AM", #"12 PM", #"1 PM", #"2 PM", #"3 PM", #"4 PM", #"5 PM", nil];
int i;
NSMutableAttributedString *day, *time;
for(i = 0; i < HORIZONTAL_CELL_COUNT; i++)
{
day = [[[NSMutableAttributedString alloc] initWithString:[dayStrings objectAtIndex:i] attributes:attributes] autorelease];
[days addObject:day];
}
for(i = 0; i < VERTICAL_CELL_COUNT; i++)
{
time = [[[NSMutableAttributedString alloc] initWithString:[timeStrings objectAtIndex:i] attributes:attributes] autorelease];
[times addObject:time];
}