How to subclass UITableViewCell?

I created a subclass of UITableViewCell. So far, I have only used cells that have been “designed” in my storyboard, where I can configure segues.

- (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;
    }
}

After I created my subclass of UITableViewCell, I can no longer use Storyboard to create segues for a custom cell, right? I tried to create a cell in the storyboard and set its class to my custom class, but then the kernel is just empty when I run the application.

So instead, I just assign and run a custom cell in 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;
}

Then in, tableView:didSelectRowAtIndexPathI try to create a NewsViewController by setting a news item and click it on 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];
}

, NewsViewController , . ? ?

+5
1

, alloc-init NewsViewController. NewsViewController . [UIStoryboard instantiateViewControllerWithIdentifier:(NSString *)identifier] .

, ,

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

didSelectRowAtIndexPath. prepareForSegue....

- (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;
    }
}
+3

All Articles