2017-02-21 75 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的数组边界例外,非常酷,它像一个专业版...为您的解决方案竖起大拇指。 –