[英]error when add uitableviewcontroller as subview in uiviewcontroller
我正在做一個簡單的應用程序,它從json服務器查看項目
我在一個UIViewController中使用了UISegment來添加不同的子視圖
- (IBAction)segmentSwitch:(id)sender {
UISegmentedControl *segmentedControl = (UISegmentedControl *) sender;
NSInteger selectedSegment = segmentedControl.selectedSegmentIndex;
if (selectedSegment == 0) {
instantiateViewControllerWithIdentifier:@"prestige"] animated:NO];
UIViewController *subController = [self.storyboard instantiateViewControllerWithIdentifier:@"prestige"];
[mainView addSubview:subController.view];
}
else if(selectedSegment == 1)
{
instantiateViewControllerWithIdentifier:@"latest"]];
//[self setView: [self.storyboard instantiateViewControllerWithIdentifier:@"latest"]];
//UIViewController *subController = [self.storyboard instantiateViewControllerWithIdentifier:@"latest"];
//[mainView addSubview:subController.view];
UITableViewController *tableVC =[self.storyboard instantiateViewControllerWithIdentifier:@"latest"];
[self.view addSubview:tableVC.tableView];
}
else if (selectedSegment == 2)
{
instantiateViewControllerWithIdentifier:@"contactUs"]];
UIViewController *subController = [self.storyboard instantiateViewControllerWithIdentifier:@"contactUs"];
[mainView addSubview:subController.view];
}
}
當我選擇Segment 2來查看uitableviewcontroller作為子視圖x代碼時,我的錯誤就出現了以下錯誤
exc_bad_access(代碼= 1地址= 0x110ff210在線NSDictionary * latests = [latestArray objectAtIndex:indexPath.row];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"latestCell1";
latestCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSDictionary *latests = [latestArray objectAtIndex:indexPath.row];
NSString *latest_name_En = [latests objectForKey:@"title_en"];
NSString *logo1 = [latests objectForKey:@"logo"];
NSString *img1 = [latests objectForKey:@"img1"];
NSData *latestsImg1 = [NSData dataWithContentsOfURL:[NSURL URLWithString:img1]];
NSData *latestsLogo1 = [NSData dataWithContentsOfURL:[NSURL URLWithString:logo1]];
cell.latest_name_en1.text = latest_name_En;
dispatch_async(dispatch_get_main_queue(), ^{
cell.latest_img1.image = [UIImage imageWithData:latestsImg1];
cell.latest_logo1.image = [UIImage imageWithData:latestsLogo1];
});
return cell;
}
我確定UITableViewController工作正常,因為我試圖單獨運行它並且它也可以在沒有自定義單元格的情況下嘗試它並且它也可以工作但是使用custon cell作為子視圖它會給出上面的錯誤。
謝謝您的幫助
我終於弄明白我用過了
[self addChildViewController:subController];
[mainView addSubview:subController.view];
將tableview作為子視圖添加到viewcontroller
它起作用了
latestArray尚未正確初始化。
每當您獲得EXEC_BAD_ACCESS時,請通過Zombies Instrument運行您的代碼。 這將指向任何有問題的代碼。
初始化對象latestsImg1和latestsLogo1時不會被保留。 因此,當運行dispatch_async中執行的異步代碼塊時,這些值可能已經釋放並且不再有效。 因此,當異步塊執行時,這兩個指針被指向遺忘。 我建議不要通過GrandCentral調度執行這兩個賦值,或者如果必須異步執行它們,則在創建后保留它們並在異步塊中釋放它們。
就像是:
NSData * latestsImg1 = [NSData dataWithContentsOfURL:[NSURL URLWithString:img1]]; [latestsImg1保留]; NSData * latestsLogo1 = [NSData dataWithContentsOfURL:[NSURL URLWithString:logo1]];
[latestsLogo1保留]; cell.latest_name_en1.text = latest_name_En;
dispatch_async(dispatch_get_main_queue(), ^{
cell.latest_img1.image = [UIImage imageWithData:latestsImg1];
cell.latest_logo1.image = [UIImage imageWithData:latestsLogo1];
[latestsImg1 release];
[latestLogo1 release];
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.