如何从数组中过滤多个选定的值,并将其从uibutton加载到其他uitableview [英] How to filter the multiple selected values from an array and load it to other uitableview from a uibutton
问题描述
我在UiViewController中有2个UiTableviews。我的确切要求是从tableview1获取所选值并通过点击添加按钮加载到tableview2。
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.
我完成了当前
1.已加载JSON响应tableview1(此时加载来自文件路径的响应)并打印到自定义nib单元格
2.我可以将所选值输入到响应中,如下所示
下一页
3.我想从所选值中将该值打印到* cell2。请详细说明接下来的步骤。
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 Next 3. I want to print that values to *cell2 from the selected values. Please explain the next steps in details.
这是UI
主要用户界面和描述
以下是从表格加载到选择的代码。
Here are the codes from table loading to selections.
对象 - PSAData.h
@interface PSAData : NSObject
@property (nonatomic, strong) NSString *firstname;
@property (nonatomic, strong) NSString *lastname;
- (void)loadWithDictionary:(NSDictionary *)dict;
@end
对象 - 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
主视图控制器 - 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代表
//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 - 多项选择
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;
}
**最后NetworkConnectivity类 - NetworkConnectivityClass.h **
**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
**最后NetworkConnectivity class - NetworkConnectivityClass.m **
**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);
});
});
}
我添加了所有必需的代码来查看它。因为我是iOS Dev的新手。请一步一步地写一个代码/说明,以节省一些时间:)。
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 :).
**请注意:目前我将相同的tableview1数据加载到tableview2 * cell2但是我想在这里加载所选的值(Multiple Selection)。从tableview1 loadedPSAData数组加载到表视图中通过点击添加按钮。最后抱歉我的英语不好;)**
**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 ;) **
推荐答案
假设这2个是你的桌面插座。
Suppose these 2 are your table outlet.
__weak IBOutlet UITableView * myTable1;
__weak IBOutlet UITableView * myTable2;
您在viewDidLoad中设置代理和数据源如下所示
You set the Delegate and Data Source like below in your viewDidLoad
myTable1.delegate=self;
myTable1.dataSource=self;
myTable2.delegate=self;
myTable2.dataSource=self;
现在这些是2个数组
array1 ==> Which contain All Data
arraySelectedValues ==> Which contain you selected data
所以你使用下面这个
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(self.myTable1 == tableView)
{
return [array1 count];
} else {
return [arraySelectedValues count];
}
}
当我查看您的代码时,您的问题可能出在CellForRow。当存在相同的CellIdentifier时,为什么要创建2个单独的单元格。您只需要更改数据。
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.
你应创建2个单元 当两个Tableview使用 Diff-Diff tableviewcells时。在你的情况下你是使用相同的单元格。
You should create 2 cells only When Both Tableview are using Diff-Diff tableviewcells. In your case you are using same cell.
尝试修改这样的代码
- (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;
}
didSelectRowAtIndexPath 应如下所示:
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(tableView == myTable1)
{
// Your code for highlighting
} else {
// This will be you myTable2 did select click.
}
}
正好当你想刷新时记录,您应该调用ReloadData方法对于各自的表格视图。
希望您了解如何在单个ViewController中的2个Tableview中加载数据。
Hope you understand how you load the data in 2 Tableviews in Single ViewController.
这篇关于如何从数组中过滤多个选定的值,并将其从uibutton加载到其他uitableview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!