如何检测静态表视图中的单元格并使用segue

时间:2014-03-18 16:25:36

标签: ios objective-c uitableview static segue

我有我的静态tableViewController的代码。 tableViewController工作得很好。但我不知道如何检测行并将segue运行到另一个视图/ tableview控制器。

以下是表格视图的代码:

#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”的行,因为我点击了这一行,我想用标识符“学生”做segue。当我点击另一行时,我想转到另一个视图,但这并不重要。它只是一个开关组件。

3 个答案:

答案 0 :(得分:2)

您必须实施以下方法:

- (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];
}

你有:)

答案 1 :(得分:2)

在这种情况下最简单的解决方案是直接在界面构建器中创建segue。只需从Studenti单元格中按住Ctrl键拖动到目标视图控制器即可。不需要乱用didSelectRowAtIndexPath:)

答案 2 :(得分:1)

您可以执行@thomas建议的操作。如果它是一个静态表,你没有太多的逻辑,那么就像@David说的那样:

From Cell To detail Controller

<强>步骤

  • 选择单元格
  • 按住 control
  • 拖放