>我正在开发一个应用程序,该应用程序在新部分显示新闻。要选择部分,我使用UIPickerView并在UITableView中显示所选部分的新闻。后端我使用2个NSMutable数组,其中一个用于保存所有新闻,另一个用于保存过滤的新闻以进行部分选择。一切都很好,但是当我选择一个部分时,我的方法会过滤新闻并在数组中加载,但表视图没有重新加载数据,当我重新加载表视图时,应用程序崩溃了。任何人都知道当我选择新的部分时,谁应该重新加载表视图的所有单元格。
这是我的一些代码。
- (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;
}
https://i.stack.imgur.com/vS8c8.png https://i.stack.imgur.com/a8pIl.png
首先,问题是您在ViewController.m文件中重新启动UITableView。如果将插座连接到接口文件,则无需重新启动表视图。
在-(void)getNoticiasArrayForSeccion
中,无需分配对象。您可以直接在 ivar 中获取对象,而无需分配它。
只需致电 [noticiasTable reloadData]
在函数结束时
-(void)getNoticiasArrayForSeccion
像这样:
-(void)getNoticiasArrayForSeccion
{
.... // loading data according to the filter selected
[noticiasTable reloadData];
}