I have this code of my static tableViewController. The tableViewController works great. But I don't know how I can detect the row and run segue to another view/tableview controller.
Here is code of table 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
I need something as follows:
I clicked on row with text "Studenti"/ on row with index "1" and because I clicked on THIS row I want to do segue with Identifier "Students". When I clicked on another row I want go to another view, but it isn't important. It's only switch component.
You must implement the following method:
- (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];
}
And there you are :)
Easiest solution in this case is to create the segue directly in interface builder. Just ctrl-drag from the Studenti cell to the target view controller. No need to mess around with didSelectRowAtIndexPath :)
You can do what @thomas
suggested. If it's a static table and you dont have too much logic then do it like @David
said:
Steps
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.