iOS Search Bar Won't Link to Proper Results - ios

Whenever I search for something from a search bar, I get the correct results. When I click on those results, it links me to the same place that the original results would have linked me to. In other words, I have teacher a-e, I type in 'e', and get only the result 'e', but when I click on that cell, it links me to the teacher 'a' profile.
Here is what I have so far.
#import <UIKit/UIKit.h>
#interface ListTableViewController : UITableViewController
#end
---
#import "ListTableViewController.h"
#import "DetailsViewController.h"
#interface ListTableViewController () <UISearchDisplayDelegate>
#property (strong, nonatomic) NSArray *className;
#property (strong, nonatomic) NSArray *teacherName;
#property (strong, nonatomic) NSArray *blockNumber;
#property (strong, nonatomic) NSArray *myNew;
#property (strong, nonatomic) NSArray *searchResults;
#end
#implementation ListTableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.className = [NSArray arrayWithObjects:#"Biology",#"English III",#"Chemistry",#"Algebra II",#"Morality", nil];
self.teacherName = [NSArray arrayWithObjects:#"Teacher A",#"Teacher B",#"Teacher C",#"Teacher D",#"Teacher E", nil];
self.blockNumber = [NSArray arrayWithObjects:#"B1",#"B3",#"B6",#"B2",#"B1", nil];
NSMutableArray *combinedArray = [[NSMutableArray alloc]init];
for (int i = 0; i < [self.className count]; i++)
{
NSString *combinedString = [NSString stringWithFormat:#"%# | %# | %#",[self.className objectAtIndex:i],[self.teacherName objectAtIndex:i],[self. blockNumber objectAtIndex:i]];
[combinedArray addObject:combinedString];
}
self.myNew = combinedArray;
}
- (void)filterContentForSearchText: (NSString *) searchText
{
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:#"SELF CONTAINS[cd] %#", searchText];
self.searchResults = [self.myNew filteredArrayUsingPredicate:resultPredicate];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString];
return YES;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.tableView) {
return [self.myNew count];
} else { // (tableView == self.searchDisplayController.searchResultsTableView)
return [self.searchResults count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (tableView == self.tableView) {
cell.textLabel.text = [self.myNew objectAtIndex:indexPath.row];
} else {
cell.textLabel.text = [self.searchResults objectAtIndex:indexPath.row];
}
return cell;
}
#pragma mark - Table view delegate
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"showDetails"]) {
DetailsViewController *dvc = segue.destinationViewController;
NSIndexPath *indexPath = nil;
if ([self.searchDisplayController isActive]) {
indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
dvc.sendLabel = [self.searchResults objectAtIndex:indexPath.row];
dvc.teachersendLabel = [self.teacherName objectAtIndex:indexPath.row];
return;
} else{
indexPath = [self.tableView indexPathForSelectedRow];
dvc.sendLabel = [self.myNew objectAtIndex:indexPath.row];
dvc.teachersendLabel = [self.teacherName objectAtIndex:indexPath.row];
return;
}
}
}
#end
In my DetailsViewController
#import <UIKit/UIKit.h>
#interface DetailsViewController : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *label;
#property (strong, nonatomic) NSString *sendLabel;
#property (weak, nonatomic) IBOutlet UILabel *teacherlabel;
#property (strong, nonatomic) NSString *teachersendLabel;
#end
---
#implementation DetailsViewController
#synthesize label;
- (void)viewDidLoad
{
[super viewDidLoad];
self.teacherlabel.text = [NSString stringWithFormat:#"%#", self.teachersendLabel];
self.label.text = [NSString stringWithFormat:#"%#", self.sendLabel];
}
#end

Looking at your code it wouldn't seem there to be any problem. The are only two things I can think of:
1) I'm not sure how you're displaying the 'main' tableView and the search results one. Might it be that your touches are actually getting handled by the 'main' tableView? This might happen if you have the two tables aligned on top of each other and the bottom one is still visible and with userInteractionEnabled set to YES when the search one 'isActive'. In this case the view hierarchy should look similar to this:
- UIView
- UITableView (main)
- UITableView (search)
2) the use of -[UITableView indexPathForSelectedRow] in prepareForSegue:sender:. If you're using Storyboard the sender is the selected cell. You may want to check that the sender is an actual cell or an indexPath isKindOfClass. If the sender is an indexPath you can use it, if it's a cell you can call the method -[UITableView indexPathForCell:]. Using this approach you make sure your segue is actually triggering for the right event (e.g. you can programmatically select a cell, but this won't fire a segue and you can later decide to call -performSegueWithIdentifier:sender: and this would break your implementation).

Related

Problems with Search Display Controller

I am learning to use Search Bars a Search Display Controllers in iOS. I made a simple test project with two views, one view contains a UITableView and a UISearchBar, when you select an row from the table view, the new view shows a label with the name of the row. I'm getting a couple of problems, first when I start to add text in the search bar and the search display appears, the search bar is gone. The filtered results are shown, but when I select a row it does not take me to the next view, this only happens when the table is filtered. Here is the code:
ViewController.h
#import <UIKit/UIKit.h>
#import "DetailViewController.h"
#interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, UISearchDisplayDelegate, UISearchBarDelegate>
#property (strong, nonatomic) IBOutlet UISearchBar *searchBar;
#property (strong, nonatomic) IBOutlet UITableView *tableView;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#property (nonatomic, strong) NSArray *objects;
#property (nonatomic, strong) NSArray *filteredObjects;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
_objects = [[NSMutableArray alloc] initWithObjects:#"One", #"Two", #"Three", #"Four", #"Five", #"Six", #"Seven", #"Eight", #"Nine", #"Ten", nil];
_tableView.delegate = self;
_tableView.dataSource = self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [_filteredObjects count];
} else {
return [_objects count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (tableView == self.searchDisplayController.searchResultsTableView) {
cell.textLabel.text = [_filteredObjects objectAtIndex:indexPath.row];
} else {
cell.textLabel.text = [_objects objectAtIndex:indexPath.row];
}
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"ToDetail"]) {
DetailViewController *detailViewController = [segue destinationViewController];
if (self.searchDisplayController.active) {
NSIndexPath *indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
detailViewController.detailString = [_filteredObjects objectAtIndex:indexPath.row];
} else {
NSIndexPath *indexPath = [_tableView indexPathForSelectedRow];
detailViewController.detailString = [_objects objectAtIndex:indexPath.row];
}
}
}
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:#"SELF contains[c] %#", searchText];
_filteredObjects = [_objects filteredArrayUsingPredicate:resultPredicate];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];
return YES;
}
Thanks for the help!
when I select a row it does not take me to the next view, this only happens when the table is filtered
Because you have no implementation of tableView:didSelectRowAtIndexPath:.
Remember, there is really no such thing as a "filtered table view". There are two table views: the normal one, and the one that appears because of the search display controller. That is a different table view (basically appearing in front of yours), and you must configure it for whatever you want it to do and however you want it to look.

