当我选择UITableViewCell时,我的视图控制器标签就是后面的动作

Jos*_*ill 4 objective-c uitableview ios

我有一个视图控制器设置与表视图.我还有一个方法,当选择一个表视图的单元格时,该方法应该推送到新的视图控制器.新的视图控制器包含一个标签,我希望标签显示所选单元格内容的全文.

目前,当选择单元格时,标签上显示先前选择的单元格内容.这是我的ViewController.m文件的当前内容(委托和数据源在头文件中声明)

    #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
Run Code Online (Sandbox Code Playgroud)

Zha*_*ang 8

你有一个"didDeselect"而不是"didSelect"tableview委托方法.

- (void) tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

应该是这样的:

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ...
}
Run Code Online (Sandbox Code Playgroud)