The indexPath of custom table cell is undefined, objective c - objective-c

I have created a custom cell call PendingDoctorTableViewCell and is used in PendingDoctorTableView. Everything is work find and i added a disclosure indicator by using
[cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
It's able to performed the segue "ShowPendingDoctor".
However when i used the prepareForSegue, the indexPath is undefined.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"ShowPendingDoctor"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
ActionForPendingDoctorViewController *destViewController = segue.destinationViewController;
destViewController.myObjects = [myObject objectAtIndex:indexPath.row];
// Hide bottom tab bar in the detail view
// destViewController.hidesBottomBarWhenPushed = YES;
}
}
How can i get to know which row had been tap. because its always return the object of 1st index.
Anyone can help me? i'm rushing for my project. Thanks.

Declare a variable of NSIndexPath to save your currently selected indexPath
NSIndexPath *selectedIndexPath;
Use Tableview delegate to identify selected row and perform segue programmatically
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:YOUR_SEGUE_IDENTIFIER sender:self];
}
Pass your object to next ViewController through prepareForSegue
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:YOUR_SEGUE_IDENTIFIER])
{
ActionForPendingDoctorViewController *destViewController = segue.destinationViewController;
destViewController.myObjects = [myObject objectAtIndex:selectedIndexPath.row];
}
}

Related

Unrecognized selector - using indexpath.row to send a string to view2

Im having trouble passing a string to a second view. I am trying to grab the indexpath.row for the selected tableviewcell and pass the associated string but keep getting "unrecognized selector.."
When using DLog the console displays the correct string. On the view im passing info. to I have an NSString setup. Thanks for any help you can provide!
Here is the prepareforsegue code:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"kShowMovie"])
{
MoviePlayerNEWViewController *dvc = (MoviePlayerNEWViewController *)[segue destinationViewController];
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
DemoVideos *demovideo = [_tableContents objectAtIndex:indexPath.row];
dvc.videoDemoString = demovideo.vd_link;
DLog(#"Video link to pass = %#",demovideo.vd_link);
}
UIViewController * controller = segue.destinationViewController;
[controller setHidesBottomBarWhenPushed:YES];
}
The error message is saying that the controller you're trying to call setVideoDemoString: on is a UINavigationController, not a MoviePlayerNEWViewController as you think it is. You probably want this:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"kShowMovie"])
{
UINavigationController *nav = segue.destinationViewController;
MoviePlayerNEWViewController *dvc = nav.topViewController;
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
DemoVideos *demovideo = [_tableContents objectAtIndex:indexPath.row];
dvc.videoDemoString = demovideo.vd_link;
DLog(#"Video link to pass = %#",demovideo.vd_link);
}
UIViewController * controller = segue.destinationViewController;
[controller setHidesBottomBarWhenPushed:YES];
}

Change data of a button according to selected Row in TableView

I'm trying to change the destination of a button using the selected Row of a tableViewCell.
How would I do that? I know how to change the text in a UILabel. Would it be something like that? I'm using this in my MainView to get to the DetailView:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"toDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
DetailViewController *destViewController = segue.destinationViewController;
destViewController.nameIdentity = [nameData objectAtIndex:indexPath.row];
}
}
And in my DetailView.m:
{
[super viewDidLoad];
nameLabel.text = nameIdentity;
}
I used a NSArray for my data. Is that the correct way to go for the numbers as well?
I`m using X-Code 4.6, Objective-C, Storyboard.
Could this work? (I can't test it on an actual Device yet)
MainView.m
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"toDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
DetailViewController *destViewController = segue.destinationViewController;
destViewController.numberIdentity = [numberData objectAtIndex:indexPath.row];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
numberData = [NSArray arrayWithObjects:/*1*/#"5552525",/*2*/#"5553456", nil];
}
DetailView.m
-(IBAction)callCell {
[[UIApplication sharedApplication]
openURL:[NSURL URLWithString:numberIdentity]];
}
How can I fire a message to the debugger?

Cannot pass value from UITableViewCell to a detail view

I am trying to change the color of a label in my detail view based on which cell is touched in a table view.
I just can't make this happen. Below is my code. I have included the header file from the detail view, and created an IBOutlet for the label.
Update: Ideally, when I clicked on a cell, the label color in detailview should be red. And here is a screencast about my problem. http://www.youtube.com/watch?v=i0nhbsNJWHY&feature=youtube_gdata_player
#pragma mark - TableView Delegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self performSegueWithIdentifier:#"ReferenceDetail" sender:tableView];
}
#pragma mark - Segue
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"ReferenceDetail"]) {
ReferenceDetailViewController *referenceDetailViewController = [segue destinationViewController];
UIViewController *referenceDetailView = [segue destinationViewController];
if(sender == self.searchDisplayController.searchResultsTableView) {
NSIndexPath *indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
NSString *destinationTitle = [[filteredReferenceArray objectAtIndex:[indexPath row]] name];
[referenceDetailView setTitle:destinationTitle];
}
else {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSString *destinationTitle = [[referenceArray objectAtIndex:[indexPath row]] name];
[referenceDetailView setTitle:destinationTitle];
[referenceDetailViewController.label1 setTextColor:[UIColor redColor]];
}
}
}
The problem you are having is that the outlet UILabel inside your ReferenceDetailViewController has not loaded up from the xib, yet you are trying to set it's textColor property.
As a way to make sure this is the problem, try setting a breakpoint in the line where you set the color and another breakpoint inside the ReferenceDetailViewController [viewDidLoad] method. The line where you setup the color should be getting called first.
A simple solution to your problem is to have a property on your ReferenceDetailViewController class that "holds" the UIColor instance and later set it on the viewDidLoad method:
// ...
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"ReferenceDetail"]) {
ReferenceDetailViewController *referenceDetailViewController = [segue destinationViewController];
UIViewController *referenceDetailView = [segue destinationViewController];
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSString *destinationTitle = [[referenceArray objectAtIndex:[indexPath row]] name];
[referenceDetailView setTitle:destinationTitle];
referenceDetailViewController.auxTextColor = [UIColor redColor];
}
// ..
And in ReferenceDetailViewController:
#interface ReferenceDetailViewController : UIViewController
#property (retain, nonatomic) UIColor *auxTextColor;
// ...
#end
#implementation ReferenceDetailViewController
- (void)viewDidLoad
{
[super viewDidLoad];
label1.textColor = self.auxTextColor;
// ...
}
#end
If you must do this for a set of labels, it'd be wise to have a NSDictionary object to hold these properties for you.
The outlets are not setup yet when you segue so you can't get to them to change them. Try sending some object or something over and in the viewdidload of the viewcontroller you segue to you read that and then use that as the way to know what color to change to and then in that same code you change the color of the label.

How do I check what tableview is the current one in a segue

How do I check what tableview is the current one in a segue?
Since UISearchDisplayController activates its own tableview, I get another array with results.
This is the code I tried, but it doesn't work.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"showDetails"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
if (self.tableView == [[self searchDisplayController]searchResultsTableView]) {
}
DetailViewController *detailViewController = [segue destinationViewController];
detailViewController.myObject = [mySearchArray objectAtIndex:indexPath.row];
}
}
According to Apple website, inside the method:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {...}
you would check the tableView
if (tableView == self.tableView) {
return ...;
}
// If necessary (if self is the data source for other table views),
// check whether tableView is searchController.searchResultsTableView.
return ...;

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