2017-02-21 76 views
0

首先我的問題是嚴重&不適合那些只願意分配負面反饋或只是在這裏編輯&獲得獎勵的目的。如何在prepareForSegue方法中訪問不同屬性或選定對象的Array對象中的屬性

我的問題很難傳達,但我正在盡我所能傳達完整的問題。 其實我在後端有兩個表格視圖和兩個Json文件,還有一個AVPlayerViewController。第一alll firstTableViewController的示出了其中包含的關注JSON數據

[{ 
    "id": "0", 
    "name": "Religion" 
}, { 
    "id": "1", 
    "name": "News" 
}, { 
    "id": "2", 
    "name": "Sport" 
}, { 
    "id": "3", 
    "name": "Doc" 
}, { 
    "id": "4", 
    "name": "Kids" 
}, { 
    "id": "5", 
    "name": "Music" 
}, { 
    "id": "6", 
    "name": "Movie" 
}, { 
    "id": "7", 
    "name": "Entr" 
}, { 
    "id": "8", 
    "name": "Cooking" 
}, { 
    "id": "9", 
    "name": "All" 
}] 

在SecondViewController其中提取second Json file含有不同鏈接第一JSON文件沿與上述第一JSON文件提到的類別。 這兩個Json文件的數據在我的應用程序中成功檢索。

當我在它帶給我的secondTableViewController與含有特定類別(命名爲category_name在第二JSON文件)所選類別的所有對象的title財產我FirstTableViewController的任意單元格(包含特定類別)點擊。 到此爲止,一切都非常酷。 ,但是當我點擊任何標題時會出現問題,它應該將我帶到AVPlayerViewController並播放相關的直播鏈接。但它給我下面的錯誤,

*終止應用程序由於未捕獲的異常 'NSRangeException',原因是: '* - [NSArrayM objectAtIndex:]:指數2097174超越界限 [0 .. 32]' ***第一擲調用堆棧:(0的CoreFoundation 0x0000000104b0234b __exceptionPreprocess + 171 1 libobjc.A.dylib
0x000000010456321e objc_exception_throw + 48 2的CoreFoundation
0x0000000104a33f1b - [__ NSArrayM objectAtIndex:] + 203 3 AlbTVRadio 0x00000001022bcae1 - [第二prepareForSegue:發件人:] + 321 4 UIKit 0x0000000102bbef5f - [UIStoryboardSegueTemplate _performWithDestinationViewController:發送方:] + 353 5的UIKit 0x0000000102bbedcd - [UIStoryboardSegueTemplate _perform:] + 82 6
的UIKit 0x0000000102bbf08f - [UIStoryboardSegueTemplate執行:] + 156 7的UIKit 0x000000010253b52a - [UITableView的 _selectRowAtIndexPath:動畫:的scrollPosition :notifyDelegate:] + 1769 8的UIKit 0x000000010253b78d - [UITableView的_userSelectRowAtPendingSelectionIndexPath:] + 330個9的UIKit 0x00000001023f12eb _runAfterCACommitDeferredBlocks + 320 10的UIKit 0x00000001023ddf6f _cleanUpAfterCAFlushAndRunDeferredBlocks + 566 11 的UIKit 0x000000010240f3da _afterCACommitHandler + 176 12 CoreFou ndation 0x0000000104aa6e17 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION
+ 23 13的CoreFoundation 0x0000000104aa6d87 __CFRunLoopDoObservers + 391 14的CoreFoundation 0x0000000104a8bb9e __CFRunLoopRun + 1198 15的CoreFoundation
0x0000000104a8b494 CFRunLoopRunSpecific + 420個16 GraphicsServices
0x0000000106784a6f GSEventRunModal + 161 17的UIKit
0x00000001023e4964 UIApplicationMain + 159 18 AlbTVRadio
0x00000001022cd65f主+ 111 19 libdyld.dylib
0x000000010a00968d start + 1 20 ???
0x0000000000000001 0x0 + 1)libC++ abi。dylib:與類型NSException的 未捕獲的異常終止

我認爲問題出在SEGUE代碼,未採摘準確ChannelObjectObject.livestream

這裏是我的第二個的viewController代碼。其中包含第二個json文件。所以請儘可能糾正此代碼。

#import "Second.h" 
#import "Channel.h" 
#import "UIImageView+AFNetworking.h" 
#import "Type.h" 
#define getDataUrl @"https://creativestudy.000webhostapp.com/webservices/test.json" 

@interface Second() 

@end 

@implementation Second 
@synthesize tvArray,currentObject; 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    [self retrieveData]; 



    // Uncomment the following line to preserve selection between presentations. 
    // self.clearsSelectionOnViewWillAppear = NO; 

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    // self.navigationItem.rightBarButtonItem = self.editButtonItem; 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 

    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

    return tvArray.count; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cells" forIndexPath:indexPath]; 

    Channel * ChannelObject; 
    ChannelObject = [tvArray objectAtIndex:indexPath.row]; 

    cell.textLabel.text = ChannelObject.channelTitle; 
    cell.detailTextLabel.text = ChannelObject.channelLivestream; 
    cell.detailTextLabel.hidden = YES; 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

    NSURL *url = [NSURL URLWithString:ChannelObject.channelImage]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
    UIImage *placeholderImage = [UIImage imageNamed:@"placeholder"]; 
    __weak UITableViewCell *weakCell = cell; 
    [cell.imageView setImageWithURLRequest:request placeholderImage:placeholderImage success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) { 
     weakCell.imageView.image = image; 
     [weakCell setNeedsLayout]; 
    } failure:nil]; 



    // Configure the cell... 

    return cell; 
} 
-(void) getType:(id)typeObject{ 

    currentObject = typeObject; 

} 

