IPad--how to refresh subview on ipad? - xcode

I add 4 subviews(UIScrollView,UITableView)in main view on ipad, and each view has it own data.
In Marco view, I try to reload data from server every 3 second to refresh my Marco View.
as first pic..
I set 3 Marco icons on ipad main view, when I delete one on popoverView.
Icons on popoverView really delete, but still show up 3 icons on main view.
as second and third pic...
It must be delete from background and then open app it gonna be correct view..
as fourth pic..
Dose any one know what's problem.. I try to use [marco_scrollview setNeedsDisplay] .
but it no work..
Thank in advance..
here is my code to get Marcodata:
- (void)getTheMacroURLData{
//[self.view setNeedsDisplay];
// [marco_scrollview setNeedsDisplay];
self.macroList = nil;
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
EnvoTouchAppDelegate *appDelegate = (EnvoTouchAppDelegate *)[[UIApplication sharedApplication] delegate];
struct sockaddr_in Addr;
bzero(&Addr, sizeof(Addr));
Addr.sin_len = sizeof(Addr);
Addr.sin_family = AF_INET;
SCNetworkReachabilityRef target = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *) &Addr);
SCNetworkReachabilityFlags flags;
SCNetworkReachabilityGetFlags(target, &flags);
if (flags & kSCNetworkReachabilityFlagsIsWWAN){
//Serial Number not save in the keychain now
NSString *theNewURLString = [NSString stringWithFormat:eGetAllMacrosSettingURL3G,[userDefaults objectForKey:#"ecsn"],appDelegate.udid];
NSURL *theNewURL = [NSURL URLWithString:theNewURLString];
self.macroList = [NSMutableArray arrayWithContentsOfURL:theNewURL];
}
if (flags & kSCNetworkFlagsReachable){
NSString *theNewURLString = [NSString stringWithFormat:eGetAllMacrosSettingURLWiFi,appDelegate.ipString,appDelegate.udid];
NSURL *theNewURL = [NSURL URLWithString:theNewURLString];
self.macroList = [NSMutableArray arrayWithContentsOfURL:theNewURL];
}
int k=0;
for (int i=0; i<[self.macroList count]; i++) {
int j=i%9;
if (j==0) {
k++;
}
UIButton *iconButton=[UIButton buttonWithType:UIButtonTypeCustom];
NSString *picString = [[self.macroList objectAtIndex:i] valueForKey: #"pid"];
UIImage *buttonImage = [UIImage imageNamed:picString];
[iconButton setImage:buttonImage forState:UIControlStateNormal];
[iconButton setTag:i];
[iconButton addTarget:self action:#selector(exeMacro:) forControlEvents:UIControlEventTouchUpInside];
[iconButton setFrame:CGRectMake(30+(i%9*80), 17+((k-1)*70), 48, 48)];
//[self.view addSubview:iconButton];
[marco_scrollview addSubview:iconButton];
if ([self.macroList count] > 18) {
[marco_scrollview setContentSize:CGSizeMake(320, 525 + (([self.macroList count]/4 - 4) * 70) )];
}
}
[marco_view addSubview:marco_scrollview];
}
And I try to reload data every 3 second..
I dont know why it doesn't refresh Marco View..

The display makes it appear as if you are using a tableview somewhere. If so, are you calling the reloadData method on the table view?

Related

Load images on background thread in Spritekit

Right guys, I have scroll view in my SKScene running from a SKAction with the following code -
for (int i=0; i<[listOfImages count]; i++) {
NSDictionary *myDic = [listOfImages objectAtIndex:i];
NSString *urlImage = [myDic objectForKey:#"product_image"];
NSURL *imageURL = [NSURL URLWithString:urlImage];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *cacheImage = [UIImage imageWithData:imageData];
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/4+leftMargin, 0, cacheImage.size.width/2, cacheImage.size.height/2)];
[image setImage:cacheImage];
image.tag = tag;
image.contentMode = UIViewContentModeScaleAspectFit;
[scrollView addSubview:image];
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
recognizer.numberOfTapsRequired = 1;
recognizer.numberOfTouchesRequired = 1;
recognizer.delegate = self;
[image addGestureRecognizer:recognizer];
[image setUserInteractionEnabled:YES];
leftMargin += SCREEN_WIDTH;
tagValue += 1;
tag += 1;
}
I need to understand how to load the images in the background as there are almost 140 images to load and its taking 5 minutes to load everything and show. Please help me out !!
You could have a queue where one load ending triggers another, as opposed to triggering all the loads at once.
I did a google search and got this if you need help with applying the concept :
http://khanlou.com/2012/08/asynchronous-downloaded-images-with-caching/

MapView Problems - Pin change color when map reloded

I have a problem with the PIn color mapView when a refresh is done.
In my I app i display some point with two color in order to identify if a service is available.
On the first start, no problems appear. The code is the follower:
- (void)viewDidLoad
{
[super viewDidLoad];
[self dowloadPoint]; // here I exucte the first start
}
- (void)dowloadPoint{
NSURL *url1 =[NSURL URLWithString:#"http:MYUSRL"];
NSData *datos1 =[[NSData alloc] initWithContentsOfURL:url1];
[self plotBarPosition:datos_string1]; //Here I call the plotBarPosition method
}
- (void)plotBarPosition:(NSString *)datos_string1 {
for (id<MKAnnotation> annotation in _mapView.annotations) {
[_mapView removeAnnotation:annotation];
}
// Parse the string into JSON
NSDictionary *json = [(NSDictionary*)[datos_string1 JSONValue]objectForKey:#"features"];
// Get the objects you want, e.g. output the second item's client id
NSArray *items_properties = [json valueForKeyPath:#"properties"];
NSArray *items_geo = [json valueForKeyPath:#"geometry"];
for (int i = 0; i < [json count]; i++){
NSString *nomprePunto =[[items_properties objectAtIndex:i] objectForKey:#"title"];
NSNumber *lat =[[[items_geo objectAtIndex:i] objectForKey:#"coordinates"] objectAtIndex:0];
NSNumber *lon =[[[items_geo objectAtIndex:i] objectForKey:#"coordinates"] objectAtIndex:1];
CLLocationCoordinate2D coordinate;
coordinate.latitude = lat.doubleValue;
coordinate.longitude = lon.doubleValue;
//ESTADO
NSString *description = [[items_properties objectAtIndex:i] objectForKey:#"description"];
NSString *estado_punto = [[NSString alloc]init];
if ([description rangeOfString:#"Averiado"].location == NSNotFound) {
estado_punto = #"Available";
} else {
estado_punto = #"NOt Available";
averiados ++;
}
NSString *averiadosStr = [NSString stringWithFormat:#"%d",averiados];
averiadosLabel.text = averiadosStr;
MyLocation *location =[[MyLocation alloc] initWithName:nomprePunto coordinate:coordinate estado:estado_punto];
[_mapView addAnnotation:location];
}
}
- (MKPinAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(MyLocation *)annotation {
static NSString *identifier = #"MyLocation";
if ([annotation isKindOfClass:[MyLocation class]]) {
MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [_mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
if([[annotation estado] isEqualToString:#"En Servicio"])
annotationView.pinColor = MKPinAnnotationColorGreen;
} else {
annotationView.annotation = annotation;
}
return annotationView;
}
return nil;
}
But whe I add a refres button that is function is simply a refreshcalling the dowloadPoint once again,
- (IBAction)refresh{
[self dowloadPoint];
}
the color of pins change in a "random manner", not corrisponding with the real state of point.
Any ideas about what is happening? Thanks in advance.
EDIT: It seemps pproblems is due to:
for (id<MKAnnotation> annotation in _mapView.annotations) {
[_mapView removeAnnotation:annotation];
}
erasing it, the app work properly but pins area drown abow the previous ones...:S
The default color of the pin is red. You set it to green if the estado property of your MyLocation object is equal to #"En Servicio". I understand that sometimes the color is red, when your estado property is equal to #"En Servicio", or sometimes green when it is not.
One reason could be that your MyLocation object simply does no longer exist when you press the refresh button. In this case, you might still have a pointer to the memory location where it once existed, but this location may have been overwritten by anything, causing a random color.
This can happen e.g. if your MyLocation object has been created as an autorelease object that has been released when you returned to the main event loop, i.e. to handle user interactions.
This should not be the case if you are using ARC.

access an UIImage instance variable and display it in UIImageView

I'm trying to access an UIImage instance variable and display it in UIImageView. When I try to NSLog the path I get null. I can manually display a pic through the IB, but I want to do this strictly through code
#import "Deck.h"
#import "Card.h"
#implementation Deck
#synthesize cards;
- (id) init
{
if(self = [super init])
{
cards = [[NSMutableArray alloc] init];
NSInteger aCount, picNum = 0;
for(int suit = 0; suit < 4; suit++)
{
for(int face = 1; face < 14; face++, picNum++)
{
NSString *fileName = [NSString stringWithFormat:#"card_%d", picNum];
NSString *path = [[NSBundle mainBundle] pathForResource:fileName
ofType:#"png"inDirectory:#"/cards"];
NSLog(#"%#", path); //outputs correctly
UIImage *output = [UIImage imageNamed:path];
NSLog(#"%#", output); //outputs null
Card *card = [[Card alloc] initWithFaceValue:(NSInteger)face
countValue:(NSInteger)aCount
suit:(Suit)suit
cardImage:(UIImage *)output];
[cards addObject:card];
}
}
}
return self;
}
I've added a link to show where the pics are found
Link
You don't have to include all the path to the image, if you just put the name, Xcode will automatically look for it.

Program doesn't jump in Delegate-Method

Good Morning Community,
I have a problem, because I have implemented an CoverFlowViewController.m with an Delegate method, but the Debugger NEVER jump in this method, does anyone know why?
Here is the code:
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *weiter = [UIButton buttonWithType:UIButtonTypeRoundedRect];
weiter.frame = CGRectMake(100, 400, 120, 40);
[weiter addTarget:self action:#selector(goToChart) forControlEvents:UIControlEventTouchUpInside];
NSString *ansicht = #"Weiter";
[weiter setTitle:ansicht forState:UIControlStateNormal];
[self.view addSubview:weiter];
// loading images into the queue
loadImagesOperationQueue = [[NSOperationQueue alloc] init];
NSString *imageName;
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)];
[(AFOpenFlowView *)self.view setImage:scaledImage forIndex:i];
}
[(AFOpenFlowView *)self.view setNumberOfImages:10];
}
- (void)openFlowView:(AFOpenFlowView *)openFlowView selectionDidChange:(int)index{
NSLog(#"%d is selected",index);
}
Does anyone know why the Debugger (= the Programm) never jump into the selectionDidChange() method?
I have no solution since three days, and hope somebody could help me?
Greetings and Thank you beforehand
Marco
are you sure you connected the viewDelegate to your viewController?
Please add NSLog(#"Delegate: %#", ((AFOpenFlowView*)self.view).viewDelegate); at the end of your viewDidLoad method to see if it is connected.
Add
((AFOpenFlowView *)self.view).viewDelegate = self; on the init or viewDidLoad method

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