I know it's a commom problem but the cellForRowAtIndexPath: is not called when I use [actionTableView reloadData];. I already linked dataSource and delegate thanks to the storyboard but it doesn't solve the issue.
Here's a part of my code:
SearchViewController.h
#import <UIKit/UIKit.h>
#interface SearchViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate, UISearchDisplayDelegate>
#property (strong, nonatomic) IBOutlet UITableView *actionTableView;
#property (strong, nonatomic) IBOutlet UISearchBar *actionSearchBar;
#property (strong, nonatomic) NSMutableArray *actionsArray;
#property (strong, nonatomic) NSMutableArray *filteredActionArray;
#end
SearchViewController.m
#import "SearchViewController.h"
#import "Action.h"
#interface SearchViewController ()
#end
#implementation SearchViewController
#synthesize actionsArray, actionTableView, filteredActionArray, actionSearchBar;
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [filteredActionArray count];
} else {
return [actionsArray 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];
}
NSLog(#"Aloha");
// Create a new Action object
Action *a = nil;
if (tableView == self.searchDisplayController.searchResultsTableView) {
a = [filteredActionArray objectAtIndex:indexPath.row];
} else {
a = [actionsArray objectAtIndex:indexPath.row];
}
// Configure the cell
cell.textLabel.text = a.nomAction;
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
return cell;
}
-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
// Update the filtered array based on the search text and scope.
// Remove all objects from the filtered search array
[self.filteredActionArray removeAllObjects];
// Filter the array using NSPredicate
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF.nomAction contains[c] %#",searchText];
filteredActionArray = [NSMutableArray arrayWithArray:[actionsArray filteredArrayUsingPredicate:predicate]];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
// Tells the table data source to reload when text changes
[self filterContentForSearchText:searchString scope:
[[self.searchDisplayController.searchBar scopeButtonTitles]objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption {
// Tells the table data source to reload when scope bar selection changes
[self filterContentForSearchText:self.searchDisplayController.searchBar.text scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
As you see I also use a searchBar to filter the array, but w/o this it doesn't work either.
EDIT: SPECIFICATION OF THE PROBLEM: reloadData works only if I call it within the viewController. Not if I call it from another viewController. Obviously for the two cases I call the same function updatingTableView located in the viewcontroller where the tableView is. Any ideas to reload the tableView from anotherViewController?
SearchViewController.m (works)
-(IBAction)update{
[self updatingTableView:nil];
}
-(void)updatingTableView:(NSData*)someData {
actionsArray = [NSArray arrayWithObjects:#"item1", #"item2", #"item3", nil];
[actionTableView reloadData];
}
AnotherViewController.m (does not work)
-(IBAction)updateFromElsewhere{
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main"
bundle: nil];
SearchViewController *searchViewController = (SearchViewController*) [mainStoryboard
instantiateViewControllerWithIdentifier: #"searchcontroller_id"];
[searchViewController updatingTableView:nil];
}
NB: I can pass some data from a view controller to the other without problem here.
One thing I always run into when struggle up a table view is connecting the table with the interface using interface builder. But when you call [actionTableView reloadData] without having the connection made to the table, nothing will happen.
I ways forget that super simple step. Hope this helped
The problem is you never initialize actionsArray and filteredActionArray to be non-nil. As a result, numberOfRowsInSection always returns 0, so cellForRowAtIndexPath won't be called.
Finally found the solution!
In SearchViewController.m where the tableView is, put this in viewDidLoad
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(updatingTableView) name:#"reload_data" object:nil];
With still:
-(void)updatingTableView{
[actionTableView reloadData];
}
In AnotherViewController.m
[[NSNotificationCenter defaultCenter] postNotificationName:#"reload_data" object:self];
how to reload tableview of another uiviewcontroller in current viewcontroller
Thanks everyone!
Related
I have 2 viewcontrollers - one is a tableview and the other is a normal viewcontroller. I want pass data from the second viewcontroller to a tableview controller by using delegates. I have created a delegate and delegatemethod in viewcontroller and implemented delegatemethod in the tableview controller. The problem is that I am getting data to the array but tableview is not reloading. Why?
Can anyone help with this problem? Thanks in advance.
#import "TableViewController.h"
#interface TableViewController ()<name>{
NSMutableArray *data;
}
#end
#implementation TableViewController
- (void)viewDidLoad {
[super viewDidLoad];
data = [NSMutableArray array];
[self.tableView reloadData];
}
- (IBAction)callingSecondView:(id)sender {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]];
ViewController *var = [storyboard instantiateViewControllerWithIdentifier:#"vc"];
var.delegate = self;
[self.navigationController pushViewController:var animated:YES];
}
-(void)getdata:(NSString *)name{
[data addObject:name];
[self.tableView reloadData];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [data count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell" forIndexPath:indexPath];
if (cell != nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"cell"];
}
cell.textLabel.text = [data objectAtIndex:indexPath.row];
return cell;
}
And I am creating delegate a object and protocol in ViewController.h
#import <UIKit/UIKit.h>
#protocol name <NSObject>
-(void)getdata : (NSString *)name;
#end
#interface ViewController : UIViewController
#property (strong, nonatomic) IBOutlet UITextField *txt;
- (IBAction)done:(id)sender;
#property(nonatomic,retain) id<name> delegate;
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize delegate;
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)done:(id)sender {
[delegate getdata:self.txt.text];
[self.navigationController popToRootViewControllerAnimated:YES];
}
#end
I think the first VC don't reloadData because it's not the visible VC. Try reloadData when the VC willAppear.
-(void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.tableView reloadData];
}
My Question is this. Why is the filtered table view showing rooms, after I start typing in the search bar that are not present when printing the filtered array to the console. I thought it might be that the table view cells are being reused so set the cell label text to nil to ensure the text gets reset in tableviewcellforindex: method , all to no avail. Can anyone help?
This is my Table View Controller that acts as the data source and delegate for for both my standard tableview and filtered tableview
#import <UIKit/UIKit.h>
#import "Rooms.h"
#interface RoomsTableViewController : UITableViewController
<UISearchControllerDelegate,UISearchResultsUpdating,UISearchBarDelegate,UITableViewDelegate>
#property (strong,nonatomic) NSMutableArray *roomList;
#property (strong,nonatomic) NSMutableArray *orderedRoomList;
#property (strong,nonatomic) NSMutableArray *filteredRooms;
#property (strong,nonatomic) UITableViewController *searchResultsController;
#property (strong,nonatomic) UISearchController *searchController;
#end
#import "RoomsTableViewController.h"
#interface RoomsTableViewController ()
#property BOOL searchControllerWasActive;
#property BOOL searchControllerSearchFieldWasFirstResponder;
#end
The implementation file
#implementation RoomsTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
//sort the room list into filetred by alpha numeric A10 before C1 example this will eventually be done on the server
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:#"name" ascending:YES comparator:^NSComparisonResult(id obj1, id obj2) {
return [(NSString *)obj1 compare:(NSString *)obj2 options:NSNumericSearch];
}];
self.orderedRoomList = (NSMutableArray*)[self.roomList sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];
self.searchResultsController= [[UITableViewController alloc]initWithStyle:UITableViewStylePlain];
self.searchController = [[UISearchController alloc]initWithSearchResultsController:self.searchResultsController];
self.searchController.searchResultsUpdater=self;
[self.searchController.searchBar sizeToFit];
self.tableView.tableHeaderView=self.searchController.searchBar;
//set up the data source and delegate of this new table view to be this (roomsTableviewcontroller) view controller
self.searchResultsController.tableView.delegate=self;
self.searchResultsController.tableView.dataSource=self;
self.searchController.delegate=self;
self.searchController.dimsBackgroundDuringPresentation=NO;
self.searchController.searchBar.delegate=self;
self.definesPresentationContext=YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
//restore the search controllers active state
if (self.searchControllerWasActive) {
self.searchController.active = self.searchControllerWasActive;
_searchControllerWasActive=NO;
if (self.searchControllerSearchFieldWasFirstResponder) {
[self.searchController.searchBar becomeFirstResponder];
_searchControllerSearchFieldWasFirstResponder=NO;
}
}
}
-(void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
}
#pragma mark - UISearchControllerDelegate
- (void)willPresentSearchController:(UISearchController *)searchController {
// do something before the search controller is presented
self.navigationController.navigationBar.translucent = YES;
}
-(void)willDismissSearchController:(UISearchController *)searchController
{
self.navigationController.navigationBar.translucent = NO;
}
#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.orderedRoomList count];
}else{
return [self.filteredRooms count];
}
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 71;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellReuseIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellReuseIdentifier];
if (cell==nil) {
cell= [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellReuseIdentifier];
}
Rooms *room = (tableView==self.tableView)? [self.orderedRoomList objectAtIndex:indexPath.row]: [self.filteredRooms objectAtIndex:indexPath.row];
cell.textLabel.text=nil;
NSString *labelString = [NSString stringWithFormat:#"%# %#",room.name,room.roomDescription];
cell.textLabel.text=labelString;
return cell;
}
Below is a photo of the table view in the simulator and a screen print of the check to see if the filtered array has indeed worked correctly as it has. You can see that C3 is not listed as being part of the filtered array but it still appears on screen.
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.
I'm creating an iPad app. The root UITableview has a right bar button item in the navigation controller. When you tap the button, it shows a pop over controller. The popover is a UITableViewController. When you tap a cell in the popover, how could I pass the data in that cell and insert it into a cell into the root UITableview? I searched the Apple docs and couldn't find what I needed. Can anyone push me in the right direction?
Roottable.h
#interface Roottable : UITableViewController<PopoverDelegate>
Popover.h
#protocol AthleteSelectPopoverDelegate <NSObject>
#required
-(void)selectedObject:(Object *)newObject;
#end
#property (nonatomic, weak) id<PopoverDelegate> delegate;
#property (readwrite, nonatomic) Object *currentObject;
#end
popover.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
_currentObject = [_objectArray objectAtIndex:indexPath.row];
//Notify the delegate if it exists.
if (_delegate != nil) {
[_delegate selectedObject:_currentObject];
}
}
You add data from the selected cell to the main table's data source delegate.
Then that data source should tell the main table that a cell has been inserted at an index path.
I figured it out. Hope I help someone. I'll explain the code first then post it below. Basically, I set the data source of the root table view, "ObjectSelect", as a NSMutableArray called "currentObjectArray". ObjectSelect is also the ObjectSelectPopoverDelegate. Basically, when a cell in the popover is tapped, it adds the object tapped to the "currentObjectArray" and reloads the tableview.
ObjectSelect.h
#import <UIKit/UIKit.h>
#import "ObjectSelectPopover.h"
#interface ObjectSelect : UITableViewController<ObjectSelectPopoverDelegate>
#property (nonatomic, strong) ObjectSelectPopover *objectPicker;
#property (nonatomic, strong) UIPopoverController *objectPickerPopover;
#property (readwrite, nonatomic) Object *currentObject;
#property (nonatomic, strong) NSMutableArray *selectedObjectArray;
#end
ObjectSelect.m
-(void)selectedObject:(Object *)newObject
{
_currentObject = newObject;
if(!_selectedObjectArray){
_selectedObjectArray = [[NSMutableArray alloc] init];
}
if([_selectedObjectArray containsObject:_currentAthlete]){
//lol you don't get added, bub
}
else{
[_selectedObjectArray addObject:_currentObject];
}
[self.tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
Object *objectTapped = (Object *)[_objectAthleteArray objectAtIndex:indexPath.row];
return cell;
}
ObjectSelectPopover.h
#import <UIKit/UIKit.h>
#import "Object.h"
#protocol ObjectSelectPopoverDelegate <NSObject>
#required
-(void)selectedObject:(Object *)newObject;
#end
#interface ObjectSelectPopover : UITableViewController
#property (nonatomic, weak) id<ObjectSelectPopoverDelegate> delegate;
#property (nonatomic, strong) NSMutableArray *objectArray;
#property (readwrite, nonatomic) Object *currentObject;
#end
ObjectSelectPopover.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
_currentObject = [_objectArray objectAtIndex:indexPath.row];
//Notify the delegate if it exists.
if (_delegate != nil) {
[_delegate selectedObject:_currentObject];
}
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}
I think you should have a property with a name other than delegate in your popover controller since UITableViewController already has a delegate property for the UITableViewDelegate protocol; maybe masterTable or something.
Then in the selectedObject: implementation in the root UITableView you can do an insert row or add it to the data array and reload the table.
Oops, my bad... #geraldWilliam is right, UITableViewController does not have the delegate property...
What you have seems like it should work... So does the selectedObject: method get called in the delegate? If so, what do you do in that method? If you add the object to the data set (array or dictionary or database) for the root view, insert a row in its tableview (or reload the data), it should work.
Here is some code that works for me. It is not from a popover but from a pushed view but there is no reason that should make a difference:
- (ThingStatus) thingPicker: (ThingPickerTableViewController *) thingPicker didSelectThing: (Thing *) thing {
NSLog( #"Entering %s", __func__ );
// Dismiss the pushed view controller (for you, the popover)
[self.navigationController popViewControllerAnimated: YES];
NSArray *startingList = self.currentCellObjectList;
[self.databaseManager addThing: thing];
NSArray *endingList = self.databaseManager.thingsForTableView;
// Figure out the differences adding made...
DiffResult *changes = [startingList simpleDiffWithArray: endingList];
NSLog( #"%d deletions, %d insertions", changes.deletionCount, changes.insertionCount );
// I only handle insertions in this code... deletions would be similar
__block NSUInteger objIdx = 0;
NSMutableArray *changeableThingList = [startingList mutableCopy];
[changes.insertionIndexes enumerateIndexesUsingBlock: ^( NSUInteger idx, BOOL *stop ) {
NSLog( #" - insert %# at %d", [[changes.insertionObjects objectAtIndex: objIdx] name], idx );
NSIndexPath *indexPath = [NSIndexPath indexPathForRow: idx inSection: 0];
[changeableThingList insertObject: [changes.insertionObjects objectAtIndex: objIdx] atIndex: idx];
self.currentCellObjectList = changeableThingList;
[self.tableView insertRowsAtIndexPaths: [NSArray arrayWithObject: indexPath] withRowAnimation: UITableViewRowAnimationRight];
++objIdx;
}];
[self.databaseManager save];
return [self.databaseManager: thingStatus];
}
Here is some good code to use that may be able to help you.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return self.item.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
//Get the row
Sport *rowSport = self.sports[indexPath.row];
cell.textLabel.text = rowItem.itemName;
cell.detailTextLabel.text = rowItem.section;
return cell;
}
I hope this will help you.
I am trying to link from a dynamic table view cell (as part of a search result table) to a specific view controller
The code I have implemented so far is:
SearchViewController.h
import <UIKit/UIKit.h>
#interface SearchViewController : UITableViewController <UISearchDisplayDelegate, UISearchDisplayDelegate>
#property (strong,nonatomic) NSArray *sysTArray;
#property (strong,nonatomic) NSMutableArray *filteredsysTArry;
#property IBOutlet UISearchBar *sysTSearchBar;
#end
SearchViewController.M
#import "SearchViewController.h"
#import "sysT.h"
#interface SearchViewController ()
#end
#implementation SearchViewController
#synthesize sysTArray;
#synthesize filteredsysTArry;
#synthesize sysTSearchBar;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
sysTArray = [NSArray arrayWithObjects:
[sysT sysTOfCategory:#"p" name:#"H1"],
[sysT sysTOfCategory:#"p" name:#"W2"],
[sysT sysTOfCategory:#"p" name:#"W3"],
[sysT sysTtOfCategory:#"p" name:#"C4"],
[sysT sysTOfCategory:#"c" name:#"O5"],
[sysT sysTOfCategory:#"c" name:#"C6"],
[sysT sysTOfCategory:#"a" name:#"L7"], nil];
self.filteredSysTArry = [NSMutableArray arrayWithCapacity:[sysTArray count]];
[self.tableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [filteredsysTArry count];
}else{
return [sysTArray count];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (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];
}
SysT *sysT = nil;
if (tableView == self.searchDisplayController.searchResultsTableView) {
sysT = [filteredsysTArry objectAtIndex:indexPath.row];
}else{
sysT = [sysTArray objectAtIndex:indexPath.row];
}
cell.textLabel.text = sysT.name;
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
return cell;
}
#pragma mark Search Filtering
-(void)filterContentForSearchText:(NSString*) searchText scope:(NSString*)scope {
[self.filteredSysTArry removeAllObjects];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF.name contains[c] %#", searchText];
filteredSysTArry = [NSMutableArray arrayWithArray:[sysTArray filteredArrayUsingPredicate:predicate]];
}
#pragma mark - UISearchDisplayController Delegate Methods
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
[self filterContentForSearchText:searchString scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex: [self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
return YES;
}
-(BOOL) searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption {
[self filterContentForSearchText:self.searchDisplayController.searchBar.text scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
return YES;}
#end
How do I initiate a specific view controller depending on the data inside the dynamic cell?
To further elaborate, if a user searched H1, and then clicked on that dynamic cell, how would I display the relevant H1 view controller?
As you can probably tell from my very rough code, I'm on a steep learning curve. If you could make your answers as baby proof as possible that would be fantastic, and would really help me out. (Also, I am using storyboards).
Thanks!
You need to implement tableView:didSelectRowAtIndexPath: which is called when you select a row. You can get the data for that row by querying your data source, using the indexPath passed into that method. You can then use whatever logic you need to choose which view controller to go to next. You do that by calling performSegueWithIdentifier.