Utworzono podklasę: UITableViewCell. Do tej pory używałem tylko komórek, które są "zaprojektowane" w moim scenopisie, gdzie mogę ustawić segues.Jak seque podklasę 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;
}
}
Po utworzeniu mój podklasa UITableViewCell nie jestem już w stanie używać Storyboard stworzyć segues dla komórki niestandardowego, prawda? Próbowałem utworzyć komórkę w scenorysie i ustawić jej klasę na moją niestandardową klasę - ale wtedy komórka jest pusta po uruchomieniu aplikacji.
Zamiast więc jestem po prostu alloc i init komórkę niestandardowe w moim 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;
}
Następnie w tableView:didSelectRowAtIndexPath
Próbuję tworzenia NewsViewController, ustawienie newsa i przesunąć go do 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];
}
Ale kiedy wybieram wiersz mój NewsViewController nie jest wyświetlany - ale zamiast tego widzę pusty widok z czarnym tłem. Jak sobie z tym poradzić? Czy nadal można używać seques?
Dziękujemy za rozwiązanie! – dhrm
Dlaczego nie jest potrzebne połączenie typu "super"? –
Masz na myśli '[super prepareForSegue: ...]'? Myślę, że nie musisz tego robić b/c Implementacja 'UIViewController' nie robi nic. Jeśli podklasujesz własną podklasę 'UIViewController' i chcesz również użyć jej implementacji' prepareForSegue', musisz wywołać 'super' w tym czasie. – barley