Fawaz Faiz Fawaz Faiz - 1 month ago 10
JSON Question

How to filter the multiple selected values from an array and load it to other uitableview from a uibutton

I have 2 UiTableviews in a UiViewController. My exact requirement is to get the selected values from tableview1 and loaded to the tableview2 by tapping the Add Button.

I done Following at the moment
1. loaded JSON Responses to tableview1 (loaded the responses from a filepath at the moment) and printed to a custom nib cell
2. I can get the selected values to a response as follows Getting Selected Values as NSLOG responses
Next
3. I want to print that values to *cell2 from the selected values. Please explain the next steps in details.

Here is the UI
Main UI and Description

Here are the codes from table loading to selections.

Object - PSAData.h

@interface PSAData : NSObject

@property (nonatomic, strong) NSString *firstname;
@property (nonatomic, strong) NSString *lastname;

- (void)loadWithDictionary:(NSDictionary *)dict;

@end


Object - PSAData.m

@implementation PSAData

- (void)loadWithDictionary:(NSDictionary *)dict
{
self.firstname=[dict objectForKey:@"firstname"];
self.lastname=[dict objectForKey:@"lastname"];
}


table1 Viewcell - PSATableViewCell.h

#import <UIKit/UIKit.h>
#import "PSAData.h"
#import "NetworkConnectivityClass.h"
@interface PSATableViewCell : UITableViewCell

@property (nonatomic) NetworkConnectivityClass *networkConnectivityClassInstance;

-(void)loadWithData:(PSAData *)psaData;
@end


table1 viewcell - PSATableViewCell.m

@interface PSATableViewCell ()
@property (strong, nonatomic) IBOutlet UILabel *firstnameLbl;
@property (strong, nonatomic) IBOutlet UILabel *lastnameLbl;


@end

@implementation PSATableViewCell

- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];


}

-(void)loadWithData:(PSAData *)psaData
{
[self methodSetupText:psaData];
}

-(void)methodSetupText:(PSAData *)psaData
{
self.firstnameLbl.text=psaData.firstname;
self.lastnameLbl.text=psaData.lastname;

}
@end


Main View Controller - PersonnelViewController

- (void)viewDidLoad {
[super viewDidLoad];


// NSMutableArray *selectedArray=[[NSMutableArray alloc]init];

tableView1.dataSource =self;
tableView1.delegate=self;
tableView2.dataSource=self;
tableView2.delegate=self;

//initializing arrays for get selected values
self.selectedCells=[NSMutableArray array];
self.selectedPSAData=[NSMutableArray array];

//loading web resonse data
self.loadedPSAData=[[NSMutableArray alloc]init];

NetworkConnectivityClass *networkConnectivityClassInstance = [NetworkConnectivityClass new];

__weak PersonnelViewController *weakVersionOfSelf=self;

[networkConnectivityClassInstance methodReturnTableViewMessages:^(NSMutableArray *returnedArrayWithMessages)
{
weakVersionOfSelf.loadedPSAData=returnedArrayWithMessages;
[weakVersionOfSelf.tableView1 reloadData];

}];
//register left side tableview cell (Assigned tableview1)
[self.tableView1 registerNib:[UINib nibWithNibName:@"PSATableViewCell" bundle:nil] forCellReuseIdentifier:@"PSATableViewCell"];

}


//tableview Delegates

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

if(tableView ==self.tableView1)
{
return 1;
}
return 1;
}

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

if(tableView==self.tableView1)
{

return self.loadedPSAData.count;

}
return self.loadedPSAData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


//loading table data into cell tableview1 and tableview2
static NSString *cellIdentifier=@"PSATableViewCell";

PSATableViewCell *cell=[tableView1 dequeueReusableCellWithIdentifier:cellIdentifier];


if (cell==nil) {

cell=[[PSATableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"PSATableViewCell"];
}



//tableview2 implementation
static NSString *cellIdentifier2=@"PSSITableViewCell";

PSSITableViewCell *cell2=[tableView2 dequeueReusableCellWithIdentifier:cellIdentifier2];

if (cell2==nil) {
cell2=[[PSSITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"PSSITableViewCell"];
}
cell.accessoryType = ([self isRowSelectedOnTableView:tableView1 atIndexPath:indexPath]) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;

if(tableView == self.tableView1)
{
[cell loadWithData:[self.loadedPSAData objectAtIndex:[indexPath row]]];
}
else if(tableView == self.tableView2)
{
[cell2 loadWithDataS2:[self.loadedPSAData objectAtIndex:[indexPath row]]];
return cell2;
}

return cell;
}


pragma mark - multiple selection



- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

NSString *psaData =[self.loadedPSAData objectAtIndex:indexPath.row];
PSATableViewCell *cell=[tableView1 cellForRowAtIndexPath:indexPath];


NSString *sampleAH =[self.selectedPSAData description];

if([self isRowSelectedOnTableView:tableView1 atIndexPath:indexPath])
{
[self.selectedCells removeObject:indexPath];
[self.selectedPSAData removeObject:psaData];

cell.accessoryType =UITableViewCellAccessoryNone;
}
else{
[self.selectedCells addObject:indexPath];
[self.selectedPSAData addObject:psaData];

cell.accessoryType =UITableViewCellAccessoryCheckmark;

}

NSLog(@"%@", self.selectedPSAData);


sampleArrayholder.text=[NSString stringWithFormat:@"%@", sampleAH];

}