Empty cells on SearchDisplayController

I am trying to implement a SearchDisplayController to a tableView.
When I create my tableview everything it is ok.
I have got a UiViewController with Delegate of UitableViewController.
This is my .h
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#interface getClientsViewController : UIViewController <UITableViewDelegate,UITableViewDataSource> {
IBOutlet UIView *vistaClientes;
IBOutlet UIView *menu;
IBOutlet UITableView *tablita;
}
#property(nonatomic, retain) AppDelegate *app;
#property(nonatomic, retain) NSMutableDictionary *clientList;
#property(nonatomic, retain) IBOutlet UIView *vistaClientes;
#property(nonatomic, retain) IBOutlet UIView *menu;
#property(nonatomic, retain) NSString *menuPos;
#property(nonatomic, retain) IBOutlet UITableView *tablita;
-(IBAction)showMenu:(id)sender;
#end
And this is on my .m
#import "getClientsViewController.h"
#import "checkInternet.h"
#import "AccessLogin.h"
#import "ClientesCell.h"
#import "menuGlobalViewController.h"
#import "getOrdenesViewController.h"
#import "InterfaceGLobal.h"
#interface getClientsViewController ()
#end
#implementation getClientsViewController {
NSArray *recipes;
NSArray *searchResults;
}
#synthesize app,clientList,vistaClientes,menuPos,menu,tablita;
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSArray *cliente = [NSArray arrayWithArray:[clientList objectForKey:#"clients"]];
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:#"nickname contains[c] %# OR clientNumber contains[c] %#",searchText, searchText];
searchResults = [cliente filteredArrayUsingPredicate:resultPredicate];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];
return YES;
}
#pragma mark - Table view data source
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [searchResults count];
} else {
return [[clientList objectForKey:#"clients"] count];
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"ClienteCelda";
ClientesCell *cell = (ClientesCell *)[self.tablita dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSLog(#"Im in");
cell = [[ClientesCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSArray *cliente = [NSArray array];
if (tableView == self.searchDisplayController.searchResultsTableView) {
cliente = [NSArray arrayWithArray:searchResults];
} else {
cliente = [NSArray arrayWithArray:[clientList objectForKey:#"clients"]];
}
[cell.Direccion setText:[[cliente objectAtIndex:indexPath.row] objectForKey:#"address1"]];
[cell.nombreCliente setText:[[cliente objectAtIndex:indexPath.row] objectForKey:#"name"]];
[cell.nombreCorto setText:[[cliente objectAtIndex:indexPath.row] objectForKey:#"nickname"]];
[cell.imagen setImage:[UIImage imageNamed:#"avatar.png"]];
NSLog(#"cellda: %#",[[cliente objectAtIndex:indexPath.row] objectForKey:#"address1"]);
return cell;
}
I quite some methods, just for make more visible the code.
When the view load, is showing correctly the results, but when I type some search, return empty cells with default style.
I know, when I search, the if(cell==nil) is true, but is not loading correctly the style.
Someone can help me?
Here are two screen shots will help explain:

Why are my mutable arrays and table views behaving unexpectedly?

I'm building a sign-in app. It has two table views. One lists people who have visited before (existingNames), and one lists the current people signed in (names).
At certain points in my code the only mutable array that does not crash the program when accessed is names.
names and existingNames seems to somehow be reversed as well. When I try to remove from names, the program crashes. When I remove from existingNames, the changes are reflected in tableView2, but tableView2 is supposed to be associated with names.
In the app's current state everything is "working" except for accessing any company arrays. I put quotes around working because of the names and existingNames being used backwards.
Any insight into what might be causing the problem would be much appreciated!
.h:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>{
UITableView *tableView;
}
#property (weak, nonatomic) IBOutlet UITextField *nameField;
#property (weak, nonatomic) IBOutlet UITextField *companyField;
#property (nonatomic, retain) NSMutableArray *names;
#property (nonatomic, retain) NSMutableArray *companies;
#property (nonatomic, retain) NSMutableArray *existingNames;
#property (nonatomic, retain) NSMutableArray *existingCompanies;
- (IBAction)add:(id)sender;
- (IBAction)addExisting:(id)sender;
- (IBAction)remove:(id)sender;
- (IBAction)submit:(id)sender;
#property (strong, nonatomic) IBOutlet UITableView *tableView1;
#property (weak, nonatomic) IBOutlet UITableView *tableView2;
#end
.m:
#import "ViewController.h"
#import <MessageUI/MessageUI.h>
#interface ViewController () <MFMailComposeViewControllerDelegate>
#end
#implementation ViewController
#synthesize nameField;
#synthesize companyField;
#synthesize names;
#synthesize companies;
#synthesize existingNames;
#synthesize existingCompanies;
#synthesize tableView1 = _tableView1;
#synthesize tableView2 = _tableView2;
int rowNumber1;
int rowNumber2;
- (void)viewDidLoad
{
[super viewDidLoad];
self.names = [[NSMutableArray alloc] init];
self.companies = [[NSMutableArray alloc] init];
self.existingNames = [[NSMutableArray alloc] init];
self.existingCompanies = [[NSMutableArray alloc] init];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.tableView1){
return [existingNames count];
}
else if (tableView == self.tableView2){
return [names count];
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableItem";
UITableViewCell *cell;
if (tableView == self.tableView1){
cell = [_tableView1 dequeueReusableCellWithIdentifier:simpleTableIdentifier];
}
else if (tableView == self.tableView2){
cell = [_tableView2 dequeueReusableCellWithIdentifier:simpleTableIdentifier];
}
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
if (tableView == self.tableView1){
cell.textLabel.text = [existingNames objectAtIndex:indexPath.row];
}
else if (tableView == self.tableView2){
cell.textLabel.text = [names objectAtIndex:indexPath.row];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (tableView == self.tableView1){
rowNumber1 = indexPath.row;
}
else if (tableView == self.tableView2){
rowNumber2 = indexPath.row;
}
}
- (IBAction)add:(id)sender {
BOOL exists = [existingNames containsObject:nameField.text];
if(exists == FALSE){
[names addObject:nameField.text];
[companies addObject:companyField.text];
[existingNames addObject:nameField.text];
[existingCompanies addObject:companyField.text];
}
else{
[names addObject:nameField.text];
[companies addObject:companyField.text];
}
[_tableView1 reloadData];
[_tableView2 reloadData];
nameField.text=#"";
companyField.text=#"";
}
- (IBAction)addExisting:(id)sender {
[existingNames addObject:[names objectAtIndex:rowNumber1]];
//[companies addObject:[existingCompanies objectAtIndex:rowNumber]];
[_tableView2 reloadData];
}
- (IBAction)remove:(id)sender {
[existingNames removeObjectAtIndex:rowNumber2];
[existingCompanies removeObjectAtIndex:rowNumber2];
[_tableView2 reloadData];
}
#end
The following methods are part of the UITableViewDataSource and UITableViewDelegate protocols
tableView:numberOfRowsInSection:
tableView:cellForRowAtIndexPath:
tableView:didSelectRowAtIndexPath:
and will be called by your table view assuming you've property assigned the table view's delegate and dataSource properties. These methods, however,
tableView2:numberOfRowsInSection:
tableView2:cellForRowAtIndexPath:
tableView2:didSelectRowAtIndexPath:
are not part of the protocol and won't ever get called by the table view. It looks like you may be confusing your property names, e.g. tableView, with with the protocol method names, e.g. tableView:numberOfRowsInSection:. What you need to do is:
If you haven't already done so, set your view controller as the delegate and dataSource for both your tableView and tableView2.
In each of the data source and delegate methods that you need to implement, handle the cases for both tables.
For example, your tableView:numberOfRowsInSection: method would look like:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.tableView) {
return [existingNames count];
}
else if (tableView == self.tableView2) {
return [names count];
}
return 0;
}

How to change the text in several labels when a special table view cell is pressed?

Here are the ViewControllers again.
Here is my ViewController.h:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>
#property (copy, nonatomic) NSDictionary *firstTableView;
#property (copy, nonatomic) NSArray *firstTableViewKey;
#property (weak, nonatomic) IBOutlet UILabel *norskLabel;
#property (weak, nonatomic) IBOutlet UILabel *infinitivLabel;
#property (weak, nonatomic) IBOutlet UILabel *presensLabel;
#property (weak, nonatomic) IBOutlet UILabel *preteritumLabel;
#property (weak, nonatomic) IBOutlet UILabel *perfektumLabel;
#end
Here is my ViewController.m:
#import "ViewController.h"
static NSString *SectionsTableIdentifier = #"SectionsTableIdentifier";
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UITableView *tableView = (id)[self.view viewWithTag:1];
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:SectionsTableIdentifier];
NSString *path = [[NSBundle mainBundle] pathForResource:#"SterkeVerb" ofType:#"plist"];
self.firstTableView = [NSDictionary dictionaryWithContentsOfFile:path];
self.firstTableViewKey = [[self.firstTableView allKeys] sortedArrayUsingSelector:#selector(compare:)];
tableView.backgroundColor = [UIColor clearColor];
tableView.opaque = NO;
tableView.backgroundView = nil;
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [self.firstTableViewKey count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSString *key = self.firstTableViewKey[section];
NSArray *nameSection = self.firstTableView[key];
return [nameSection count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return self.firstTableViewKey[section];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SectionsTableIdentifier];
NSString *key = self.firstTableViewKey[indexPath.section];
NSArray *nameSection = self.firstTableView[key];
cell.textLabel.text = nameSection[indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0){
_norskLabel.text = #"å bake";
_infinitivLabel.text = #"zu backen";
_presensLabel.text = #"bäckt/backt";
_preteritumLabel.text = #"backte";
_perfektumLabel.text = #"hat gebacken";
}
else if (indexPath.row == 1){
_norskLabel.text = #"å motta";
_infinitivLabel.text = #"zu empfangen";
_presensLabel.text = #"empfängt";
_preteritumLabel.text = #"empfing";
_perfektumLabel.text = #"hat empfangen";
}
}
Ok, so i have my table view devided into sections(A-Z) and when i filled in the code it worked perfectly for the first section. BUT, when i pressed one of the cells in the next section, it showed the same information as the first cell in the first section. WHY?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0){
_norskLabel.text = #"å bake";
_infinitivLabel.text = #"zu backen";
_presensLabel.text = #"bäckt";
_preteritumLabel.text = #"backte";
_perfektumLabel.text = #"hat gebacken";
else if (indexPath.row == 1){
_norskLabel.text = #"å begynne";
_infinitivLabel.text = #"zu beginnen";
_presensLabel.text = #"beginnt";
_preteritumLabel.text = #"begann";
_perfektumLabel.text = #"hat begonnen";
}
}
If you set the text in tableview:didSelectRowAtIndexPath: it will just get overwritten when tableview:cellForRowAtIndexPath: is called
Inside tableview:didSelectRowAtIndexPath:, you need to access the data driving the table and change it at its source.
NSString *key = self.firstTableViewKey[indexPath.section];
NSArray *nameSection = self.firstTableView[key];
nameSection[indexPath.row] = #"New Value";

Detail Accessory Button Segue

My app have UITableViewController and DetailViewController.
UITableViewController with multi cell selection and Detail accessory button.
If the user select multiple cell and tap on TOTAL Button Alert message show the total of courses price.
And if user tap on Detail accessory button it Segue to DetailViewController.
OK now my problem is how could I do that with my code:
UITableViewController.m
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = [[object valueForKey:#"courseCode"] description];
cell.detailTextLabel.text = [[object valueForKey:#"courseName"] description];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"showDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSManagedObject *object = [[self fetchedResultsController] objectAtIndexPath:indexPath];
[[segue destinationViewController] setDetailItem:object];
}
}
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
if ([[[object valueForKey:#"creditHours"] description]isEqualToString: #""]) {
}
//some thing doing if the user deselect the cell..
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
if ([[[object valueForKey:#"creditHours"] description]isEqualToString: #""]) {
}
//some thing doing if the user deselect the cell..
}
- (IBAction)doneButton:(id)sender {
given the total...
}
DetailViewController.h
#property (strong, nonatomic) id detailItem;
#property (weak, nonatomic) IBOutlet UITextField *courseCodeLabel;
#property (weak, nonatomic) IBOutlet UITextField *courseNameLabel;
#property (weak, nonatomic) IBOutlet UITextField *creditHoursLabel;
#property (weak, nonatomic) IBOutlet UITextField *preRequisitesLabel;
#property (weak, nonatomic) IBOutlet UITextField *coursePriceLabel;
#property (weak, nonatomic) IBOutlet UITextField *courseEPPLabel;
#property (weak, nonatomic) IBOutlet UITextView *courseDetailLabel;
DetailViewController.m
#interface DetailViewController ()
- (void)configureView;
#end
#implementation DetailViewController
#synthesize detailItem = _detailItem;
#synthesize courseCodeLabel = _courseCodeLabel;
#synthesize courseNameLabel = _courseNameLabel;
#synthesize creditHoursLabel = _creditHoursLabel;
#synthesize preRequisitesLabel = _preRequisitesLabel;
#synthesize coursePriceLabel = _coursePriceLabel;
#synthesize courseEPPLabel = _courseEPPLabel;
#synthesize courseDetailLabel = _courseDetailLabel;
#pragma mark - Managing the detail item
- (void)setDetailItem:(id)newDetailItem
{
[self configureView];
if (_detailItem != newDetailItem) {
_detailItem = newDetailItem;
// Update the view.
[self configureView];
}
}
- (void)configureView
{
// Update the user interface for the detail item.
if (self.detailItem) {
self.courseCodeLabel.text = [[self.detailItem valueForKey:#"courseCode"] description];
self.courseNameLabel.text = [[self.detailItem valueForKey:#"courseName"] description];
self.creditHoursLabel.text = [[self.detailItem valueForKey:#"creditHours"] description];
self.preRequisitesLabel.text = [[self.detailItem valueForKey:#"preRequisites"] description];
self.coursePriceLabel.text = [[self.detailItem valueForKey:#"coursePrice"] description];
self.courseEPPLabel.text = [[self.detailItem valueForKey:#"courseEPP"] description];
self.courseDetailLabel.text = [[self.detailItem valueForKey:#"courseDetails"] description];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self configureView];
}
Notice that the segue working good in the normal way.
I think currently your segue links a tableview cell (that prototype) with your detail controller. You can try instead link the tableview controller itself (i.e. the entire thing) with your detail controller. (just ctrl drag from the bottom of your tableview controller to do that)
Then you handle your -prepareForSegue method as usual, but you will need to call -performSegue to actually trigger the segue. Tapping on the cell will have no effect. So in your -tableView:tableViewaccessory.... method you can call performSegue to do your navigation, and handle your didSelectRow.. method as you wish.

Resources