gpt4 book ai didi

ios - 如何检测静态 TableView 中的单元格并使用 segue

转载 作者:行者123 更新时间:2023-11-28 18:34:03 24 4
gpt4 key购买 nike

我有我的静态 tableViewController 的代码。 tableViewController 工作得很好。但我不知道如何检测该行并将 segue 运行到另一个 View / TableView Controller 。

表格 View 代码如下:

#import "MainMenuTVC.h"
#import "LoggedUser.h"

@interface MainMenuTVC ()

@end

@implementation MainMenuTVC

- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];

// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
User* loggedUser = [LoggedUser getLoggedUser];

if([loggedUser.role isEqualToString:@"admin"])
return 5;
else
return 4;
}

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

if (cell == nil) {
cell = [[UITableViewCell alloc] init];
}

switch (indexPath.row) {
case 0:{
cell.textLabel.text = @"Třídy";
break;
}
case 1:{
cell.textLabel.text = @"Studenti";
break;
}
case 2:{
cell.textLabel.text = @"Události";
break;
}
case 3:{
cell.textLabel.text = @"Rozrvh";
break;
}
case 4:{
cell.textLabel.text = @"Správa školy";
break;
}

default:
break;
}

return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/


#pragma mark - Navigation

// In a story board-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSLog(@"%@", sender);
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}

@end

我需要如下内容:

我单击了带有文本“Studenti”的行/索引为“1”的行,因为我单击了这一行,所以我想使用标识符“Students”进行 segue。当我单击另一行时,我想转到另一个 View ,但这并不重要。它只是开关组件。

最佳答案

您必须实现以下方法:

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// access the cell via indexPath, do whatever you need to prepare the segue. Then:
[self performSegueWithIdentifier:@"Students" sender:nil];
}

你在这里 :)

关于ios - 如何检测静态 TableView 中的单元格并使用 segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22485276/

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