2013-05-23 3 views
0

이제 NSRangeException이 무엇인지 압니다. 내가 얻지 못하는 것은 왜 내 코드를 처음 실행했을 때 괜찮습니까?하지만 동일한 코드 (및 변수)가 두 번째로 설정되면 언제 실패할까요? 코드를 밟아도 첫 번째 실행과 다른 점을 볼 수 없습니다.Xcode NSRangeException 두 번째 코드 실행

전체 코드는 내 PHP API (기본적으로 클라이언트 정보, 예약 된 단위, 날짜 등)에서 JSON 값 집합을 가져옵니다.이 코드는 UITableView에 표시하기 만하면 테이블을 이 예약 된 날짜 (애플은 아마도 자신의 jQuery과 확장, 내 의견으로는이 작업을 수행하는 더 좋은 방법이해야합니까?)

내 .H

@interface LTBViewController : UIViewController { 
IBOutlet UITableView *mainTableView; 

NSArray *tickets; //for storing our serialized JSON 
NSMutableData *data; //for appending our JSON as we're getting it 
NSString *token; //for security authentication with our custom API 


NSMutableArray *days; //for storing any days with bookings retrieved from tickets 
NSMutableDictionary *groupedTickets; //for storing tickets to correlate to days 
NSArray *filteredArray; //for our finalized output 
} 
@end 

내하는 .m의 대부분 (중요한 부분)

- (void)viewDidAppear:(BOOL)animated 
{ 
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; 

    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://myurl.com/api/api.php?api=%@&function=%@", token, @"active"]]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
    [NSURLConnection connectionWithRequest:request delegate:self]; 



    //[[NSURLConnection alloc] initWithRequest:request delegate:self]; old way of doing it 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    //initialize from our settings file and make sure we're authenticated 
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    token = [defaults objectForKey:@"api_token"]; 
    NSLog(@"%@",token); 

    //initialize our sorting arrays 
    days = [[NSMutableArray alloc] init]; 
    groupedTickets = [[NSMutableDictionary alloc] init]; 
    filteredArray = [[NSArray alloc] init]; 
} 


- (void)groupTable { 


    NSUInteger count = 0; 
    for (LTBViewController *ticket in tickets) 
    { 

     NSString *date = [[tickets objectAtIndex:count] objectForKey:@"dateIn"]; 
     if (![days containsObject:date]) 
     { 
      [days addObject:date]; 
      [groupedTickets setObject:[NSMutableArray arrayWithObject:ticket] forKey:date]; 
     } 
     else 
     { 
      [((NSMutableArray*)[groupedTickets objectForKey:date]) addObject:ticket]; 
     } 
     count +=1; 

    } 
    count = 0; 
    [mainTableView reloadData]; 
} 


//for when we get a response 
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    data = [[NSMutableData alloc] init]; 
} 


//for getting data 
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)thedata 
{ 
    [data appendData:(thedata)]; 

} 


//for when connection finished 
- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    tickets = [NSJSONSerialization JSONObjectWithData:data options:NO error:nil]; 

    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 

    [self groupTable]; //starts our grouping methods 
} 


//for when connection fails 
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
    UIAlertView *errorView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Could not connect to server" delegate:nil cancelButtonTitle:@"dismiss" otherButtonTitles:nil, nil]; 
    [errorView show]; 
} 

//overriding the "delete" text for our UITableView 
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath { 
    return @"Close Ticket"; 
} 

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 
    NSString *date = [days objectAtIndex:section]; 
    return date; 
} 


- (int)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return [days count]; 
} 


- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    NSString *date = [days objectAtIndex:section]; 
    NSLog(@"%@", date); 
    return [[groupedTickets objectForKey:date] count]; 
} 


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

    tableView.layer.cornerRadius = 5.0; //rounds our table corners 

    if(cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"MainCell"]; 
    } 

//I'm sure theres a better way to do this but it works for now 

    NSString *date = [days objectAtIndex:[indexPath section]]; 

    NSPredicate *search = [NSPredicate predicateWithFormat:@"dateIn == %@", date]; 

    filteredArray = [tickets filteredArrayUsingPredicate:search]; 
,210

//이 바로 여기에 항목이 다른 뷰 (확실히 작동) 컨트롤러하지만 다시 버튼에서 눌렀을 때 그것이로 이동을 클릭하면 인덱스 1

cell.textLabel.text = [NSString stringWithFormat:@"%@",[[filteredArray objectAtIndex:[indexPath row]] objectForKey:@"ticketnumber"] ]; 

    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@", [[filteredArray objectAtIndex:[indexPath row]] objectForKey:@"customerName"]]; 

    return cell; 
} 

에서를 통해 두 번 실패 할 것입니다 이 코드는 실패합니다. 나는 그 이상의 눈을 보며 내 실수를 찾도록 도와주는 추가 세트를 감상하게 될 것입니다. (또는 이것을하는 더 좋은 방법)

고마워!

답변

0

알 수 있습니다. 내 NSMutableArray 일 및 NSMutableDictionary groupedTickets 값을 다시 할당하면 그냥 이전 값을 덮어 쓸 것이라고 생각했다.

내가보기를 해제하고 화면이 사라지고 이제는 작동합니다.

그래도 더 좋은 방법이 있다면 학습하는 데 관심이 있습니다.

관련 문제