ios - 无法将数据从 TableView 传递到 View Controller

标签 ios objective-c iphone uitableview

我想创建一个 segue 以便在我的项目中的 tableviewcontroller 和 View Controller 之间进行链接。但是,当我运行应用程序时,当我单击表格单元格时它没有任何反应。

这是我的代码:

    #import "TableViewController.h"
    #import "SimpleTableCell.h"
    #import "ViewController2.h"

    @interface TableViewController ()

    @end

    @implementation TableViewController
    {
        NSArray *tableData;
        NSArray *thumbnails;
        NSArray *detail;


    }

    @synthesize tableview;

    - (void)viewDidLoad {
        [super viewDidLoad];

        NSString*path = [[NSBundle mainBundle]pathForResource:@"LawFirm"ofType:@"plist"];

        NSDictionary*dict= [[NSDictionary alloc] initWithContentsOfFile:path];
        tableData=[dict objectForKey:@"Name"];
        thumbnails=[dict objectForKey:@"Thumbnail"];
        detail=[dict objectForKey:@"Detail"];

        [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];

        UIEdgeInsets inset = UIEdgeInsetsMake(5, 5, 5, 5);
        self.tableView.contentInset = inset;


    }


    #pragma mark - Table view data source

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

        return 1;
    }

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
    {
        return (toInterfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);

    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

        return [tableData count];
    }

    - (UIImage *)cellBackgroundForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSInteger rowCount = [self tableView:[self tableView] numberOfRowsInSection:0];
        NSInteger rowIndex = indexPath.row;
        UIImage *background = nil;

        if (rowIndex == 0) {
            background = [UIImage imageNamed:@"cell_top.png"];
        } else if (rowIndex == rowCount - 1) {
            background = [UIImage imageNamed:@"cell_bottom.png"];
        } else {
            background = [UIImage imageNamed:@"cell_middle.png"];
        }

        return background;
    }




    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

         static NSString *simpleTableIdentifier = @"SimpleTableItem";

        SimpleTableCell *cell = (SimpleTableCell*) [tableView dequeueReusableCellWithIdentifier: simpleTableIdentifier];

        if (cell == nil) {

            NSArray*nib = [[NSBundle mainBundle]loadNibNamed:@"SimpleTableCell" owner:self options:nil];
            cell = [nib objectAtIndex:0];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }

        cell.name.text = [tableData objectAtIndex:indexPath.row];
        cell.photo.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
        cell.detail.text = [detail objectAtIndex:indexPath.row];

        UIImage*background = [self cellBackgroundForRowAtIndexPath:indexPath];

        UIImageView *cellBackgroundView = [[UIImageView alloc] initWithImage:background];
        cellBackgroundView.image = background;
        cell.backgroundView = cellBackgroundView;

        return cell;
    }




    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return 78;
    }



    -(void)prepareForSegue:(UIStoryboardSegue*)segue sender:(id)sender {
        if([segue.identifier isEqualToString:@"showlawfirmdetail"]){
            NSIndexPath*indexPath = [self.tableView indexPathForSelectedRow];
            ViewController2*vc = segue.destinationViewController;
            vc.lawfirmname = [thumbnails objectAtIndex:indexPath.row];
        }
    }

    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
        return YES;
    }

@end

我添加了导航 Controller ,并在 tableview Controller 和 View Controller 之间添加了 segue。

谁能帮帮我?

最佳答案

你应该在单元格被点击时触发 segue

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {  
    [self performSegueWithIdentifier:@"showlawfirmdetail" sender:self];
}

关于ios - 无法将数据从 TableView 传递到 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36775512/

相关文章:

iphone - 如何在我的网站上嵌入视频以便在 iPhone 上运行以及需要什么视频格式?

iPhone 邮件客户端阻止时间戳日历链接?

objective-c - 为 XCODE 项目生成 Apple Docs

ios - 具有两个单元格的 UICollectionView 自定义布局

iphone - 为什么在自动保存 UIDocument 后调用 loadFromContents?

不同商店中的 iPhone 应用程序名称

ios - iOS 中的 Touch ID 密码集成

iphone - 无法在 iPhone 中切换回 View

iphone - 无法在 iPhone 上加载/安装应用程序

objective-c - NSURLSessionDownloadTask 在使用后台配置时不断重试