2014-12-19 45 views
0

我正在一個應用程序顯示新消息在新的部分。要選擇部分,我使用UIPickerView並在UITableView中顯示所選部分的消息。 後端我使用2個NSMutable數組,其中一個用於保存所有新聞,另一個用於保存篩選出的部分選擇的新聞。 一切都很好,但是當我選擇一個節時,我的方法會過濾新聞並加載數組,但是tableview沒有重新加載數據,而且當我重新加載tableview時應用程序崩潰。 當我選擇一個新部分時,任何人都知道是誰重新加載tableview的所有單元格。不能更新UITableView數據取決於從UIPickerView選擇器

這是我的一些代碼。

 - (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? – 2014-12-19 10:22:25

+0

您需要調用tableview的'reloadData'方法來告訴表重新從數組重新加載數據。 – 2014-12-19 10:25:40

+0

發佈您收到的錯誤。它對你的問題至關重要 – 2014-12-19 10:25:51

回答

0

所有的問題首先是你在ViewController.m文件重新開始的UITableView。如果將插座連接到接口文件,則不需要重新啓動tableview。

-(void)getNoticiasArrayForSeccion中不需要分配對象。您可以直接在伊娃中直接獲取對象而無需分配它。

只需撥打 [noticiasTable reloadData]

在你的函數

-(void)getNoticiasArrayForSeccion這樣的結尾:

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

    [noticiasTable reloadData]; 
} 
+0

我已經做到了但沒有重新加載UITableView – KurroCantos 2014-12-19 10:48:21

+0

@KroroCantos的單元格,如果您與我共享一些代碼片段,這將非常有幫助。 – 2014-12-19 12:04:35

+0

我不知道該怎麼做,但我認爲這是最好的方法。我將該項目上傳到GitHub:https://github.com/KurroCantos/newsPickerExample.git – KurroCantos 2014-12-19 12:14:23