【发布时间】:2014-12-19 10:08:18
【问题描述】:
我正在开发一个应用程序,该应用程序根据新的部分显示新闻。要选择部分,我使用 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
【问题讨论】:
-
您从哪个数组获取数据并将其显示到 tableview 中?
-
您需要调用tableview 的
reloadData方法来告诉表格重新从数组中重新加载数据。 -
发布您收到的错误。它对您的问题至关重要
-
我从 arrayNoticias 获取数据,稍后我按部分对其进行过滤,并将过滤后的“Noticias”添加到我在 IUTableView 中显示的 NSMutableArray 的 arrayNoticiasFiltrado。
-
当我拖动表格视图时收到的错误 IndexPath.row 大于 arrayNoticiasFiltrado.count 并且当我得到第一个 objectAtIndex:indexPath.row 它崩溃,因为 indexPath.row 没有更新为0.
标签: objective-c uitableview nsmutablearray uipickerview