如何从数组中过滤多个选定值并将其从uibutton加载到其他uitableview

菲多

我在UiViewController中有2个UiTableviews。我的确切要求是通过点击“添加”按钮从tableview1获取选定的值并将其加载到tableview2。

我在此刻完成了以下操作:1.将JSON Responses加载到tableview1(当前从文件路径加载响应)并打印到自定义笔尖单元2。我可以将选择的值转换为响应,如下所示获取选定的值作为NSLOG响应。Next 3.我想要从所选值将值打印到* cell2。请详细说明接下来的步骤。

这是用户界面主界面和说明

这是从表加载到选择的代码。

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 ;) **

Wolverine

Suppose these 2 are your table outlet.

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

您可以在viewDidLoad中设置如下所示的Delegate和Data Source。

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个单独的单元格。您只需要更改数据。

当两个Tableview都使用Diff-Diff tableviewcells时,应创建2个单元格在您的情况下,您使用的是同一单元格。

尝试像这样修改代码

- (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.
 }    
}

只要您想刷新记录,就应该为各自的Tableview调用ReloadData方法。

希望您了解如何在Single ViewController的2个Tableview中加载数据。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用ARM程序集声明多个数组值并将其加载到RAM中?

如何将其他表中的多个选定行值插入到新表中

Tensorflow:如何保存变量并将其加载到其他变量?

在下拉列表中获取选定的值,并将其放入其他下拉列表中?

如何将javascript数组转换为php数组并将其加载到div中

如何读取多个文件并将其加载到数据框中

如何使用Javascript获取多个选定项的列表并将其值输出为数组

JavaFx将其他FXML加载到FXML“模板”中

Javascript无法将其他页面加载到DIV中

如何创建JavaScript数组并将其他数组数据保存到Javascript中创建的数组

如何获取选定复选框的值并将其放入android中的String []数组中

从其他列的值中过滤出列值,并将结果转换为多个列表Pandas

如何从Swift 3数组中的对象提取值并将其比较或映射到其他数组中不同对象的值?

如何从数组中过滤多个项目并将其添加到对象?

PDFBox - 从多个 PDF 中读取文本并将其加载到多个文本文件中

如何从 aync/await 函数返回 bool 值并将其传递给其他页面中的其他变量

从数组中获取重复的下一个值的计数并将其存储到其他数组

如何循环数组并将操作的值存储在其他数组中

从本地存储中检索数据对象并将其重新加载到数组中

如何将值转换为列并将其他列中的值放入 Pandas 中的这些列中

如何在jquery中获取选定复选框的值并将其分配给json数组?

循环浏览资源文件并将其加载到数组中

何时其他类使用php的类中的常数变量将其加载到RAM中?

jQuery:将其他页面中的div加载到div中

将其他数据加载到仍在商店中的EmberData模型中

如何从数据框中创建多个其他列并将其添加到同一数据框中

如何根据其他数组的值过滤对象数组?

Spring批处理读取key =值并将其加载到数据库中

将其他网页的内容加载到div中并添加样式