无法根据UIPickerView中的选择器更新UITableView数据

时间:2014-12-19 10:08:19

标签: objective-c uitableview nsmutablearray uipickerview

我正在处理一个应用程序,该应用程序在新部分显示新闻。要选择部分,我使用UIPickerView并在UITableView中显示所选部分的新闻。 后端我使用2个NSMutable数组,其中一个用于保存所有新闻,另一个是保存为部分选择过滤的新闻。 一切都很好,但是当我选择一个部分时,我的方法会过滤新闻并加载数组,但是tableview没有重新加载数据,当我重新加载tableview时,应用程序崩溃。 当我选择一个新的部分时,任何人都知道谁将重新加载tableview的所有单元格。

这是我的一些代码。

     - (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

1 个答案:

答案 0 :(得分:0)

首先,问题是你在ViewController.m文件中重新启动UITableView。如果将插座连接到接口文件,则无需重新启动tableview。

-(void)getNoticiasArrayForSeccion中,无需分配对象。您可以直接在ivar中获取一个对象,而无需分配它。

打电话 [noticiasTable reloadData]

在你的功能结束时

像这样的

-(void)getNoticiasArrayForSeccion

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

    [noticiasTable reloadData];
}