-(BOOL)isRowSelectedOnTableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath
{
return ([self.selectedCells containsObject:indexPath]) ? YES : NO;
}


**Finally NetworkConnectivity class - NetworkConnectivityClass.h **

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "WorksitenameList.h"

@interface NetworkConnectivityClass : NSObject

-(void)methodLogin:(NSString *)stringToLogin withUserName:(NSString *)stringUsername withPassword:(NSString *)stringPassword completion:(void(^)(NSDictionary *))completion;


-(void)methodReturnTableViewMessages:(void (^)(NSMutableArray *))completion;


-(NSURLSessionTaskState)methodCheckIfSessionIsRunning;

-(void)methodCancelNetworkRequest;


@end

**Finally NetworkConnectivity class - NetworkConnectivityClass.m **

-(void)methodReturnTableViewMessages:(void (^)(NSMutableArray *))completion
{
dispatch_queue_t queueForJSON = dispatch_queue_create("queueForJSON", NULL);
dispatch_async(queueForJSON, ^{
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"PSAData" ofType:@"json"];

NSError *error = nil;

NSData *rawData = [NSData dataWithContentsOfFile:filePath options:NSDataReadingMappedIfSafe error:&error];

id JSONData = [NSJSONSerialization JSONObjectWithData:rawData options:NSJSONReadingAllowFragments error:&error];

NSMutableArray *loadedPSAData = [NSMutableArray new];
[loadedPSAData removeAllObjects];

if([JSONData isKindOfClass:[NSDictionary class]])
{
NSArray *loadedArray=[JSONData objectForKey:@"records"];
if([loadedArray isKindOfClass:[NSArray class]])
{
for(NSDictionary *psaDict in loadedArray)
{
PSAData *psaData=[[PSAData alloc]init];
[psaData loadWithDictionary:psaDict];
[loadedPSAData addObject:psaData];
}

}
}
dispatch_async(dispatch_get_main_queue(), ^{
completion(loadedPSAData);
NSLog(@"test: %@", loadedPSAData);
});
});

}


I Added All the required codes to have a look at it. Since I am relatively new to iOS Dev. Please Write a codes / instructions step by step clearly to save some time :).

**Please Note : At the moment I loaded the same tableview1 data to tableview2 *cell2 But I want here to load the selected values(Multiple Selection). from tableview1 loadedPSAData Array to load in table view to by tapping an Add Button. Finally sorry for my poor English ;) **

Answer

Suppose these 2 are your table outlet.

__weak IBOutlet UITableView * myTable1;
__weak IBOutlet UITableView * myTable2;

You set the Delegate and Data Source like below in your viewDidLoad

myTable1.delegate=self;
myTable1.dataSource=self;
myTable2.delegate=self;
myTable2.dataSource=self;

Now These are 2 Arrays

array1 ==> Which contain All Data
arraySelectedValues ==> Which contain you selected data

So you use this like below

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section   
{
    if(self.myTable1 == tableView) 
    {
        return [array1 count];        
    } else {
        return [arraySelectedValues count];
    }
}

As I review your Code, Your problem may be in CellForRow. Why to create 2 separate cells, when there is same CellIdentifier. You just need to change the Data.

You should create 2 cells only When Both Tableview are using Diff-Diff tableviewcells. In your case you are using same cell.

Try to modify the code like this

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *cellIdentifier=@"PSATableViewCell";

    PSATableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell==nil) {

        cell=[[PSATableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier: cellIdentifier];
    }

if(tableView == myTable1)
{
    [cell loadWithData:[array1 objectAtIndex:[indexPath row]]]; 
}
else if(tableView == myTable2)
{
    [cell loadWithData:[arraySelectedValues objectAtIndex:[indexPath row]]];
}
return cell;

}

Just When ever you want to refresh the record, you should to Call ReloadData method For respective Tableviews.

Hope you understand how you load the data in 2 Tableviews in Single ViewController.

Comments