iOS - Why my next view callback my previous view? - ios

My situation is the next :
I have two view controller.
The first contains an UITableView. When I tap on a cell, the second view is called and display another UITableView.
Problem :
Immediately that my second view has been displayed, it's removed and my app come back on the first view.
In debug mode, I can see that viewDidLoad() is called and the TableView has been initialised because the TableView is filled.
But I don't know why, viewWillDisappear() is called immediately, as if the view was removed...
Could someone to help me out please?
EDIT :
My first View :
#import "CIMSaddlesResearchesSavedViewController.h"
#import <Foundation/Foundation.h>
#interface CIMSaddlesResearchesSavedViewController ()
#property (weak, nonatomic) IBOutlet UITableView *tableView;
#end
#implementation CIMSaddlesResearchesSavedViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
}
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"NavigationBarBackground"] forBarMetrics:UIBarMetricsDefault];
}
- (void) viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
}
- (UIStatusBarStyle) preferredStatusBarStyle {
return UIStatusBarStyleLightContent;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.researches count];
}
- (UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
static NSString *reuseIdentifier = #"researchCell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
CIMSaddleResearch *research;
research = self.researches[indexPath.row];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"researchCell"];
}
cell.textLabel.textColor = [UIColor whiteColor];
cell.detailTextLabel.textColor = [UIColor whiteColor];
cell.backgroundColor = [UIColor clearColor];
if (research.dbId && ![research.dbId isEqual:#""]) {
CIMDbManager *dbMngr = [[CIMDbManager alloc] init];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd/MM/yyyy"];
NSMutableString *minPrice = [NSMutableString stringWithString:#""];
NSMutableString *maxPrice = [NSMutableString stringWithString:#""];
NSMutableString *currencyS = [NSMutableString stringWithString:#""];
CIMCurrency *currency = [[CIMCurrency alloc] init];;
NSMutableString *researchText = [NSMutableString stringWithString:#""];
if (research.customer && ![research.customer isEqual:#""]) {
CIMCustomer *customer = [[CIMCustomer alloc] init];
customer.dbId = research.customer;
customer = [dbMngr firstObjectFromDb:customer];
[researchText appendFormat:#"%# le %#", customer.lastName, [formatter stringFromDate:research.date]];
}
CIMSaddleResearchLine *researchLine = [[CIMSaddleResearchLine alloc] init];
researchLine.saddleResearch = research.dbId;
NSArray *lines = [dbMngr objectsFromDb:researchLine];
for(CIMSaddleResearchLine *line in lines){
if([line.field isEqualToString:#"PRIX_MIN"])
[minPrice appendFormat:#"%#", line.value];
else if([line.field isEqualToString:#"PRIX_MAX"])
[maxPrice appendFormat:#"%#", line.value];
else if([line.field isEqualToString:#"DEVISE"])
[currencyS appendFormat:#"%#", line.value];
}
if(![minPrice isEqual:#""] || ![maxPrice isEqual:#""]){
if(currencyS){
currency.dbId = currencyS;
currency = [dbMngr firstObjectFromDb:currency];
} else {
currency = [dbMngr defaultSocietyCurrency];
}
[researchText appendString:#" | Budget : "];
if(![minPrice isEqual:#""] && ![maxPrice isEqual:#""])
[researchText appendFormat:#"%#%# → %#%#", minPrice, currency.symbol, maxPrice, currency.symbol];
else if(![minPrice isEqual:#""] && [maxPrice isEqual:#""])
[researchText appendFormat:#"%#%# min.", minPrice, currency.symbol];
else if([minPrice isEqual:#""] && ![maxPrice isEqual:#""])
[researchText appendFormat:#"%#%# max.", maxPrice, currency.symbol];
}
NSMutableString *researchDetailText = [NSMutableString stringWithFormat:#"Relance le : %#", [formatter stringFromDate:research.deadline]];
CIMResearchStatus *status = [[CIMResearchStatus alloc] init];
status.dbId = research.status;
status = [dbMngr firstObjectFromDb:status];
if(status){
[researchDetailText appendFormat:#" (%#)", status.name];
}
cell.textLabel.text = researchText;
cell.detailTextLabel.text = researchDetailText;
if (research.comment && ![research.comment isEqual:#""] ) {
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
}
} else {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"saddleCell"];
cell.backgroundColor = [UIColor clearColor];
cell.textLabel.text = NSLocalizedString(#"The saddle is not in this list", nil);
cell.textLabel.textAlignment = NSTextAlignmentCenter;
}
return cell;
}
-(void) tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
CIMSaddleResearch *research = self.researches[indexPath.row];
if (research.comment && ![research.comment isEqual:#""] ) {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(#"Comment", nil)
message:research.comment
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *actionOk = [UIAlertAction actionWithTitle:NSLocalizedString(#"Ok", nil)
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action){}];
[alert addAction:actionOk];
[self presentViewController:alert animated:YES completion:nil];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.navigationController popViewControllerAnimated:YES];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#pragma mark - Navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"openResultResearch"]) {
UITableView *table = (UITableView*)[(UITableViewCell*)sender superview];
NSIndexPath *index = [table indexPathForCell:(UITableViewCell*) sender];
CIMSaddleResearch *research = self.researches[index.row];
CIMDbManager *dbMngr = [[CIMDbManager alloc] init];
CIMSaddleResearchLine *researchLine = [[CIMSaddleResearchLine alloc] init];
CIMSaddleResearchFormViewController *form = [[CIMSaddleResearchFormViewController alloc] init];
form.minimumPriceRow = [[XLFormRowDescriptor alloc] init];
form.maximumPriceRow = [[XLFormRowDescriptor alloc] init];
form.minimumYearRow = [[XLFormRowDescriptor alloc] init];
form.maximumYearRow = [[XLFormRowDescriptor alloc] init];
form.currency = [[CIMCurrency alloc] init];
NSMutableDictionary *filters = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
[[NSMutableArray alloc] init], #"brand",
[[NSMutableArray alloc] init], #"model",
[[NSMutableArray alloc] init], #"size",
[[NSMutableArray alloc] init], #"color",
[[NSMutableArray alloc] init], #"finishing",
nil];
researchLine.saddleResearch = research.dbId;
NSArray *lines = [dbMngr objectsFromDb:researchLine];
NSSortDescriptor *sortFieldDesc = [[NSSortDescriptor alloc] initWithKey:#"field" ascending:YES selector:#selector(compare:)];
NSSortDescriptor *sortOrderDesc = [[NSSortDescriptor alloc] initWithKey:#"order" ascending:YES selector:#selector(compare:)];
lines = [lines sortedArrayUsingDescriptors:#[sortFieldDesc, sortOrderDesc]];
for(CIMSaddleResearchLine *line in lines){
if([line.field isEqualToString:#"PRIX_MIN"]){
form.minimumPriceRow.value = line.value;
}else if([line.field isEqualToString:#"PRIX_MAX"]){
form.maximumPriceRow.value = line.value;
}else if([line.field isEqualToString:#"ANNEE_MIN"]){
form.minimumYearRow.value = line.value;
}else if([line.field isEqualToString:#"ANNEE_MAX"]){
form.maximumYearRow.value = line.value;
}else if([line.field isEqualToString:#"DEVISE"]){
form.currency.dbId = line.value;
form.currency = [dbMngr firstObjectFromDb:form.currency];
}
else if([line.field isEqualToString:#"GA_REFCONSTRUC"]){
form.serialNumberRow.value = line.value;
}else if([line.field isEqualToString:#"GA_QUARTIER"]){
form.flapRow.value = line.value;
}else if([line.field isEqualToString:#"GA_MARQUE"]){
CIMBrand *brand = [[CIMBrand alloc] init];
brand.dbId = line.value;
[[filters objectForKey:#"brand"] addObject:[dbMngr firstObjectFromDb:brand]];
}else if([line.field isEqualToString:#"GA_MODEL"]){
CIMModel *model = [[CIMModel alloc] init];
model.dbId = line.value;
[[filters objectForKey:#"model"] addObject:[dbMngr firstObjectFromDb:model]];
}else if([line.field isEqualToString:#"GA_TAILLE"]){
CIMSize *size = [[CIMSize alloc] init];
size.dbId = line.value;
[[filters objectForKey:#"size"] addObject:[dbMngr firstObjectFromDb:size]];
}else if([line.field isEqualToString:#"GA_COULEUR"]){
CIMColor *color = [[CIMColor alloc] init];
color.dbId = line.value;
[[filters objectForKey:#"color"] addObject:[dbMngr firstObjectFromDb:color]];
}else if([line.field isEqualToString:#"GA_FINITION"]){
CIMFinishing *finishing = [[CIMFinishing alloc] init];
finishing.dbId = line.value;
[[filters objectForKey:#"finishing"] addObject:[dbMngr firstObjectFromDb:finishing]];
}
}
if(!form.currency.dbId && [form.currency.dbId isEqualToString:#""]){
CIMCustomer *customer = [[CIMCustomer alloc] init];
customer.dbId = research.customer;
customer = [dbMngr firstObjectFromDb:customer];
form.currency.dbId = customer.currency;
form.currency = [dbMngr firstObjectFromDb:form.currency];
}
CIMSaddlesViewController *saddlesViewController = segue.destinationViewController;
saddlesViewController.filters = filters;
saddlesViewController.saddleDbId = [NSMutableString stringWithString:#""];
saddlesViewController.selectionMode = NO;
saddlesViewController.saddleNotFoundOption = NO;
saddlesViewController.showSaddlePictures = YES;
saddlesViewController.showSaddleWarehouse = YES;
saddlesViewController.showToolbar = NO;
saddlesViewController.saddles = [dbMngr stockSaddlesWithFilters:filters
andSerialNumber:form.serialNumberRow.value
andFlap:form.flapRow.value
betweenMinimumPrice:form.minimumPriceRow.value
andMaximumPrice:form.maximumPriceRow.value
inCurrency:form.currency.dbId
betweenMinimumYear:form.minimumYearRow.value
andMaximumYear:form.maximumYearRow.value
inStock:YES];
saddlesViewController.formResearch = form;
saddlesViewController.saddlesPricesCurrency = form.currency;
}
}
#end
My second view :
#import "CIMSaddlesViewController.h"
#interface CIMSaddlesViewController ()
#property (weak, nonatomic) IBOutlet UITableView *tableView;
#end
#implementation CIMSaddlesViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
}
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"NavigationBarBackground"] forBarMetrics:UIBarMetricsDefault];
}
- (void) viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
}
- (UIStatusBarStyle) preferredStatusBarStyle {
return UIStatusBarStyleLightContent;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.saddles count];
}
- (UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
static NSString *reuseIdentifier = #"saddleCell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
CIMItem *saddle;
saddle = self.saddles[indexPath.row];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"saddleCell"];
}
cell.textLabel.textColor = [UIColor whiteColor];
cell.detailTextLabel.textColor = [UIColor whiteColor];
cell.backgroundColor = [UIColor clearColor];
if (saddle.dbId && ![saddle.dbId isEqual:#""]) {
NSMutableString *saddleText = [NSMutableString stringWithString:saddle.dbId];
NSMutableString *saddleDetailText = [NSMutableString stringWithString:saddle.name];
// Serial number
if (saddle.serialNumber && ![saddle.serialNumber isEqual:#""]) {
[saddleText appendFormat:#" - %#", saddle.serialNumber];
}
// Warehouse
if (self.showSaddleWarehouse) {
CIMDbManager *dbMngr = [[CIMDbManager alloc] init];
CIMStock *stock = [[CIMStock alloc] init];
stock.item = saddle.dbId;
stock.quantity = [NSNumber numberWithInt:1];
stock = [dbMngr firstObjectFromDb:stock];
if (stock) {
CIMWarehouse *warehouse = [[CIMWarehouse alloc] init];
warehouse.dbId = stock.warehouse;
warehouse = [dbMngr firstObjectFromDb:warehouse];
[saddleText appendFormat:#" → [%#]", warehouse.name];
}
}
// Estimed price
if (self.saddlesPricesCurrency) {
CIMDbManager *dbMngr = [[CIMDbManager alloc] init];
CIMSaddlePrices *saddlePrices = [[CIMSaddlePrices alloc] init];
saddlePrices.item = saddle.dbId;
saddlePrices.currency = self.saddlesPricesCurrency.dbId;
saddlePrices = [dbMngr firstObjectFromDb:saddlePrices];
if (saddlePrices) {
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setMaximumFractionDigits:0];
[numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[numberFormatter setCurrencyCode:self.saddlesPricesCurrency.isoCode];
NSString *isEstimateStr;
if ([saddlePrices.isEstimate isEqual:#"-"]) {
isEstimateStr = NSLocalizedString(#"Official price", nil);
} else {
isEstimateStr = NSLocalizedString(#"Estimation", nil);
}
[saddleText appendFormat:#" | %# (%#)", [numberFormatter stringFromNumber:saddlePrices.price], isEstimateStr];
}
}
cell.textLabel.text = saddleText;
cell.detailTextLabel.text = saddleDetailText;
// Saddle pictures
if (self.showSaddlePictures && [[self.saddlesPicturesDictionary allKeys] containsObject:saddle.dbId]) {
UIButton *accessoryButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 40.0f, 40.0f)];
if ([self.saddlesiPadPictures containsObject:saddle.dbId]) {
[accessoryButton setImage:[UIImage imageNamed:#"SaddleiPadPictureButton"] forState:UIControlStateNormal];
} else {
[accessoryButton setImage:[UIImage imageNamed:#"SaddlePictureButton"] forState:UIControlStateNormal];
}
accessoryButton.tag = indexPath.row;
[accessoryButton addTarget:self action:#selector(showSaddlePicturesViewForSaddleWithSender:) forControlEvents:UIControlEventTouchDown];
cell.accessoryView = accessoryButton;
} else {
cell.accessoryView = nil;
}
} else {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"saddleCell"];
cell.backgroundColor = [UIColor whiteColor];
cell.textLabel.text = NSLocalizedString(#"The saddle is not in this list", nil);
cell.textLabel.textAlignment = NSTextAlignmentCenter;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.selectionMode) {
CIMItem *saddle = self.saddles[indexPath.row];
[self.saddleDbId setString:saddle.dbId];
[self.navigationController popViewControllerAnimated:YES];
} else {
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}
}
#end
To be honest, this project is very big, I begun to work on it since 2 month ago and I'm alone to work on it. I didn't work in Objective-C before and even less with X-Code. I'm discovering features and Objective-C gradually...

In your first VC you call [self.navigationController popViewControllerAnimated:YES]; which tells the navigationVC to pop the currently visible view controller. If you set breakpoints to the tableview didSelectRowAtIndexPath: in first VC and to viewDidLoad in your second VC you'll see in what order they are called. I have a feeling that the order of execution could be the following:
prepare for segue in firstVC
view did load in secondVC
didSelectRowAtIndexPath in first VC
finally your second VC unloads
This is my idea without testing it. Please make sure you really need to pop the VC in didSelectRow

Related

SearchBar and Mysql

New to iOS...i am trying to do a search with a downloaded NSMutableArray table using searchbar or do need to get search from mysql table...its driving me nuts here is code below...any help would be appreciated
#import "VendorViewController.h"
#import "VendLocation.h"
#import "VendorDetailController.h"
#interface VendorViewController ()
{
VendorModel *_VendorModel; NSMutableArray *_feedItems; VendLocation *_selectedLocation; UIRefreshControl *refreshControl;
}
#property (nonatomic, weak) IBOutlet UISearchBar *searchBar;
#end
#implementation VendorViewController
//#synthesize item;
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Vendors";
self.searchBar.hidden = YES;
self.searchBar.delegate = self;
self.searchBar.barTintColor = [UIColor clearColor];
//self.searchBar.barTintColor = [UIColor orangeColor];
_feedItems = [[NSMutableArray alloc] init];
_VendorModel = [[VendorModel alloc] init];
_VendorModel.delegate = self;
[_VendorModel downloadItems];
#pragma mark Bar Button
UIBarButtonItem *searchItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:#selector(searchButton:)];
NSArray *actionButtonItems = #[searchItem];
self.navigationItem.rightBarButtonItems = actionButtonItems;
#pragma mark Table Refresh
UIView *refreshView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
[self.tableView insertSubview:refreshView atIndex:0]; //the tableView is a IBOutlet
refreshControl = [[UIRefreshControl alloc] init];
refreshControl.backgroundColor = [UIColor grayColor];
refreshControl.tintColor = [UIColor whiteColor];
[refreshControl addTarget:self action:#selector(reloadDatas) forControlEvents:UIControlEventValueChanged];
NSMutableAttributedString *refreshString = [[NSMutableAttributedString alloc] initWithString:#"Refreshing"];
//add date to refresh
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MMM d, h:mm a"];
NSString *lastUpdated = [NSString stringWithFormat:#"Last updated on %#", [formatter stringFromDate:[NSDate date]]];
refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:lastUpdated];
[refreshString addAttributes:#{NSForegroundColorAttributeName : [UIColor whiteColor]} range:NSMakeRange(0, refreshString.length)];
[refreshView addSubview:refreshControl];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
**#pragma mark - Search
- (void)searchButton:(id)sender{
[self.searchBar becomeFirstResponder];
self.searchBar.hidden = NO;
//[self.listTableView resignFirstResponder];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
self.searchBar.hidden = YES;
}
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if(searchText.length == 0)
{
isFilltered = NO;
} else {
isFilltered = YES;
filteredString = [[NSMutableArray alloc]init];
for(NSString *vendorName in _feedItems)
{
NSRange vendorNameRange = [vendorName rangeOfString:searchText options:NSCaseInsensitiveSearch];
if(vendorNameRange.location != NSNotFound) {
[filteredString addObject:vendorName];
}
}
}
[self.listTableView reloadData];
}**
#pragma mark - Table
-(void)reloadDatas {
[refreshControl endRefreshing];
}
-(void)itemsDownloaded:(NSMutableArray *)items
{ // This delegate method will get called when the items are finished downloading
_feedItems = items;
[self.listTableView reloadData];
}
#pragma mark Table Delete Button
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleDelete;
}
- (void) setEditing:(BOOL)editing
animated:(BOOL)animated{
[super setEditing:editing
animated:animated];
[self.listTableView setEditing:editing
animated:animated];
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[_feedItems removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:#[indexPath]
withRowAnimation:UITableViewRowAnimationLeft];
[self.tableView reloadData];
/*
NSError *error = nil;
if (![tableView save:&error]) {
NSLog(#"Can't Delete! %# %#", error, [error localizedDescription]);
return;
} */
}
}
#pragma mark TableView Delegate Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (isFilltered) {
return [filteredString count];
}
return _feedItems.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
VendLocation *item = _feedItems[indexPath.row];
static NSString *CellIdentifier = #"BasicCell";
UITableViewCell *myCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
myCell.layer.cornerRadius = 5;
myCell.layer.masksToBounds = YES;
if (myCell == nil) {
myCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; }
if (!isFilltered) {
myCell.textLabel.text = item.vendorName;
myCell.detailTextLabel.text = item.vendorNo;
//Retreive an image
UIImage *myImage = [UIImage imageNamed:#"DemoCellImage"];
[myCell.imageView setImage:myImage];
} else {
myCell.textLabel.text = [filteredString objectAtIndex:indexPath.row];
myCell.detailTextLabel.text = nil;
}
return myCell;
}
#pragma mark Tableheader
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 55.0;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
NSString *newString = [NSString stringWithFormat:#"VENDOR \n%lu", (unsigned long) _feedItems.count];
NSString *newString1 = [NSString stringWithFormat:#"NASDAQ \n4,727.35"];
NSString *newString2 = [NSString stringWithFormat:#"DOW \n17,776.80"];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 0)];
tableView.tableHeaderView = view; //makes header move with tablecell
[view setBackgroundColor:[UIColor clearColor]];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(12, 3, tableView.frame.size.width, 45)];
[label setFont:[UIFont systemFontOfSize:12]];
[label setTextColor:[UIColor whiteColor]];
label.numberOfLines = 0;
NSString *string = newString;
[label setText:string];
[view addSubview:label];
UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(12, 45, 60, 1.5)];
separatorLineView.backgroundColor = [UIColor redColor];
[view addSubview:separatorLineView];
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(85, 3, tableView.frame.size.width, 45)];
label1.numberOfLines = 0;
[label1 setFont:[UIFont systemFontOfSize:12]];
[label1 setTextColor:[UIColor whiteColor]];
NSString *string1 = newString1;
[label1 setText:string1];
[view addSubview:label1];
UIView* separatorLineView1 = [[UIView alloc] initWithFrame:CGRectMake(85, 45, 60, 1.5)];
separatorLineView1.backgroundColor = [UIColor redColor];
[view addSubview:separatorLineView1];
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(158, 3, tableView.frame.size.width, 45)];
label2.numberOfLines = 0;
[label2 setFont:[UIFont systemFontOfSize:12]];
[label2 setTextColor:[UIColor whiteColor]];
NSString *string2 = newString2;
[label2 setText:string2];
[view addSubview:label2];
UIView* separatorLineView2 = [[UIView alloc] initWithFrame:CGRectMake(158, 45, 60, 1.5)];
separatorLineView2.backgroundColor = [UIColor redColor];
[view addSubview:separatorLineView2];
return view;
}
here is my header
#import <UIKit/UIKit.h>
#import "VendorModel.h"
#interface VendorViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, VendorModelProtocol>
{
// NSMutableArray *_feedItems;
NSMutableArray *filteredString;
BOOL isFilltered;
}
#property (weak, nonatomic) IBOutlet UITableView *listTableView;
#property (weak, nonatomic) IBOutlet UITableView *tableView;
#end
Changed Code to textDidChange
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if(searchText.length == 0)
{
isFilltered = NO;
} else {
isFilltered = YES;
filteredString = [[NSMutableArray alloc]init];
for(VendLocation* vendor in _feedItems)
{
NSRange stringRange = [vendor.vendorName rangeOfString:searchText options:NSCaseInsensitiveSearch];
if(stringRange.location != NSNotFound) {
[filteredString addObject:vendor];
}
}
}
[self.tableView reloadData];
}
i changed filter too this code still no luck hit a key and crash
if(searchText.length == 0)
{
isFilltered = NO;
[filteredString removeAllObjects];
[filteredString addObjectsFromArray:_feedItems];
} else {
isFilltered = YES;
// filteredString = [[NSMutableArray alloc]init];
[filteredString removeAllObjects];
for(NSString* string in _feedItems)
{
NSRange stringRange = [string rangeOfString:searchText options:NSCaseInsensitiveSearch];
if(stringRange.location != NSNotFound) {
[filteredString addObject:string];
}
}
}
[self.tableView reloadData];
If you already have a downloaded NSMutableArray with data in it then your search can consist of merely searching through the array and filtering out entries that do not match. You do not have to go back to MySQL.

UIActivityIndicatorView Not Appearing

I am trying to show a UIActivityIndicatorView while my table view is loading data and have it disappear once loading is finished. The loading indicator never appears. What am I doing wrong?
#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
#define storeURL [NSURL URLWithString: #"https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=25&playlistId=PL9DC706DCCCE00188&key=AIzaSyBS4do208_KPGHAhszfVkHadSvtfSgr7Mo"]
#import "BBYoutubeVideosTableViewController.h"
#import "Reachability.h"
#import "TSMessage.h"
#import "TSMessageView.h"
#import "YoutubeCell.h"
#import "KFBYoutubeVideoView.h"
#import "KFBAppDelegate.h"
#interface BBYoutubeVideosTableViewController ()
{
UIActivityIndicatorView *loadingIndicator;
}
#end
#implementation BBYoutubeVideosTableViewController
#synthesize title, videoID, thumbURL, descriptionString, url, titleArray, videoIDArray, thumbArray, descriptionArray;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"" style:UIBarButtonItemStylePlain target:nil action:nil];
UIImageView *backgroundImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"plain_app-background.png"]];
CGFloat width = [[UIScreen mainScreen]bounds].size.width;
CGFloat height = [[UIScreen mainScreen]bounds].size.height;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
loadingIndicator = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(width / 2, height / 2, 37, 37)];
loadingIndicator.center = CGPointMake(width / 2, height / 2 - 37);
}
else
{
loadingIndicator = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(142, 365, 37, 37)];
}
loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
loadingIndicator.hidesWhenStopped = YES;
Reachability *networkReachability = [Reachability reachabilityForInternetConnection];
NetworkStatus networkStatus = [networkReachability currentReachabilityStatus];
if(networkStatus == NotReachable)
{
[TSMessage showNotificationWithTitle:#"Network Error" subtitle:#"No active network connection!" type:TSMessageNotificationTypeError];
[loadingIndicator stopAnimating];
}
else {
[self.tableView addSubview:loadingIndicator];
[loadingIndicator startAnimating];
}
self.title = #"Bluegrass & Backroads";
self.tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
self.tableView.backgroundView = backgroundImage;
url = [NSURL URLWithString:#"https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=25&playlistId=PL9DC706DCCCE00188&key=AIzaSyBS4do208_KPGHAhszfVkHadSvtfSgr7Mo"];
dispatch_async(kBgQueue, ^{
NSData *data = [NSData dataWithContentsOfURL:url];
if (data == nil)
{
NSLog(#"data is nil");
}
else
{
[self performSelectorOnMainThread:#selector(fetchedData:) withObject:data waitUntilDone:YES];
}
});
}
- (void)viewDidDisappear:(BOOL)animated
{
[loadingIndicator stopAnimating];
}
- (void)fetchedData:(NSData *)responseData
{
NSError *error;
titleArray = [[NSMutableArray alloc]init];
videoIDArray = [[NSMutableArray alloc]init];
thumbArray = [[NSMutableArray alloc]init];
descriptionArray = [[NSMutableArray alloc]init];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
NSArray *items = [json objectForKey:#"items"];
for (NSDictionary *item in items)
{
NSDictionary *snippet = [item objectForKey:#"snippet"];
title = [snippet objectForKey:#"title"];
videoID = [[snippet objectForKey:#"resourceId"] objectForKey:#"videoId"];
thumbURL = [[[snippet objectForKey:#"thumbnails"] objectForKey:#"default"] objectForKey:#"url"];
descriptionString = [snippet objectForKey:#"description"];
[titleArray addObject:title];
[videoIDArray addObject:videoID];
UIImage *thumbnailImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:thumbURL]]];
[thumbArray addObject:thumbnailImage];
[descriptionArray addObject:descriptionString];
}
[self.tableView reloadData];
[loadingIndicator stopAnimating];
}
- (IBAction)morePressed:(id)sender
{
NSURL *kyfbVideos = [NSURL URLWithString:#"https://www.youtube.com/playlist?list=PL9DC706DCCCE00188"];
[[UIApplication sharedApplication] openURL:kyfbVideos];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [titleArray count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 215;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 60;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIColor *kfbBlue = [UIColor colorWithRed:8.0/255.0f green:77.0/255.0f blue:139.0/255.0f alpha:1];
UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, tableView.frame.size.height)];
footerView.backgroundColor = [UIColor clearColor];
CGFloat width = footerView.frame.size.width;
UIButton *moreButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
moreButton.backgroundColor = [UIColor clearColor];
[moreButton setTitle:#"More" forState:UIControlStateNormal];
[moreButton setTitleColor:kfbBlue forState:UIControlStateNormal];
moreButton.titleLabel.textAlignment = NSTextAlignmentCenter;
moreButton.titleLabel.font = [UIFont fontWithName:#"FranklinGothicStd-ExtraCond" size:25.0];
moreButton.frame = CGRectMake(width / 2 - 25, 0, 50, 50);
moreButton.layer.cornerRadius = 25.0;
moreButton.layer.borderWidth = 2.0f;
moreButton.layer.borderColor = kfbBlue.CGColor;
moreButton.clipsToBounds = YES;
moreButton.backgroundColor = [UIColor clearColor];
[moreButton addTarget:self action:#selector(morePressed:) forControlEvents:UIControlEventTouchUpInside];
[footerView addSubview:moreButton];
return footerView;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UIColor *kfbBlue = [UIColor colorWithRed:8.0/255.0f green:77.0/255.0f blue:139.0/255.0f alpha:1];
YoutubeCell *cell = [tableView dequeueReusableCellWithIdentifier:#"youtubeCell"];
if (!cell)
{
NSArray *nibs =[[NSBundle mainBundle] loadNibNamed:#"YoutubeCell" owner:self options:NULL];
cell = [nibs firstObject];
}
cell.videoTitle.text = [titleArray objectAtIndex:indexPath.row];
cell.videoDescription.text = [descriptionArray objectAtIndex:indexPath.row];
cell.videoThumbnail.image = [thumbArray objectAtIndex:indexPath.row];
cell.videoTitle.textColor = kfbBlue;
cell.videoDescription.textColor = kfbBlue;
cell.videoTitle.font = [UIFont fontWithName:#"FranklinGothicStd-ExtraCond" size:22.0];
cell.videoDescription.font = [UIFont fontWithName:#"FranklinGothicStd-ExtraCond" size:16.0];
cell.backgroundColor = [UIColor clearColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
KFBYoutubeVideoView *videoView = [[KFBYoutubeVideoView alloc]init];
videoView.videoIDString = [videoIDArray objectAtIndex:indexPath.row];
videoView.videoTitle = [titleArray objectAtIndex:indexPath.row];
videoView.videoDescription = [descriptionArray objectAtIndex:indexPath.row];
[self.navigationController pushViewController:videoView animated:YES];
}
else
{
KFBYoutubeVideoView *videoView = [[KFBYoutubeVideoView alloc]initWithNibName:nil bundle:nil];
videoView.videoIDString = [videoIDArray objectAtIndex:indexPath.row];
videoView.videoTitle = [titleArray objectAtIndex:indexPath.row];
videoView.videoDescription = [descriptionArray objectAtIndex:indexPath.row];
NSMutableArray *details = [self.splitViewController.viewControllers mutableCopy];
UINavigationController *detailNav = [[UINavigationController alloc]initWithRootViewController:videoView];
[details replaceObjectAtIndex:1 withObject:detailNav];
KFBAppDelegate *appDelegate = (KFBAppDelegate *)[[UIApplication sharedApplication]delegate];
appDelegate.splitViewController.viewControllers = details;
appDelegate.window.rootViewController = self.splitViewController;
appDelegate.splitViewController.delegate = videoView;
[appDelegate.splitViewController viewWillAppear:YES];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I figured it out. tableView was being initialized after adding loadingIndicator as a subview.

UITableView cellForRowAtIndexPath is not being called

I am having a problem with blank table data as this method is not getting called after I press search in my UISearchBar. I have tried everything and the table is just showing up blanking after pressing search.
This code takes the description from my json url and scans it for the searched word and then puts the index location of whatever description has that search term into an array (self.indexArray).
I would like to the call the table to only display those cells of the index values in that array.
- (void)viewDidLoad
{
[super viewDidLoad];
UISearchBar *tempSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 64, 320, 40)];
self.searchBar = tempSearchBar;
self.searchBar.delegate = self;
self.searchBar.placeholder = #"Search listings...";
[self.view addSubview:self.searchBar];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
[self sendData];
}
- (void)sendData
{
NSString *search = self.searchBar.text;
NSString *temp = [NSString stringWithFormat:#"MY JSON RETRIEVAL LINK"];
NSURL *url = [[NSURL alloc] initWithString:temp];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]
initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation
, id responseObject) {
NSLog(#"%#",responseObject);
NSLog(#"%#",[responseObject class]);
self.images = responseObject;
self.descriptions = [[NSMutableArray alloc] init];
for (int i = 0; i < [self.images count]; i++)
{
[self.descriptions addObject:self.images[i][#"description"]];
}
for (int i = 0; i < [self.descriptions count]; i++)
{
NSLog(#"%d: %#", i, self.descriptions[i]);
if ([self.descriptions[i] rangeOfString:search options:NSCaseInsensitiveSearch].location != NSNotFound)
{
NSLog(#"ADDING, %d", i);
[self.indexArray addObject:[NSNumber numberWithInt:i]];
}
}
tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,104,320,480) style:UITableViewStylePlain];
tableView.dataSource = self;
tableView.delegate = self;
[self.view addSubview:tableView];
[tableView reloadData];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"%#", error.localizedDescription);
}];
[operation start];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"FUNCTION CALLED");
TLCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if(cell == nil) {
cell = [[TLCustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"Cell"];
}
for(UIView *view in cell.contentView.subviews){
if ([view isKindOfClass:[UIView class]]) {
[view removeFromSuperview];
}
}
NSString *name = self.images[indexPath.row][#"name"];
NSString *location = self.images[indexPath.row][#"location"];
NSString *body = self.images[indexPath.row][#"description"];
NSString *list_type = self.images[indexPath.row][#"category"];
NSString *millisecs = self.images[indexPath.row][#"_createdAt"];
NSDate *date = [NSDate date];
NSTimeInterval ti = [date timeIntervalSince1970];
double myDouble = [millisecs doubleValue];
double delta = (ti * 1000) - myDouble;
NSString *time = [self calculateInterval:delta];
cell.nameLabel.text = name;
cell.locationLabel.text = location;
cell.bodyLabel.text = body;
CGFloat fixedWidth = cell.bodyLabel.frame.size.width;
CGSize newSize = [cell.bodyLabel sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
CGRect newFrame = cell.bodyLabel.frame;
newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
cell.bodyLabel.frame = newFrame;
cell.timeLabel.text = time;
if ([list_type isEqualToString:#"Sell"])
{
UIImageView *thumbnailView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"sell_icon_small.png"]];
thumbnailView.frame = CGRectMake(12, 12, 50., 50.);
[cell addSubview:thumbnailView];
}
else if ([list_type isEqualToString:#"Trade"])
{
UIImageView *thumbnailView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"trade_icon_small.png"]];
thumbnailView.frame = CGRectMake(12, 12, 50., 50.);
[cell addSubview:thumbnailView];
}
else if ([list_type isEqualToString:#"Wanted"])
{
UIImageView *thumbnailView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"want_icon_small.png"]];
thumbnailView.frame = CGRectMake(12, 12, 50., 50.);
[cell addSubview:thumbnailView];
}
else
{
UIImageView *thumbnailView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"ad_icon_small.png"]];
thumbnailView.frame = CGRectMake(12, 12, 50., 50.);
[cell addSubview:thumbnailView];
}
NSString *temp = self.images[indexPath.row][#"link"];
if ([temp isEqualToString:#"no_link"])
{
_thereIsAnImage = FALSE;
}
else
{
_thereIsAnImage = TRUE;
}
if (_thereIsAnImage)
{
SDWebImageManager *manager = [SDWebImageManager sharedManager];
[manager downloadWithURL:self.images[indexPath.row][#"link"]
options:0
progress:^(NSInteger receivedSize, NSInteger expectedSize)
{
// progression tracking code
}
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished)
{
if (image)
{
cell.imageView.image = image;
cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
cell.imageView.clipsToBounds = YES;
cell.imageView.tag = indexPath.row;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleImageTap:)];
[tap setNumberOfTapsRequired:1];
[tap setNumberOfTouchesRequired:1];
[cell.imageView setUserInteractionEnabled:YES];
[cell.imageView addGestureRecognizer:tap];
}
}];
}
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.indexArray.count;
}
If you are doing it programmatically then you need to set tableview.delegate = self and tableview.datasource = self in viewDidLoad method. Put a breakpoint in numberOfRowsInSection: to see the number that it's returning to check whether it's more than zero or not.
I would ask that you please check your header file - it should include the following...
#interface YourTableViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, UISearchDisplayDelegate>
Investigate how to implement UISearchBarDelegate and UISearchDisplayDelegate methods to manage search function in your (table) view controller. Read the Apple documentation UISearchBarDelegate and UISearchDisplayDelegate.
When you use a UISearchBar, you need a UISearchDisplayController to manage the search results.
If you are not using storyboards, it is important to set the appropriate data source and delegates for instances of both the UITableView and the UISearchDisplayController in your UITableViewController.
Then in your two table view data source methods, you need to provide information to the searchResultsTableView (accessed via self.searchDisplayController.searchResultsTableView) so that it knows how to prepare the search results table view.
For example...
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSInteger nofRowsInSection = 0;
if (tableView == self.searchDisplayController.searchResultsTableView) {
nofRowsInSection = self.searchResults.count;
} else {
nofRowsInSection = self.indexArray.count;
}
return nofRowsInSection;
}
...and...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
... <<other code>> ...
NSString *name = nil;
NSString *location = nil;
etc...
if (tableView == self.searchDisplayController.searchResultsTableView) {
name = self.searchResults[indexPath.row][#"name"];
location = self.searchResults[indexPath.row][#"location"];
...etc...
} else {
name = self.images[indexPath.row][#"name"];
location = self.images[indexPath.row][#"location"];
...etc...
}
... <<other code>> ...
}
Note that self.searchResults should be an NSMutableArray and it should contain data for the search results table view, prepared by filtering self.indexArray based on the search bar text.
Hope this helps.
Your code seems OK . check whether table from storeybaord is connected with the table and delegated correctly.
Did you put
#interface YourViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
to the top of you ViewController.m file?
You need to set tableView.delegate = self and tableView.dataSource = self in the viewDidLoad

UITableView for Forms Crashes

I have a UITableViewController that I'm using for a form. Each cell has a UILabel and a UITextField. When I tap on a UITextField, the keyboard comes up, then I scroll down and the cell goes off screen, when I tap on another UITextField, the app crashes.
This is my cell subclass.
#implementation EditorFieldCell
- (id)init
{
self = [super init];
if (self) {
self.selectionStyle = UITableViewCellSelectionStyleNone;
[self.contentView addSubview:self.nameLabel];
[self.contentView addSubview:self.textField];
}
return self;
}
- (void)setName:(NSString *)name
{
_name = name;
CGRect frame = self.nameLabel.frame;
frame.size.width = roundf([_name sizeWithFont:[UIFont boldSystemFontOfSize:17.0f]].width);
self.nameLabel.frame = frame;
frame = self.textField.frame;
frame.size.width = self.frame.size.width - 16.0f - 14.0f - 14.0f - self.nameLabel.frame.size.width;
frame.origin.x = 16.0f + 14.0f + self.nameLabel.frame.size.width;
self.textField.frame = frame;
self.nameLabel.text = _name;
}
- (void)setPlaceholder:(NSString *)placeholder
{
_placeholder = placeholder;
self.textField.placeholder = placeholder;
}
- (void)setText:(NSString *)text
{
_text = text;
self.textField.text = text;
}
- (UILabel*)nameLabel
{
if (_nameLabel)
{
return _nameLabel;
}
_nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(16.0f, 0.0f, 0.0f, self.frame.size.height)];
_nameLabel.font = [UIFont boldSystemFontOfSize:17.0f];
return _nameLabel;
}
- (UITextField*)textField
{
if (_textField)
{
return _textField;
}
_textField = [[UITextField alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 0.0f, self.frame.size.height)];
_textField.font = [UIFont systemFontOfSize:17.0f];
_textField.textAlignment = NSTextAlignmentRight;
_textField.keyboardAppearance = UIKeyboardAppearanceDark;
return _textField;
}
#end
And here is my table subclass.
#interface ManageWineViewController ()
#end
#implementation ManageWineViewController
- (id)init
{
self = [super initWithStyle:UITableViewStyleGrouped];
if (self) {
self.title = #"Manage Wine";
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStylePlain target:self action:#selector(done)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Save" style:UIBarButtonItemStyleDone target:self action:#selector(save)];
NSMutableArray *data = [[NSMutableArray alloc] init];
NSMutableDictionary *section = [[NSMutableDictionary alloc] init];
NSMutableArray *sectionData = [[NSMutableArray alloc] init];
[sectionData addObject:#{#"name": #"Estate", #"placeholder": #""}];
[sectionData addObject:#{#"name": #"Wine", #"placeholder": #""}];
[sectionData addObject:#{#"name": #"Vintage", #"placeholder": #"", #"keyboardType": [NSNumber numberWithInt:UIKeyboardTypeDecimalPad]}];
[section setObject:sectionData forKey:#"data"];
[data addObject:section];
section = [[NSMutableDictionary alloc] init];
sectionData = [[NSMutableArray alloc] init];
[sectionData addObject:#{#"name": #"Type"}];
[sectionData addObject:#{#"name": #"Style", #"placeholder": #"Select a Style", #"options": #[#"", #"Red", #"White", #"Rosé", #"Sparkling", #"Saké", #"Dessert, Sherry, and Port"]}];
[sectionData addObject:#{#"name": #"Appellation", #"placeholder": #""}];
[section setObject:sectionData forKey:#"data"];
[data addObject:section];
section = [[NSMutableDictionary alloc] init];
sectionData = [[NSMutableArray alloc] init];
[sectionData addObject:#{#"name": #"Alcohol %", #"placeholder": #"", #"keyboardType": [NSNumber numberWithInt:UIKeyboardTypeDecimalPad]}];
[section setObject:sectionData forKey:#"data"];
[data addObject:section];
self.data = data;
self.inputTexts = [[NSMutableDictionary alloc] initWithDictionary:#{#"0": #"",
#"1": #"",
#"2": #"",
#"10": #"",
#"11": #"",
#"12": #"",
#"20": #""}];
}
return self;
}
- (void)done
{
[self.currentTextField resignFirstResponder];
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}
- (void)save
{
[self done];
}
- (void)hidePicker
{
[self.selectActionSheet dismissWithClickedButtonIndex:0 animated:YES];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.data.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return ((NSArray*)[[self.data objectAtIndex:section] objectForKey:#"data"]).count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *cellInfo = [((NSArray*)[[self.data objectAtIndex:indexPath.section] objectForKey:#"data"]) objectAtIndex:indexPath.row];
static NSString *CellIdentifier = #"EditorCell";
EditorFieldCell *cell = (EditorFieldCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
{
cell = [[EditorFieldCell alloc] init];
}
cell.textField.tag = [[NSString stringWithFormat:#"%i%i", indexPath.section, indexPath.row] integerValue];
cell.textField.delegate = self;
cell.name = cellInfo[#"name"];
cell.placeholder = cellInfo[#"placeholder"];
cell.text = [self.inputTexts objectForKey:[NSString stringWithFormat:#"%i", cell.textField.tag]];
if (cellInfo[#"keyboardType"])
{
cell.textField.keyboardType = [cellInfo[#"keyboardType"] integerValue];
}
else
{
cell.textField.keyboardType = UIKeyboardTypeDefault;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
#pragma mark - UITextFieldDelegate methods
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
self.currentTextField = textField;
if (textField.tag == 11)
{
//show select
NSArray *options = [[[[self.data objectAtIndex:1] objectForKey:#"data"] objectAtIndex:1] objectForKey:#"options"];
self.selectTextField = textField;
self.selectOptions = options;
[textField resignFirstResponder];
self.selectActionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
CGRect pickerFrame = CGRectMake(0, 40, 0, 0);
self.selectPickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
self.selectPickerView.showsSelectionIndicator = YES;
self.selectPickerView.dataSource = self;
self.selectPickerView.delegate = self;
[self.selectActionSheet addSubview:self.selectPickerView];
[self.selectActionSheet showInView:[[UIApplication sharedApplication] keyWindow]];
[self.selectActionSheet setBounds:CGRectMake(0, 0, 320, 485)];
}
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
[self.inputTexts setObject:textField.text forKey:[NSString stringWithFormat:#"%i", textField.tag]];
return YES;
}
#end
The error that I am getting with the crash is:
*** -[EditorFieldCell _didChangeToFirstResponder:]: message sent to deallocated instance 0x155f6e20
This is iOS 7 (and will only support iOS 7) if that helps.
You are not initialising EditorFieldCell with reuse identifier.
EditorFieldCell *cell = (EditorFieldCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
This condition will false.
if (!cell)
{
cell = [[EditorFieldCell alloc] init];
}
Try to create cell with reuseIdentifier
cell = [[EditorFieldCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
And write this in EditorFieldCell.m
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
self.selectionStyle = UITableViewCellSelectionStyleNone;
[self.contentView addSubview:self.nameLabel];
[self.contentView addSubview:self.textField];
}
}

Unable to use searchbar to filter tableview

I am trying to filter tableview with search bar. I have written all necessary methods.
My code is as below:
What is wrong with it?? Please help me, i could not find the solution.
There should be something that I am missing
#implementation ApothekeViewController
#synthesize tableView,searchBar,mapView,eczaneler,eczanelerTemp,locationManager,searchDispController,filteredListContent,isFiltered,tableViewController;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
tableViewController = [[UITableViewController alloc] init];
tableViewController.tableView.delegate = self;
tableViewController.tableView.dataSource = self;
[tableViewController.tableView reloadData];
tableViewController.tableView.frame = CGRectMake(0,66 ,320.0, 768.0);
// tableView = [[UITableView alloc]init];
//tableView.frame = CGRectMake(0,66 ,320.0, 768.0);
//tableView.delegate = self;
//tableView.dataSource = self;
[self.view addSubview:tableViewController.tableView];
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
searchBar.placeholder = #"Arama" ;
searchBar.delegate = self;
UISearchDisplayController *searchCon = [[UISearchDisplayController alloc]
initWithSearchBar:searchBar
contentsController:tableViewController];
self.searchDispController = searchCon;
searchDispController.delegate = self;
searchDispController.searchResultsDataSource = self;
searchDispController.searchResultsDelegate = self;
//searchBar.showsCancelButton = YES;
tableViewController.tableView.tableHeaderView = searchBar;
NSDate *now = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init ] ;
[formatter setDateFormat:#"dd MMMM yyyy, EEEE"];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"Australia/Sydney"]];
NSString *strSelectedDate= [formatter stringFromDate:now];
UINavigationBar *naviBarObj = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 1024, 55)];
UILabel *date = [[UILabel alloc] initWithFrame:CGRectMake(760,25,400,30)];
date.text = strSelectedDate;
date.textColor = [UIColor whiteColor];
[date setFont:[UIFont fontWithName:#"Arial" size:25]];
[naviBarObj addSubview:date];
[date setBackgroundColor:[UIColor clearColor]];
naviBarObj.topItem.titleView = date;
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(60,5,400,50)];
title.text = #"En Yakın Eczane";
title.textColor = [UIColor whiteColor];
[title setFont:[UIFont fontWithName:#"Arial" size:40]];
[naviBarObj addSubview:title];
[title setBackgroundColor:[UIColor clearColor]];
naviBarObj.topItem.titleView = title;
UIBarButtonItem *btnCancel = [[UIBarButtonItem alloc]
initWithTitle:#"Cancel"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(cancel_Clicked:)];
naviBarObj.topItem.leftBarButtonItem = btnCancel;
[self.view addSubview:naviBarObj];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
mapView = [[MKMapView alloc] initWithFrame:CGRectMake(320, 66, 704, 705)];
mapView.delegate = self;
[self.view addSubview:mapView];
mapView.showsUserLocation = YES;
eczanelerTemp = [[NSMutableArray alloc]init];
self.eczaneler = [[NSMutableArray alloc] init];
Eczane *eczane1 = [[Eczane alloc] init];
eczane1.customerName = #"Duyarlı Eczanesi";
eczane1.customerID = #"mohammed#gmail.com";
eczane1.longitude = #"29.0312";
eczane1.latitude = #"41.109817";
Eczane *eczane2 = [[Eczane alloc] init];
eczane2.customerName = #"Azim Eczanesi Azim";
eczane2.customerID = #"azim#gmail.com";
eczane2.longitude = #"30.0312";
eczane2.latitude = #"41.109817";
[self.eczaneler addObject:eczane1];
[self.eczaneler addObject:eczane2];
Eczane *eczane3 = [[Eczane alloc] init];
eczane3.customerName = #"Optik Eczanesi";
eczane3.customerID = #"optik#gmail.com";
eczane3.longitude = #"29.0352";
eczane3.latitude = #"40.109817";
[self.eczaneler addObject:eczane3];
for(int i=0; i<[eczaneler count];i++)
{
Eczane *temp = [self.eczaneler objectAtIndex:i];
CLLocationCoordinate2D coordinate;
coordinate.longitude = [temp.longitude floatValue];
coordinate.latitude = [temp.latitude floatValue];
CustomAnnotation *point = [[CustomAnnotation alloc] init];
point.coordinate = coordinate;
[self.mapView addAnnotation:point];
}
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.mapView.delegate = self;
[self.mapView setShowsUserLocation:YES];
return YES;
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView2 viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
{
return nil;
}
else if ([annotation isKindOfClass:[CustomAnnotation class]])
{
static NSString * const identifier = #"MyCustomAnnotation";
MKAnnotationView* annotationView = [mapView2 dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView)
{
annotationView.annotation = annotation;
}
else
{
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:identifier];
}
// set annotationView properties
UIImageView * ecz = [[UIImageView alloc]init];
ecz.frame = CGRectMake(0, 0, 65, 49);
ecz.image = [UIImage imageNamed:#"indicator.png"];
UIImageView * vstd = [[UIImageView alloc]init];
vstd.frame = CGRectMake(33, 10, 24, 22);
vstd.image = [UIImage imageNamed:#"indicator_ziyaret_gri"];
UIImageView * nbox = [[UIImageView alloc]init];
nbox.frame = CGRectMake(48, -6, 22, 22);
nbox.image = [UIImage imageNamed:#"numara_kutusu"];
[annotationView addSubview:ecz];
[annotationView addSubview:vstd];
UILabel *index = [[UILabel alloc] initWithFrame:CGRectMake(5,4,15,15)];
// index.text =[NSString stringWithFormat:#"%d", iterator ];
index.textColor = [UIColor whiteColor];
[index setFont:[UIFont fontWithName:#"Arial-BoldMT" size:18]];
[nbox addSubview:index];
[index setBackgroundColor:[UIColor clearColor]];
[annotationView addSubview:nbox];
//annotationView. = [UIImage imageNamed:#"indicator.png"];
annotationView.canShowCallout = YES;
return annotationView;
}
return nil;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (self.tableViewController.tableView == self.searchDisplayController.searchResultsTableView) {
return [filteredListContent count];
} else {
return [eczaneler count];
}
}
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 110;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"cellForRowAtIndexPath");
static NSString *CellIdentifier = #"EczaneCell";
EczaneCell *cell = (EczaneCell *) [self.tableViewController.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[EczaneCell alloc] initWithFrame:CGRectZero] autorelease];
}
Eczane *eczane = [[Eczane alloc]init];
if (self.tableViewController.tableView == self.searchDisplayController.searchResultsTableView) {
eczane = [self.filteredListContent objectAtIndex:[indexPath row]];
} else {
eczane = [self.eczaneler objectAtIndex:[indexPath row]];
}
cell.circleIndex.text = [NSString stringWithFormat:#"%d", ([indexPath row]+1)];
cell.cellName.text = eczane.customerName;
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//EczaneCell *cell = [tableView cellForRowAtIndexPath:indexPath];
Eczane *eczane = [self.eczaneler objectAtIndex:[indexPath row]];
CLLocationCoordinate2D coordinate;
coordinate.longitude = [eczane.longitude floatValue];
coordinate.latitude = [eczane.latitude floatValue];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(coordinate, 2000, 2000);
[mapView setRegion:region animated:YES];
}
#pragma mark -
#pragma mark UISearchDisplayController Delegate Methods
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[filteredListContent removeAllObjects];
for (Eczane *eczObj in eczaneler)
{
NSComparisonResult result = [eczObj.customerName compare:searchString options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchString length])];
if (result == NSOrderedSame)
{
[self.filteredListContent addObject:eczObj];
}
}
[self.searchDispController.searchResultsTableView reloadData];
//[self.tableViewController.tableView reloadData];
//[self.tableViewController.tableView] = [self.searchDispController.searchResultsTableView ];
[self.tableViewController.tableView reloadData];
[searchDispController.searchResultsTableView setFrame:CGRectMake(0, 44, 320, [filteredListContent count]*(searchDispController.searchResultsTableView.rowHeight))];
return YES;
}
#end
Your problem is probably:
contentsController:tableViewController
Because you have 'stolen' the table view from the controller and made yourself the delegate and data source so the search controller will be trying to gather source information using a broken controller.
Make self the contents controller.
Your next problem is:
if (self.tableViewController.tableView == self.searchDisplayController.searchResultsTableView) {
Again, because you broke that controller (you should fix that, why are you creating a table view controller and then stealing its view?). Change it to:
if (tableView == self.searchDisplayController.searchResultsTableView) {

Resources