prepareForSeque doesn't fire - objective-c

I have UIViewController with a button, button opens a firstTableViewController -> tabViewController (with 3 tabs) -> one of the tabs has a secondTableViewController -> UIViewController.
Everything is in storyboard.
In firstTableViewController the prepareForSeque works fine. I don't even have didSelectRow...
But on secondTableViewController the prepareForSeque doesn't fire. So I added didSelectRowAtIndexPath as follows. But still the prepareForSegue doesn't work.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:#"ShowComment" sender:self];
}
-(void) performSegueWithIdentifier:(NSString *)identifier sender:(id)sender
{
[self prepareForSegue:self.storyboard sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
//When a row is selected, the segue creates the detail view controller as the destination.
//Set the detail view controller's detail item to the item associated with the selected row.
NSLog(#"test");
if ([[segue identifier] isEqualToString:#"ShowComment"]) {
}
}

I found the issue. I did not have an identifier for the cell. Selected the Prototype Cells in the storyboard, in the attributes inspector -> identifier entered 'Cell1'.
In the code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell1";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
Note *note = [notes objectAtIndex:indexPath.row];
cell.textLabel.text = note.title;
return cell;
}
voila, it worked.

Related

pop up a view when i clicked button on table view cell

I have a button on table view cell if I click the button a pop up view will be appeared and the remaining cells will be animated to downwards like animation.and the cell having sub views it will open like a plist in ios if i click the sub view cell it will open the next level sub cells. The tableview must be like below images
Cells and sub-cells must be like this. Can any one assist me?
I have done this before. Just use the following code. Create a custom cell class having the cell and followed view in the cell view but show only what is to be shown by using heightforrowatindexpath method uitableview class. Here is the code
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
if (checkHeight == indexPath.row)
return 185;
else
return 80;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
FAQCell *cell = (FAQCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
[[NSBundle mainBundle] loadNibNamed:#"FAQCell" owner:self options:nil];
cell = myCell;
myCell = nil;
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[cell.quesText setFont:[UIFont fontWithName:#"CaviarDreams" size:16]];
[cell.ansText setFont:[UIFont fontWithName:#"CaviarDreams" size:16]];
// check for the row for which the background color has to be changed
if (checkHeight == indexPath.row)
[cell.bgView setBackgroundColor:[UIColor colorWithRed:0.3333 green:0.7373 blue:0.4588 alpha:1.0]];
else
[cell.bgView setBackgroundColor:[UIColor colorWithRed:0.9176 green:0.3765 blue:0.2980 alpha:1.0]];
return cell;
}
#pragma mark -
#pragma mark Tableview Delegate Methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Deselect cell
[tableView deselectRowAtIndexPath:indexPath animated:TRUE];
checkHeight = indexPath.row;
// reload the data of the table
[tableView reloadSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, tableView.numberOfSections)] withRowAnimation:UITableViewRowAnimationAutomatic];
}
Let me know if you face any issue.
Check out this custom view -
Custom Tree View
Expandable Table View

How to tell which UITableCells are selected with a check mark in a Multi Select UITableview. Objective C

I have a UITableview and in that TableView I which populates with json data. I have implemented a multi selected tableview code that checks the touched table cell with a checkmark.
My question is how can I tell which of my tableview cells I have selected and how do I put an action to the selected table view cells?
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.json2.count;
}
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] ;
}
cell.textLabel.text = self.json2[indexPath.row][#"SurveyName"];
//cell.detailTextLabel.text = self.json1[indexPath.row][#"AssetDesc"];
// Sets the accessory for the cell
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *thisCell = [tableView cellForRowAtIndexPath:indexPath];
if (thisCell.accessoryType == UITableViewCellAccessoryNone) {
thisCell.accessoryType = UITableViewCellAccessoryCheckmark;
}else{
thisCell.accessoryType = UITableViewCellAccessoryNone;
}
}
- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath {
//add your own code to set the cell accesory type.
return UITableViewCellAccessoryNone;
}
So normally If I were to make a segue from a tableview cell I would do the following:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Perform segue
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[self performSegueWithIdentifier: #"showSurveyForm" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"showSurveyForm"]) {
// NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
CameraViewController *destViewController = segue.destinationViewController;
// Hide bottom tab bar in the detail view
destViewController.hidesBottomBarWhenPushed = YES;
destViewController.surveyQuestionId = self.surveyQuestionIDParsed;
destViewController.jsonTitleforSurvey = self.json2;
destViewController.surveyTitleAssetId = self.assetSurvey;
NSLog(#"Survey ID for Survey: %#", self.surveyQuestionIDParsed);
NSLog(#"Survey name titile: %#", self.json2 );
NSLog(#"Asset ID for Title: %#", self.assetSurvey);
}
else if ([segue.identifier isEqualToString:#"showCameraRoll"]) {
// NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
// Hide bottom tab bar in the detail view
}
else if ([segue.identifier isEqualToString:#"showJsonData"]) {
// NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
}
else if ([segue.identifier isEqualToString:#"testPickerDemo"]) {
// NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
}
}
I would like to make a segue after choosing the multi selected cells in my UItableview.
e.g I make a selection and 2 rows are checked. I would like to capture or see in my nslog which tableview cells were selected so I can do another json call and segue based on the rows that were selected.
I know how to call and parse json and make a segue.
You can modify your -didSelectRowAtIndexPath something like this :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *thisCell = [tableView cellForRowAtIndexPath:indexPath];
NSString *selStr = [yourSourceArray objectAtIndex:indexPath.row];
---> NSMutableArray *selArray = [[NSMutableArray alloc] init]; //Write this line in viewDidLoad method.
if (thisCell.accessoryType == UITableViewCellAccessoryNone)
{
thisCell.accessoryType = UITableViewCellAccessoryCheckmark;
[selArray addObject:selStr];
}
else
{
thisCell.accessoryType = UITableViewCellAccessoryNone;
[selArray removeObject:selStr];
}
NSLog(#"selArray :: %#",selArray);
}
UPDATE :
It will give you the Array of textLabel's text of Selected Rows. Then you can use it in -yourButtonPressed method
- (IBAction)yourButtonPressed:(id)sender;
{
NSLog(#"selArray :: %#",selArray);
}
GoodLuck !!!

How to generate a second table view when the user touches a cell in the first (dynamic) table view (in Objective-C)?

I have a table view with dynamic cells generated in the class TableViewController. I made a segue to another table view (which inherits from TableViewController2). I want to trigger the display of the new table view when the user selects a cell from the first table view. I implemented prepareForSegue in TableViewController. The segue works, I can see the first table view pop off screen and the new table view pop in, but it's empty whatever I do. How can I trigger the display of the cells in TableViewController2?
In TableViewController:
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
if ([(segue.identifier) isEqualToString:#"topSegue"]){
TableViewController2 *navigationController = segue.destinationViewController;
TableViewController2 *tvc = segue.destinationViewController;
}
}
In TableViewController2:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Top 50 list";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = #"test";
return cell;
}
Did you define the following two methods in your second tvc?
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
and
(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

Prepareforsegue navigationItem.title one behind

I'm still relative new to object c, so please bear over with me if this is a rookie question. I'm trying to set the title of my navigationcontroller with the corresponding object information. I'm using prepareforsegue for it, but the first time is segue to the new controller, the title is blank. If i try again, it shows up, but if i pressed something else, it shows the title of the things i pressed the time before. I have embedded my code below.
//.h
#import <UIKit/UIKit.h>
#interface STATableViewController : UITableViewController
#property(strong,nonatomic)NSArray *listOfExercises;
#property(weak,nonatomic)NSString *navTitle;
#end
//.m
#import "STATableViewController.h"
#import "ExercisesViewController.h"
#implementation STATableViewController
#synthesize listOfExercises = _listOfExercises, navTitle = _navTitle;
- (void)viewDidLoad
{
[super viewDidLoad];
_listOfExercises = [NSArray arrayWithObjects:#"Raketstart",#"SpeedBåd",#"Træstamme",nil];
self.navigationItem.title = #"Exercises";
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [_listOfExercises count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSString *cellValue = [_listOfExercises objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
_navTitle = [_listOfExercises objectAtIndex:indexPath.row];
//NSLog(_navTitle);
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([[segue identifier] isEqualToString:#"toExercise"])
{
ExercisesViewController *foo = [segue destinationViewController];
foo.navigationItem.title= _navTitle;
}
}
#end
This is happening because prepareForSegue:sender: is being called before tableView didSelectRowAtIndexPath:. So you are always setting your navigationItem's title before you are setting your _navTitle property with the value you want.
Instead of getting the title in didSelectRowAtIndex path, do it in your prepareForSegue like this:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([[segue identifier] isEqualToString:#"toExercise"])
{
// "sender" is the table cell that was selected
UITableViewCell *cell = (UITableViewCell*)sender;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
NSString *title= [_listOfExercises objectAtIndex:indexPath.row];
ExercisesViewController *foo = [segue destinationViewController];
foo.navigationItem.title = title;
}
}

How to seque a subclass of UITableViewCell?

I've created a subclass of UITableViewCell. Until now, I've only been using cells which is "designed" in my storyboard where I can setup segues.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
[super prepareForSegue:segue sender:sender];
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
if ([[segue identifier] isEqualToString:#"showNews"]) {
NewsViewController *newsViewController = [segue destinationViewController];
News *news = (News*)[self.fetchedResultsController objectAtIndexPath:indexPath];
newsViewController.news = news;
}
}
After I've created my subclass of UITableViewCell I'm no longer able to use the Storyboard to create segues for the custom cell, right? I've tried to create a cell in the storyboard and set its class to my custom class -- but then the cell is just blank when I run the App.
So instead I'm just alloc and init the custom cell in my tableView:cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"WSTableViewCell";
WSTableViewCell *cell = (WSTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[WSTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
WSObject *item = [self.fetchedResultsController objectAtIndexPath:indexPath];
[cell.titleLabel setText:item.title];
return cell;
}
Then in tableView:didSelectRowAtIndexPath I'm trying to creating the NewsViewController, setting the news item and push it to the navigationController:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
News* news = [self.fetchedResultsController objectAtIndexPath:indexPath];
NewsViewController *newsViewController = [[NewsViewController alloc] init];
newsViewController.news = news;
[[self navigationController] pushViewController:newsViewController animated:YES];
}
But when I select a row my NewsViewController is not shown -- but instead I see a blank view with a black background. How can I deal with this? Is it possible to still use seques?
The problem I think is that you are using alloc-init to instantiate NewsViewController. That does not create your NewsViewController from storyboard. You have to use [UIStoryboard instantiateViewControllerWithIdentifier:(NSString *)identifier] to instantiate from storyboard.
However, I think the easier way is to call
[self performSegueWithIdentifier:#"showNews" sender:indexPath]
in your didSelectRowAtIndexPath. You have to change your prepareForSegue... a little bit.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
NSIndexPath *indexPath = (NSIndexPath *)sender;
if ([[segue identifier] isEqualToString:#"showNews"]) {
NewsViewController *newsViewController = [segue destinationViewController];
News *news = (News*)[self.fetchedResultsController objectAtIndexPath:indexPath];
newsViewController.news = news;
}
}