gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 12:01:35 25 4
gpt4 key购买 nike

我想创建一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com