【问题标题】:unable to pass data from tableview to view controller无法将数据从 tableview 传递到视图控制器
【发布时间】:2016-04-21 16:27:26
【问题描述】:

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

这是我的代码:

    #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

我添加了导航控制器,并在 tableview 控制器和视图控制器之间添加了 segue。

任何人都可以帮助我吗?

【问题讨论】:

    标签: ios objective-c iphone uitableview


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢,但似乎:'TableViewController' 没有可见的@interface 声明选择器'performSegueWithIdentifier:'
    【解决方案2】:

    在触发像@KIDdAe 说的segue 之后,你必须像这样从导航控制器获取视图控制器:

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

    【讨论】:

    • 谢谢,但是代码“ViewController2*vc = ((UINavigationController *) segue.destinationViewController).topViewController;”上出现了一个错误“线程1:断点2.1”
    【解决方案3】:

    通过将 sender 传递给 indexPath 来触发表视图委托调用中的 segue

    • (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

      [self performSegueWithIdentifier:@"showlawfirmdetail" sender: indexPath];

    }

    然后使用此实例从数据源数组中获取数据:

    • (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { NSLog(@"prepareForSegue: %@", segue.identifier);

      if ([segue.identifier isEqualToString:@"TeamMembersSegue"]) {

      NSIndexPath *indexPath = (NSIndexPath *)sender; ViewController2*vc = ((UINavigationController *) segue.destinationViewController).topViewController; vc.lawfirmname = [缩略图 objectAtIndex:indexPath.row];

      }

    }

    希望这项工作!

    【讨论】:

    • 太棒了! ,将其标记为答案,然后它将对其他人有所帮助。
    猜你喜欢
    • 2013-04-09
    • 1970-01-01
    • 1970-01-01
    • 2022-08-23
    • 2016-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多