正确填充数组后,当我使用 tableview 重新加载再次调用它时,第一次(清理后)返回一个空值,然后执行返回 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;
}
最佳答案
看起来像您的实例变量 appData
不保留。
编辑:正如其他人所说,使用该属性是最好的方法。这将保留您的新 appData
以及之前分配的释放对象。
尝试改变:
appData = [[request responseString] componentsSeparatedByString: @"#"];
到:
self.appData = [[request responseString] componentsSeparatedByString: @"#"];
关于objective-c - 重新加载 TableView 内容时出现 EXC_BAD_ACCES 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7757237/