开发者

Iphone SDK:A bug or my logic error ,tableview can't scrolling down?

It's happened Intermittently

Check my code and compare the print result

maybe someone can know what's wrong with my code...

1.I load a plist data from an URL

    - (void)viewDidLoad {

    NSURLRequest *theRequest=[NSURLRequestrequestWithURL:[NSURLURLWithString:@"http://www.envolab.com/envotouch/ios_status_req_test.php"]
                                 cachePolicy:NSURLRequestUseProtocolCachePolicy
                                 timeoutInterval:60.0];

    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    NSData *returnData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:nil error:nil]; 
    NSString *listFile = [[NSString alloc] initWithData:returnData encoding:NSASCIIStringEncoding];   
    plist = [listFile propertyList];
}

than I print out the result to check the data is correct or not

the address is correct,I can see the plist file in Safari or IE

2.here is the result:

plist is :::::> 
(
        {
        category = Light;
        nodeID = 1;
        nodeName = "Living Room";
        nodeStatus = 0;
        nodeTrigger = 0;
        nodeType = "light_sw";
    },
        {
        category = Light;
        nodeID = 2;
      开发者_如何学Python  nodeName = Kitchen;
        nodeStatus = 0;
        nodeTrigger = 0;
        nodeType = "light_sw";
    },
        {
        category = Light;
        nodeID = 3;
        nodeName = Bedroom;
        nodeStatus = 1;
        nodeTrigger = 0;
        nodeType = "light_sw";
    }
)

OK,this result match the plist file I create

I want to display some items in tableview

3.next I define how many rows I need

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [plist count];
}

General , It will return 3

4.than I set the data I read from the URL ,and set into my tableView Cells

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

    static NSString *CellIdentifier = @"LightCell";

    LightCell0 *cell =(LightCell0 *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[LightCell0 alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }


    int i;
    for (i=0; i<[plist count]; i++) {

        //Get "nodeNAme"
        if(indexPath.row == i){
           cell.lightLocation.text =  [[[plist objectAtIndex:i] valueForKey: @"nodeName"]description];  
        //Determine each row's pic is Light On/Off from "nodeStatus"
        if ([[[plist objectAtIndex:i] valueForKey: @"nodeStatus"] intValue] == 0){
                cell.lightImageView.image = [UIImage imageNamed:@"lightOff.png"]; 
        }

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

        }
    }
    return cell;
}

Build & Run ->Succeeded no warning

I also got the result as I think,The image is match the switch status

If the nodeStatus is 0 = Light Off

other it will be Light on

and the number of rows is 3

BUTTTTTTTTTTTTT !!!!!

When I scrolling down the view,It will crash without any message

sometimes it won't happen,just like when you at the top of the tableview

you try to "scrolling up",it will scrolling down the view automatically

when I try to scrolling down ,I think it will back automatically

but it doesn't !?!

I print the row I create,It display like this

Now you see me tableView Row Count
TOTAL PLIST ROW COUNT IS  = 3
Now you see me Load Data 0
Now you see me Load Data 1
Now you see me Load Data 2

It looks fine,But I notice one thing unusual

every time I scrolling down,It will randomly appear one more string

Now you see me Load Data 0
Now you see me Load Data 1
Now you see me Load Data 2
Now you see me Load Data 0,1,2(<-This line appear randomly with the number)

In console mode you only see "Debugging terminated"

But some times there will be an error message :

  -[__NSCFTimer count]: unrecognized selector sent to instance 0xbb00af0
    2010-09-07 00:45:04.168 EnvoTouchDemo[3803:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 
'-[__NSCFTimer count]: unrecognized selector sent to instance 0xbb00af0'
    terminate called after throwing an instance of 'NSException'

I remove the program in the simulator and rebuild again it's looking fine

but run after few times,It crash again...and with reason ???

I think my logic is right....but the crash is really annoying me....

thanks reading my problems

hope someone can figure out this problem !


You forget to retain your plist instance variable.

If it is a retaining property then you can use:

self.plist = [listFile propertyList];

If it is just an instance variable then you should use:

plist = [[listFile propertyList] retain];

That will probably fix your errors.

Also note that theConnection is not released. So you are leaking memory every time that view is loaded.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