如何糾正這種錯誤在我的情況_NSArrayM objectAtIndex:]:指數8超出範圍[0..7]」
在這個屏幕截圖我有兩個下拉列表的問題和projects.If我的問題滾動下拉它顯示錯誤_NSArrayM objectAtIndex:]:索引8超越邊界[0 .. 7]'。儘管我動態獲取值。
我的JSON響應這樣的項目下拉:
{
"projects": [
{
"id": 8,
"name": "Andriod APP",
"identifier": "andriod-app",
"description": "",
"status": 1,
"is_public": true,
"created_on": "2015-06-29T11:54:23Z",
"updated_on": "2015-06-29T11:54:23Z"
},
],
"total_count": 8,
"offset": 0,
"limit": 25
}
my json response like this for issues drpdown
{
"issues": [
{
"id": 22,
"project": {
"id": 4,
"name": "Vitals"
},
"tracker": {
"id": 3,
"name": "Support"
},
"status": {
"id": 1,
"name": "New"
},
"priority": {
"id": 2,
"name": "Normal"
},
"author": {
"id": 1,
"name": "Redmine Admin"
},
"subject": "Lead page creation",
"description": "",
"start_date": "2015-06-25",
"done_ratio": 0,
"created_on": "2015-06-25T10:22:22Z",
"updated_on": "2015-06-25T10:22:22Z"
},
],
"total_count": 17,
"offset": 0,
"limit": 25
}
我的一段代碼:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UIButton *btnoutlet;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong,nonatomic) NSArray * data;
- (IBAction)btnAction:(id)sender;
@property (weak, nonatomic) IBOutlet UIButton *btnoutlet1;
@property (weak, nonatomic) IBOutlet UITableView *tableView1;
@property (strong,nonatomic) NSArray * data1;
- (IBAction)btnAction1:(id)sender;
@end
#import "ViewController.h"
@interface ViewController()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// to fetch and parse json response of projects dropdown
NSURLRequest * urlRequest = [NSURLRequest requestWithURL:[NSURLWithString:@"http:xxx/projects.json"]];
NSError * error = nil;
NSURLResponse * response = nil;
NSData * jsonSource = [NSURLConnection sendSynchronousRequest:urlRequest
returningResponse:&response error:&error];
id jsonObjects = [NSJSONSerialization JSONObjectWithData:
jsonSource options:NSJSONReadingMutableContainers error:nil];
if ([jsonObjects objectForKey:@"projects"] != [NSNull null]) {
self.data = [jsonObjects objectForKey:@"projects"];
NSLog(@" %@",self.data);
}
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.tableFooterView =[UIView new];
// to fetch and parse the json response of issues dropdown
NSURLRequest * urlRequest1 = [NSURLRequest requestWithURL: [NSURLURLWithString:@"http:xxx/redmine/issues.json"]];
NSError * error1 = nil;
NSURLResponse * response1 = nil;
NSData * jsonSource1 = [NSURLConnection sendSynchronousRequest:urlRequest1returningResponse:&response1error:&error1];
id jsonObjects1 = [NSJSONSerialization JSONObjectWithData:jsonSource1 options:NSJSONReadingMutableContainers error:nil];
if ([jsonObjects1 objectForKey:@"issues"] != [NSNull null]) {
self.data1 = [jsonObjects1 objectForKey:@"issues"];
NSLog(@" %@",self.data1);
}
self.tableView1.delegate = self;
self.tableView1.dataSource = self;
self.tableView1.tableFooterView =[UIView new];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(tableView == self.tableView)
{
return [self.data count];
}
if(tableView == self.tableView1)
{
return [self.data1 count];
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
NSDictionary * tmpdict = [self.data objectAtIndex:indexPath.row];
NSLog(@ " %@",tmpdict);
NSDictionary * tmpdict2 = [self.data1 objectAtIndex:indexPath.row];
NSLog(@ " %@",tmpdict2);
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
if(tableView == self.tableView){
cell.textLabel.font = [UIFont systemFontOfSize:12];
cell.backgroundColor = [UIColor greenColor];
cell.textLabel.text = [tmpdict objectForKey:@"name"];
NSLog(@ " %@",cell.textLabel.text);
}
else if(tableView == self.tableView1) {
cell.backgroundColor = [UIColor greenColor];
cell.textLabel.text = [tmpdict2 objectForKey:@"subject"];
NSLog(@ " %@",cell.textLabel.text);
}
// cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView==self.tableView) {
UITableViewCell * Cell = [self.tableView cellForRowAtIndexPath:indexPath];
[self.btnoutlet setTitle:Cell.textLabel.text forState:UIControlStateNormal];
self.tableView.hidden = YES;
}
if (tableView==self.tableView1) {
UITableViewCell * Cell = [self.tableView1 cellForRowAtIndexPath:indexPath];
[self.btnoutlet1 setTitle:Cell.textLabel.text forState:UIControlStateNormal];
self.tableView1.hidden = YES;
}
}
這裏可能是錯誤?在這個代碼或任何修改needed.Please建議一些想法。
這樣做:'NSDictionary * tmpdict = [self.data objectAtIndex:indexPath.row]; NSLog(@「%@」,tmpdict); NSDictionary * tmpdict2 = [self.data1 objectAtIndex:indexPath.row]; (只有在測試中並且在if(tableView == self.tableView或self.tableView1)的正確之一中)纔會生成NSLog(@「%@」,tmpdict2)。 – Larme
感謝您的回答 – catherine007