views:

49

answers:

2

OK,First this program can load plist from URL by this code,and I put this in

- (void)viewdidLoad{
   NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.envolab.com/envotouch/ios_status_req_test.php"]
             cachePolicy:NSURLRequestUseProtocolCachePolicy
            timeoutInterval:60.0];

 NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
 NSLog(@"\n\nCONNECTION:   %@", theConnection);
 NSData *returnData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:nil error:nil]; 
 NSString *listFile = [[NSString alloc] initWithData:returnData encoding:NSASCIIStringEncoding];   
 self.plist = [listFile propertyList];
}

than I take the plist file to init the tableview cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

 static NSString *CellIdentifier = @"cell";

 LightCell0 *cell =(LightCell0 *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
 if (cell == nil) {
  cell = [[[LightCell0 alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
 }
 // Set up the cell…
 NSLog(@"Now you see me Load Data %i", indexPath.row);



 [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

  cell.lightLocation.text =  [[[self.plist objectAtIndex:indexPath.row] valueForKey: @"nodeName"]description];


  //read plist to init the image
  if ([[[plist objectAtIndex:indexPath.row] valueForKey: @"nodeStatus"] intValue] == 0){
    cell.lightImageView.image = [UIImage imageNamed:@"lightOff.png"]; 
   }

  else if([[[plist objectAtIndex:indexPath.row] valueForKey: @"nodeStatus"] intValue] == 1){
    cell.lightImageView.image = [UIImage imageNamed:@"lightOn.png"];
    cell.lightSwitch.on=YES;
   } 
       return cell;

}

now I need to keep reloading the URL data to init it

So I add

-(void)viewDidLoad{

timer = [NSTimer scheduledTimerWithTimeInterval:REFRESH_STATUS_TIME
                target:self
                 selector:@selector(timerFired:)
                 userInfo:nil
               repeats:YES];

}

and change the get URLrequest from (void)viewDidLoad to

- (void)timerFired:(NSTimer *)theTimer{
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.envolab.com/envotouch/ios_status_req_test.php"]
             cachePolicy:NSURLRequestUseProtocolCachePolicy
            timeoutInterval:60.0];

 NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
 NSLog(@"\n\nCONNECTION:   %@", theConnection);
 NSData *returnData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:nil error:nil]; 
 NSString *listFile = [[NSString alloc] initWithData:returnData encoding:NSASCIIStringEncoding];   
 self.plist = [listFile propertyList];
 NSLog(@"Timefired!!!!");
}

HERE IS THE PROBLEM ~

The TableView cell init seems didn't get any plist data from the timeFired

I check the console result,I can see there is a plist data get back every 3 sec (define REFRESH_STATUS_TIME = 3.0;)

What's Wrong When My program Reload Data pass to cell failed??

+1  A: 

I didn't see any lines with [self.tableView reloadData];. You have to call this to reload your table view data

vodkhang
I try it now !!
WebberLai
Oh...nothing happen ~
WebberLai
Does it call your - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {...} ?
vodkhang
Let me try a break point ,I just go home now
WebberLai
NO... I think it not call this function,because it's no stop at next line under the break point
WebberLai
also this one is not call "- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self.plist count];}"
WebberLai
If youor country is very late ,we can try figure out this question tomorrow ....sorry about that
WebberLai
are you sure that it calls your `[self.tableView reloadData];`
vodkhang
where should I put this code in [self.tableView reloadData]; ???
WebberLai
Oh....I think I figure out this problem...
WebberLai
A: 

First ,Change this !!!

timer = [NSTimer scheduledTimerWithTimeInterval:REFRESH_STATUS_TIME
                                                    target:self
                                                    selector:@selector(timerFired:)
                                                    userInfo:nil                
                                                    repeats:YES];

USE

[self performSelector:@selector(timerFired:) withObject:nil afterDelay:2.0];    

first time load this program it shows nothing on tableview ,after 2 seconds,It load the data . Than show everything from the plist file .

But.........It just run once ,How can I repeat it ???

WebberLai
......Just put timer back....It can repeat load data ,but sometimes runs delay...I think it's hardware device problem
WebberLai
this one should be a comment, or another question, 1 question should only ask 1 thing and don't put question to answer, you are messing things up:)
vodkhang
HA HA IT'S TRUE, THANKS ,IT'S ALREADY MIDNIGHT HERE ,GOOD NIGHT !
WebberLai