Iphone SDK:一个bug或我的逻辑错误,tableview无法向下滚动?

时间:2010-09-06 16:55:08

标签: iphone scroll plist tableview

发生了间歇性的

检查我的代码并比较打印结果

也许有人可以知道我的代码有什么问题......

1.我从URL

加载plist数据
    - (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];
}

比我打印出结果以检查数据是否正确

地址正确,我可以在Safari或IE中看到plist文件

2.结果如下:

plist is :::::> 
(
        {
        category = Light;
        nodeID = 1;
        nodeName = "Living Room";
        nodeStatus = 0;
        nodeTrigger = 0;
        nodeType = "light_sw";
    },
        {
        category = Light;
        nodeID = 2;
        nodeName = Kitchen;
        nodeStatus = 0;
        nodeTrigger = 0;
        nodeType = "light_sw";
    },
        {
        category = Light;
        nodeID = 3;
        nodeName = Bedroom;
        nodeStatus = 1;
        nodeTrigger = 0;
        nodeType = "light_sw";
    }
)

好的,这个结果与我创建的plist文件匹配

我想在tableview中显示一些项目

3.next我定义了我需要多少行

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

一般情况下,它将返回3

4.然后我设置从URL读取的数据,并设置到我的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&amp;运行 - &gt;成功没有警告

我也想到了结果,图像与开关状态匹配

如果nodeStatus为0 = Light Off

其他它将是Light on

,行数为3

BUTTTTTTTTTTT !!!!!

当我向下滚动视图时,它会在没有任何消息的情况下崩溃

有时它不会发生,就像你在桌面视图顶部的那样

您尝试“向上​​滚动”,它会自动向下滚动视图

当我尝试向下滚动时,我认为它会自动返回

但它没有!?!

我打印我创建的行,它显示如下

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

看起来很好,但我注意到一件事不寻常

每次我向下滚动时,它会随机出现一个字符串

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)

在控制台模式下,您只能看到“调试已终止”

但有时会出现错误信息:

  -[__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'

我在模拟器中删除程序并重新重建它看起来很好

但是经过几次运行,它再次崩溃......并且有理由???

我认为我的逻辑是正确的......但崩溃真的让我烦恼....

感谢阅读我的问题

希望有人能解决这个问题!

1 个答案:

答案 0 :(得分:0)

您忘记保留plist实例变量。

如果是保留财产,则可以使用:

self.plist = [listFile propertyList];

如果它只是一个实例变量,那么你应该使用:

plist = [[listFile propertyList] retain];

这可能会修复你的错误。

另请注意,theConnection未发布。因此,每次加载视图时都会泄漏内存。