2012-02-01 10 views
5

He creado una subclase de UITableViewCell. Hasta ahora, solo he estado usando celdas que están "diseñadas" en mi guión gráfico, donde puedo configurar segues.¿Cómo secuela una subclase de UITableViewCell?

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    [super prepareForSegue:segue sender:sender]; 

    NSIndexPath *indexPath = [self.tableView indexPathForCell:sender]; 

    if ([[segue identifier] isEqualToString:@"showNews"]) { 
     NewsViewController *newsViewController = [segue destinationViewController]; 
     News *news = (News*)[self.fetchedResultsController objectAtIndexPath:indexPath]; 
     newsViewController.news = news; 
    } 
} 

Después de que he creado mi subclase de UITableViewCell ya no soy capaz de utilizar el guión gráfico para crear segues para la celda personalizado, ¿verdad? Intenté crear una celda en el guión gráfico y establecer su clase en mi clase personalizada, pero luego la celda está en blanco cuando ejecuto la aplicación.

Así que en vez sólo soy alloc e init la celda personalizado en mi tableView:cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"WSTableViewCell"; 

    WSTableViewCell *cell = (WSTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[WSTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    WSObject *item = [self.fetchedResultsController objectAtIndexPath:indexPath]; 
    [cell.titleLabel setText:item.title]; 

    return cell; 
} 

Luego, en tableView:didSelectRowAtIndexPath Estoy intentando crear el NewsViewController, el establecimiento de la noticia y empujarlo a la navigationController:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    News* news = [self.fetchedResultsController objectAtIndexPath:indexPath]; 
    NewsViewController *newsViewController = [[NewsViewController alloc] init]; 
    newsViewController.news = news; 
    [[self navigationController] pushViewController:newsViewController animated:YES]; 
} 

Pero cuando selecciono una fila, no se muestra NewsViewController, pero en su lugar veo una vista en blanco con un fondo negro. ¿Cómo puedo lidiar con esto? ¿Es posible seguir usando seques?

Respuesta

3

El problema, creo que es que está utilizando alloc-init para instanciar NewsViewController. Eso no crea su NewsViewController desde el guión gráfico. Debe usar [UIStoryboard instantiateViewControllerWithIdentifier:(NSString *)identifier] para crear instancias del guión gráfico.

Sin embargo, creo que la forma más fácil es llamar

[self performSegueWithIdentifier:@"showNews" sender:indexPath] 

en su didSelectRowAtIndexPath. Usted tiene que cambiar su prepareForSegue... un poco.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    NSIndexPath *indexPath = (NSIndexPath *)sender; 

    if ([[segue identifier] isEqualToString:@"showNews"]) { 
     NewsViewController *newsViewController = [segue destinationViewController]; 
     News *news = (News*)[self.fetchedResultsController objectAtIndexPath:indexPath]; 
     newsViewController.news = news; 
    } 
} 
+0

¡Gracias por la solución! – dhrm

+0

¿Por qué no se necesita la llamada 'super'? –

+1

¿Quiere decir '[super preparedForSegue: ...]'? Creo que no tienes que hacer eso b/c La implementación de 'UIViewController' no hace nada. Si está subclasificando su propia subclase 'UIViewController' y quiere usar también su implementación' prepareForSegue', debe llamar a 'super' en ese momento. – barley

Cuestiones relacionadas