/* 
// Override to support conditional editing of the table view. 
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Return NO if you do not want the specified item to be editable. 
    return YES; 
} 
*/ 

/* 
// Override to support editing the table view. 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     // Delete the row from the data source 
     [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } else if (editingStyle == UITableViewCellEditingStyleInsert) { 
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 
    } 
} 
*/ 

/* 
// Override to support rearranging the table view. 
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { 
} 
*/ 

/* 
// Override to support conditional rearranging of the table view. 
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Return NO if you do not want the item to be re-orderable. 
    return YES; 
} 
*/ 


// In a storyboard-based application, you will often want to do a little preparation before navigation 
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
     if ([segue.identifier isEqualToString:@"playVideo"]) { 

      AVPlayerViewController *playerViewController = [segue destinationViewController]; 

      NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 
      Channel * ChannelObject; 
      ChannelObject = [self.tvArray objectAtIndex:(int)indexPath]; 
      NSURL *liveUrl = [NSURL URLWithString:ChannelObject.channelLivestream]; 
      playerViewController.player = [AVPlayer playerWithURL:liveUrl]; 
     [playerViewController.player play]; 

      } 

} 


-(void) retrieveData{ 
    NSURL * url = [NSURL URLWithString:getDataUrl]; 
    NSData * data = [NSData dataWithContentsOfURL:url]; 
    NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]; 

    tvArray = [[NSMutableArray alloc] init]; 

    for (int i = 0; i < jsonArray.count; i ++) 
    { 
     NSDictionary *aCategoryJSON = [jsonArray objectAtIndex:i]; 
     NSString *cId = [aCategoryJSON objectForKey:@"id"]; 
     NSString *cTitle = [aCategoryJSON objectForKey:@"title"]; 
     NSString *cCategory = [aCategoryJSON objectForKey:@"category_name"]; 
     NSString *cImage = [aCategoryJSON objectForKey:@"image"]; 
     NSString *cLivestream = [aCategoryJSON objectForKey:@"livestream"]; 
     NSString *cWebsite = [aCategoryJSON objectForKey:@"Website"]; 
     if (cCategory == currentObject.typeName){ 
     [tvArray addObject:[[Channel alloc] initWithChannelId:cId andChannelTitle:cTitle andChannelCategory:cCategory andChannelImage:cImage andChannelLivestream:cLivestream andChannelWebsite:cWebsite]]; 
     } 
     else if ([currentObject.typeName isEqualToString: @"All"]){ 
     [tvArray addObject:[[Channel alloc] initWithChannelId:cId andChannelTitle:cTitle andChannelCategory:cCategory andChannelImage:cImage andChannelLivestream:cLivestream andChannelWebsite:cWebsite]]; 
     } 

    } 
    [self.tableView reloadData]; 
} 

@end 
+0

請張貼完整的異常調用堆棧(帶有_Terminating應用程序由於未捕獲例外_...) –

+0

由於您使用的數組超出了最大限制,因此您正面臨問題。也許你應該檢查你的retrieveData函數。 – dreamBegin

+0

放置一個異常斷點並查看發生異常的行。 @Hina – iPeter

回答

1

你的問題是在這裏:

ChannelObject = [self.tvArray objectAtIndex:(int)indexPath]; 

你投中的對象(NSIndexPath的實例)以一個int和Objective-C還讓你,因爲它是基於C,儘管這是沒有意義的。

indexPath是一個指向內存中的對象,所以當你將它轉換爲int你解釋爲一個整數,這就是爲什麼你看到的嘗試陣列中訪問元素2097174內存地址。想必你把劇組添加到int,因爲編譯器告訴你,你不能在該函數中使用indexPath。

你需要做的是用indexPath.row來從NSIndexPath實例行整數。您還可以使用更現代的數組訪問語法和小寫字母啓動變量(大寫字母是班):

Channel *channelObject = self.tvArray[indexPath.row]; 
+0

感謝您的回答,讓我試試&測試你說的和建議在你的回答中, 但是如果你試圖注意到,我已經指出了選定索引的行在'NSIndexPath * indexPath = [self.tableView indexPathForSelectedRow ];'線,它正好在行的兩行之上'ChannelObject = [self.tvArray objectAtIndex:(int)indexPath];'在這個討論中。但是,感謝您的建議,讓我嘗試一下,希望能夠帶來任何應有的結果。 。 。 –

+0

''indexPathForSelectedRow''顧名思義就是給你一個'NSIndexPath'。然後您需要使用'indexPath.row'來獲取選定的行。你試圖直接使用'indexPath'作爲數組索引,這就是爲什麼你必須將它轉換爲int的原因,以及爲什麼你會得到一個瘋狂的索引 – Paulw11

+0

@ Paul11的數組邊界例外,非常酷,它像一個專業版...爲您的解決方案豎起大拇指。 –