重新加载tableview内容时出现EXC_BAD_ACCESS错误

时间:2011-10-13 16:24:47

标签: objective-c ios xcode uitableview

正确填充数组后,当我使用tableview reload再次调用它时,第一次(在clean之后)返回一个空值,然后执行后返回EXC_BAD_ACCES。试图提供所需的所有代码......感谢您的帮助!

在.h文件中

    @interface userViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {

        NSArray *appData;

        IBOutlet UIActivityIndicatorView *activityIndicator;
        IBOutlet UILabel *status;
        IBOutlet UITableView *mainTable;
        IBOutlet UIBarButtonItem *refresh; 
    }

    @property (nonatomic, retain) UIActivityIndicatorView *activityIndicator;
    @property (nonatomic, retain) UILabel *status;
    @property (nonatomic, retain) UITableView *mainTable;
    @property (nonatomic, retain) UIBarButtonItem *refresh;
    @property (nonatomic, retain) NSArray *appData;

- (IBAction) refresca: (id)sender;
<。>在.m文件中,当执行requestFinished函数时,appData已正确填充。

- (void)requestFinished:(ASIHTTPRequest *)request {
    NSLog(@"%@", [request responseString]);
    [activityIndicator stopAnimating];
    activityIndicator.hidden = YES;

    appData = [[request responseString] componentsSeparatedByString: @"#"]; 

    int x =0;

    while (x<[appData count] - 1)
    {
        NSLog(@"Data = %@",[appData objectAtIndex: x]);
        x = x+1;
    }

}

然而,在执行之后,当重新加载tableview时,appData似乎是空的并且之前的conted已被删除!!

2011-10-13 18:10:43.682 Nimbo[444:207] Data = <UITableViewCellContentView: 0x5c93dd0; frame = (0 0; 320 43); layer = <CALayer: 0x5c953b0>

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

        [tableView setFrame:CGRectMake(0, 44, 320, 412)];

        static NSString* cellIdentifier = @"cellIdentifier";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

        if(cell == nil)
        {
            cell = (UITableViewCell*) [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];

        }
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.textLabel.backgroundColor = [UIColor clearColor];


        int x = 0;

        if ([appData count] != 0)
           {
           NSLog(@"Data = %@",[appData objectAtIndex: 0]);
            }

        return cell;
    }

4 个答案:

答案 0 :(得分:2)

看起来您的实例变量appData未被保留。

编辑:正如其他人所说,使用该物业是最好的方式。这将保留您的新appData以及先前在那里分配的对象。

尝试更改:

    appData = [[request responseString] componentsSeparatedByString: @"#"]; 

为:

    self.appData = [[request responseString] componentsSeparatedByString: @"#"]; 

答案 1 :(得分:1)

你的问题主要在这里

appData = [[request responseString] componentsSeparatedByString: @"#"];

您正在将自动释放的对象分配给appData,这不是正确的方式,因为您没有取得所有权。您也没有释放以前持有的数据的所有权。您应该使用appData

的setter方法
self.appData = [[request responseString] componentsSeparatedByString: @"#"];

应该修复它。

答案 2 :(得分:0)

您应该使用属性(self.appData):

self.appData = [[request responseString] componentsSeparatedByString: @"#"];

答案 3 :(得分:0)

不知道这是不是问题,很可能不是......但是为什么在你不使用self.appData时定义一个属性?你是@synthesize appData吗?

当您使用属性时,调用self.property实际上会调用生成的setter和getter,并且这些属性在属性上指定。因此,如果您使用retain指定一个属性,但是为变量本身指定了一个方便返回的数组,则在事件循环之后,便捷数组将自动释放,并且因为您没有保留该数组,它将从内存中删除,丢失所有内容你曾经在那里,以及在你的情况下可能发生的事情。

相关问题