我正在一个应用程序显示新消息在新的部分。要选择部分,我使用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
从哪个数组你所得到的数据,并显示其进入的tableview? – 2014-12-19 10:22:25
您需要调用tableview的'reloadData'方法来告诉表重新从数组重新加载数据。 – 2014-12-19 10:25:40
发布您收到的错误。它对你的问题至关重要 – 2014-12-19 10:25:51