[英]EXC_BAD_ACCES error when reload tableview content
正確填充數組后,當我使用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似乎為空,並且先前的條件已刪除!!
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;
}
看起來您的實例變量appData
未保留。
編輯:正如其他人所說,使用該物業是最好的方式。 這將保留您的新appData
以及之前在那里分配的釋放對象。
嘗試改變:
appData = [[request responseString] componentsSeparatedByString: @"#"];
至:
self.appData = [[request responseString] componentsSeparatedByString: @"#"];
你的問題主要在於此
appData = [[request responseString] componentsSeparatedByString: @"#"];
您正在為appData
分配一個自動釋放的對象,這不是正確的方法,因為您沒有獲得所有權。 您也沒有釋放以前持有的數據的所有權。 您應該使用appData
的setter方法。
self.appData = [[request responseString] componentsSeparatedByString: @"#"];
應該解決它。
你應該使用property( self.appData
):
self.appData = [[request responseString] componentsSeparatedByString: @"#"];
不知道這是不是問題,很可能不是......但是為什么在你不使用self.appData時定義一個屬性? 您是@synthesize appData嗎?
當您使用屬性時,調用self.property實際上會調用生成的setter和getter,並且這些屬性在屬性上指定。 因此,如果您使用retain指定一個屬性,但是為變量本身指定了一個方便返回的數組,則在事件循環之后,便捷數組將自動釋放,並且因為您沒有保留該數組,它將從內存中刪除,丟失所有內容你曾經在那里,你的情況可能發生了什么。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.