gpt4 book ai didi

objective-c - 使用 Xcode Storyboard 从 TableView 单元格推送到详细 View

转载 作者:技术小花猫 更新时间:2023-10-29 10:46:47 24 4
gpt4 key购买 nike

我在 View Controller 中有一个 TableView 。我可以在表格 View 中填充我的所有信息。但是,我对设置详细 View 有点迷茫。我相信每个表格单元格都需要一个 segue 到每个详细 View ,但不完全确定。

这是我的代码。我缺少什么来完成从 TableView 到详细 View 的转换?代码:

.h 

@interface DetailViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>
{
IBOutlet UITableView *myTable;
NSMutableArray *contentArray;
}

@property (strong, nonatomic) IBOutlet UITableView *myTable;

.m


- (void)viewDidLoad
{
contentArray = [[NSMutableArray alloc]init];
[contentArray addObject:@"Espresso"];
[contentArray addObject:@"Latte"];
[contentArray addObject:@"Capicino"];
[super viewDidLoad];
// Do any additional setup after loading the view.
}

//Table Information
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [contentArray count];
}

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

[tableView deselectRowAtIndexPath:indexPath animated:YES];

if([[contentArray objectAtIndex:indexPath.row]isEqualToString:@"EspressoViewController"])
{
EspressoViewController *espresso = [[EspressoViewController alloc]initWithNibName:@"EspressoViewController" bundle:nil];
[self.navigationController pushViewController:espresso animated:YES];
}
else if ([[contentArray objectAtIndex:indexPath.row] isEqualToString:@"Latte"])
{
LatteViewController *latte = [[LatteViewController alloc] initWithNibName:@"Latte" bundle:nil];
[self.navigationController pushViewController:latte animated:YES];
}

}

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
[self tableView:tableView didSelectRowAtIndexPath:indexPath];
}


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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"CellIdentifier"];
}

NSString *cellValue = [contentArray objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.font = [UIFont systemFontOfSize:16];
cell.detailTextLabel.text = @"Hot and ready";

UIImage *image = [UIImage imageNamed:@"coffeeButton.png"];
cell.imageView.image = image;

cell.textLabel.text = [contentArray objectAtIndex:indexPath.row];
return cell;
}

最佳答案

我觉得你把这个搞得有点太复杂了。别担心,我经常做同样的事情。

首先,通过从 tableView:accessoryButtonTappedForRowAtIndexPath: 中发送 tableView:didSelectRowAtIndexPath:,这两种方法之间没有区别。点击单元格或其附属按钮执行相同的操作。如果您不需要配件按钮来执行与点击单元格本身不同的操作,请将其移除。

其次,如果您使用的是 Storyboard,则无需为 View Controller 分配/initWithNib。相反,使用 segue。如果您通过 Storyboard执行此操作,您也不需要以编程方式将 viewControllers 推送到您的 navigationController

首先构建你的 Storyboard:

  1. 拖出一个 UITableViewController。确保使用右侧的检查器 Pane 将拖出的 UITableViewController 的类设置为您自己的“DetailViewController”。
  2. 然后选择此 Controller 并使用菜单选择“编辑器->嵌入->导航 Controller ”。
  3. 接下来,拖出三个通用 UIViewController。将其中一个的类设置为“LatteViewController”,将另一个设置为“EspressoViewController”,将第三个设置为“CapicinoViewController”(再次使用检查器)。
  4. 按住 Control 并从 UITableViewController 拖动到每个 viewController,然后选择 PUSH
  5. 单击 UITableViewController 和每个 viewController 之间的箭头上的小圆圈。在检查器(右侧)中,在 Identifier 字段中为每个 segue 指定一个唯一的名称。您需要为您的代码记住这个名称。我将它们命名为“EspressoSegue”、“LatteSegue”和“CapicinoSegue”。您会在下面的代码中看到原因。

然后将以下代码放入您的 UITableViewController 中:

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

//Build a segue string based on the selected cell
NSString *segueString = [NSString stringWithFormat:@"%@Segue",
[contentArray objectAtIndex:indexPath.row]];
//Since contentArray is an array of strings, we can use it to build a unique
//identifier for each segue.

//Perform a segue.
[self performSegueWithIdentifier:segueString
sender:[contentArray objectAtIndex:indexPath.row]];
}

如何实现其余部分由您决定。您可能希望在 UITableViewController 中实现 prepareForSegue:sender:,然后使用该方法将信息发送到 segue.destinationViewController

请注意,我将来自您的 contentArray 的字符串作为 segue 的发件人传递。你可以通过任何你喜欢的。标识单元格的字符串似乎是最符合逻辑的传递信息,但选择取决于您。

上面发布的代码应该为您执行导航。

关于objective-c - 使用 Xcode Storyboard 从 TableView 单元格推送到详细 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12464164/

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