Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When I select a UITableViewCell, my view controller label is an action behind

I have a view controller set up with a table view. I also have a method that should push to a new view controller when one of the table view's cells is selected. The new view controller contains a label, and I would like the label to display the full text of the content of the selected cell.

Currently, the previously selected cell's content is shown on the label when a cell is selected. Here is the current content of my ViewController.m file (Delegate and data source are declared in the header file)

    #import "ViewController.h"



    @interface ViewController ()

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSArray *tweetsArray;

@end

@implementation ViewController

- (void)viewDidLoad
{
    self.tableView.dataSource = self;

    self.tableView.delegate = self;

    self.tweetsArray = [[NSArray alloc] initWithObjects:
                   @"Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus",
                   @"eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Cu",
                   @" Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero v",
                   @" eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales",
                   nil];

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tweetsArray.count;
}

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    NSString *tweet = [self.tweetsArray objectAtIndex:indexPath.row];

    [cell.textLabel setText:tweet];
    [cell.detailTextLabel setText: @"Cody be Cray"];

    return cell;
}

- (void) tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailViewController *dvc = [self.storyboard instantiateViewControllerWithIdentifier: @"DetailViewController"];
    dvc.tweet = [self.tweetsArray objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:dvc animated:YES];
}

@end
like image 264
Josh Winskill Avatar asked Feb 12 '23 22:02

Josh Winskill


1 Answers

You have a "didDeselect" rather than "didSelect" tableview delegate method there.

- (void) tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ...
}

Should probably be this:

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ...
}
like image 179
Zhang Avatar answered Feb 16 '23 02:02

Zhang