2014-12-19 2 views
0

새 섹션에서 뉴스가 흐려지는 앱을 제작하고 있습니다. 섹션을 선택하려면 UIPickerView를 사용하고 선택한 섹션의 뉴스를 UITableView에 표시하십시오. 백엔드 2 NSMutable 배열을 사용합니다. 그 중 하나는 모든 뉴스를 저장하는 것이며, 다른 하나는 섹션 선택을 위해 필터링 된 뉴스를 저장하는 것입니다. 모든 것이 정상이지만 섹션을 선택하면 메서드가 뉴스를 필터링하고 배열에서로드하지만 tableview가 데이터를 다시로드하지 않았고 tableview를 다시로드하면 앱이 다운됩니다. 누구든지 새 분리를 선택할 때 테이블 뷰의 모든 셀을 다시로드 할 사람을 알고 있습니다.UITickerView의 선택기에 따라 UITableView 데이터를 업데이트합니다.

이것은 내 코드의 일부입니다.

 - (void)viewDidLoad { 
     [super viewDidLoad]; 

     arraySecciones = @[@"Todas",@"Agua Potable",@"Agua Residual",@"Centro I+D+I",@"Cooperacion",@"Residuos"]; 
     seccionesPicker = [[UIPickerView alloc] init]; 

     NSDate *fecha = [[NSDate alloc] initWithTimeIntervalSince1970:1431231]; 

     arrayNoticias = [[NSMutableArray alloc]init]; 
     [arrayNoticias addObject:[[Noticia alloc] initWithTitle:@"Noticia 1" body:@"Cuerpo 1" section:@"Agua Potable" date:fecha]]; 
     [arrayNoticias addObject:[[Noticia alloc] initWithTitle:@"Noticia 2" body:@"Cuerpo 2" section:@"Residuos" date:fecha]]; 
     [arrayNoticias addObject:[[Noticia alloc] initWithTitle:@"Noticia 3" body:@"Cuerpo 3" section:@"Cooperacion" date:fecha]]; 
     [arrayNoticias addObject:[[Noticia alloc] initWithTitle:@"Noticia 4" body:@"Cuerpo 4" section:@"Cooperacion" date:fecha]]; 
     [arrayNoticias addObject:[[Noticia alloc] initWithTitle:@"Noticia 5" body:@"Cuerpo 5" section:@"Cooperacion" date:fecha]]; 
     [arrayNoticias addObject:[[Noticia alloc] initWithTitle:@"Noticia 6" body:@"Cuerpo 6" section:@"Agua Potable" date:fecha]]; 
     [arrayNoticias addObject:[[Noticia alloc] initWithTitle:@"Noticia 7" body:@"Cuerpo 7" section:@"Agua Residuale" date:fecha]]; 
     [arrayNoticias addObject:[[Noticia alloc] initWithTitle:@"Noticia 8" body:@"Cuerpo 8" section:@"Agua Potable" date:fecha]]; 
     [arrayNoticias addObject:[[Noticia alloc] initWithTitle:@"Noticia 9" body:@"Cuerpo 9" section:@"Centro I+D+I" date:fecha]]; 

     seccionSelecccionada = @"Todas"; 
     arrayNoticiasFiltrado = [[NSMutableArray alloc]init]; 
     [self getNoticiasArrayForSeccion]; 

    } 

     - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 
     { 
      NSLog(@"Fila Seleccionada %ld",(long)row); 

      switch (row) { 
      case 0: 
        self.seccionSelecccionada = @"Todas"; 
        break; 
       case 1: 
        self.seccionSelecccionada = @"Agua Potable"; 
        break; 
       case 2: 
        self.seccionSelecccionada = @"Agua Residual"; 
        break; 
       case 3: 
        self.seccionSelecccionada = @"Centro I+D+I"; 
        break; 
       case 4: 
        self.seccionSelecccionada = @"Cooperacion"; 
        break; 
       case 5: 
        self.seccionSelecccionada = @"Residuos"; 
        break; 
      } 

      seccionLabel.text = seccionSelecccionada; 
      [self getNoticiasArrayForSeccion]; 
    } 

    -(void)getNoticiasArrayForSeccion 
    { 
     [self.arrayNoticiasFiltrado removeAllObjects]; 

     for(int x=0; x<arrayNoticias.count; x++) 
     { 
      Noticia *noticiaAux = [arrayNoticias objectAtIndex:x]; 

      if ([self.seccionSelecccionada isEqualToString:@"Todas"]) 

       [arrayNoticiasFiltrado addObject:noticiaAux]; 

      else if([noticiaAux.seccion isEqualToString:self.seccionSelecccionada]) 
        [arrayNoticiasFiltrado addObject:noticiaAux]; 
     } 
    } 

    -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     Noticia *noticiaAux = [[Noticia alloc] init]; 

     noticiaAux = [arrayNoticiasFiltrado objectAtIndex:indexPath.row ]; 
     NoticiasTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"celdaNoticias"]; 

     if(!cell) 
      cell =[[NoticiasTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"celdaNoticias"]; 

     cell.titular.text = noticiaAux.titular; 
     cell.seccion.text = noticiaAux.seccion; 

     NSLog(@"Pintando : %@",cell.titular.text); 
     return cell; 

}

http://i.stack.imgur.com/vS8c8.png http://i.stack.imgur.com/a8pIl.png 모든 문제의

+0

하는 배열에서 사용자가 데이터를 가져 오는하고있는 tableview로 표시 :

그냥 기능이 같은

-(void)getNoticiasArrayForSeccion의 끝에 [noticiasTable reloadData]

전화? –

+0

어레이에서 데이터를 다시로드하도록 테이블에 알리기 위해 tableview의'reloadData' 메소드를 호출해야합니다. –

+0

오류를 게시하십시오. 그 질문에 매우 중요합니다 –

답변

0

먼저 당신은 ViewController.m 파일에 jQuery과 재개 한되어있다. 콘센트를 인터페이스 파일에 연결하면 tableview를 다시 시작할 필요가 없습니다.

-(void)getNoticiasArrayForSeccion에는 개체를 할당 할 필요가 없습니다. 단순히 오브젝트를 할당하지 않고 직접 ivar로 가져올 수 있습니다.

-(void)getNoticiasArrayForSeccion 
{ 
    .... // loading data according to the filter selected 

    [noticiasTable reloadData]; 
} 
+0

이미 수행했습니다. 하지만 UITableView – KurroCantos

+0

@ KurroCantos의 셀을 다시로드하지 마십시오. 나와 함께 코드 스 니펫을 공유하면 정말 도움이 될 것입니다. –

+0

나는 그것을 어떻게하는지 모른다. 그러나 나는 이것이 최선의 방법이라고 생각한다. 프로젝트를 GitHub에 업로드했습니다. https://github.com/KurroCantos/newsPickerExample.git – KurroCantos

관련 문제