Chandler Buck
Chandler Buck

Reputation: 213

If-statement won't check NSArray

I am trying to create an if statement that checks to see if the selected cell is equal to an object in an array and if it is then it will set an NSURL accordingly. The table that I am referencing is from another view controller called PopOverViewController that I have created a delegate and used in the destinationViewController.

ViewController.m:

-(void) didLoadSelectedLayer:(NSString *)selectedLayer{
self.popOverArray = [[NSArray alloc] initWithObjects:@"Electric Radio", @"Electric Truck", @"Gas Radio", @"Gas Truck", @"Meter", @"Sewer Radio", @"Sewer Truck", @"Support Radio", @"Support Truck", @"Water Radio", @"Water Truck", @"Select All", nil];

if ([_popoverTableVC.myTable.indexPathForSelectedRow isEqual:@"Electric Radio"]) {
    _url = [NSURL URLWithString:@"http://somewebsite.com"];
}else{

}

I originally had a switch statement but since switch statements cannot use NSStrings I am confused on how to achieve the desired effect.

This is the PopOverViewController.h where I create the delegate:

#import <UIKit/UIKit.h>
#import <ArcGIS/ArcGIS.h>

#pragma mark - popoverTableViewControllerDelegate

@protocol popOverTableViewControllerDelegate <NSObject>
@required
-(void) didLoadSelectedLayer:(NSString *)selectedLayer;


@end

@interface PopOverTableViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate>
{
AGSFeatureLayer *popOverLayer;

IBOutlet UITableView *myTable;


}
@property (nonatomic, strong)IBOutlet UITableView *myTable;
@property (nonatomic, strong) NSArray *popOverArray;
@property (nonatomic,strong) id<popOverTableViewControllerDelegate> delegate;
//-(void)didSelectObject:(UITableView *)tableView :(NSInteger *)key;
@end

This is the PopOverViewController.m:

#import "PopOverTableViewController.h"
#import "ViewController.h"

#pragma mark - PopOverTableViewControler

@interface PopOverTableViewController ()
{
    NSURL *_url;
    ViewController *_ViewController;
}

@end

@implementation PopOverTableViewController
@synthesize delegate;

-(void)viewDidLoad
{
    [super viewDidLoad];
    self.popOverArray = [[NSArray alloc] initWithObjects:@"Electric Radio", @"Electric Truck", @"Gas Radio", @"Gas Truck", @"Meter", @"Sewer Radio", @"Sewer Truck", @"Support Radio", @"Support Truck", @"Water Radio", @"Water Truck", @"Select All", nil];
    [self.tableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    // Return the number of rows in the section.
    return [self.popOverArray count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return 1;
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
        NSString *selectedLayer = [_popOverArray objectAtIndex: indexPath.row];
        [self.delegate didLoadSelectedLayer:selectedLayer];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier =@"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    cell.textLabel.text = [self.popOverArray objectAtIndex:indexPath.row];
    return cell;
}


@end

Upvotes: 1

Views: 70

Answers (2)

Lyndsey Scott
Lyndsey Scott

Reputation: 37290

Your conditional is incorrectly checking the NSIndexPath of the selected row against your NSString:

if ([_popoverTableVC.myTable.indexPathForSelectedRow isEqual:@"Electric Radio"]) {

You should instead compare your string to the contents of the array's index which corresponds to your selected row:

if ([[self.popOverArray objectAtIndex:_popoverTableVC.myTable.indexPathForSelectedRow.row] isEqual:@"Electric Radio"]) {

Upvotes: 1

yun
yun

Reputation: 1283

[_popoverTableVC.myTable.indexPathForSelectedRow isEqual:@"Electric Radio"]

This line of code is comparing a NSIndexPath with a NSString. It will never be true. You'll have to get the string of that row for this to work. Good luck

Upvotes: 1

Related Questions