简体   繁体   English

如何在一个包含另一个UITableView作为其单元格的UITableViewController中跟踪选定的索引(在数组中)

[英]How to keep track of selected indexes (in array) in a UITableViewController Which contains another UITableView as its Cells

with the help of THIS Tutorial . 本教程的帮助下。 . . .

I made a UITableViewController (for reference I can call it MainTableVC) , which has single row in each of its section and I made every cell of this UITableViewController(MainTableVC) a UITableView (for reference I can call it UITableViewOfMainTableVCell) with many cells. 我制作了一个UITableViewController(供参考,我可以称其为MainTableVC),该节在每个节中都有一行,并且使该UITableViewController(MainTableVC)的每个单元格成为一个UITableView(供参考,我可以称其为UITableViewOfMainTableVCell)有很多单元格。

Now I rotated Cell/rows of UITableViewOfMainTableVCell {* its a UITableView not a UITableViewCell }, so that i could scroll cells of UITableViewOfMainTableVCell horizontally. 现在,我旋转了UITableViewOfMainTableVCell的单元格/行{*是UITableView而不是UITableViewCell},以便可以水平滚动UITableViewOfMainTableVCell的单元格。

now my UI looks like this, I have a table with 10 section(say), each section has one row/cell and in this row/cell i have several rows/cells which can be scrolled horizontally 现在我的UI看起来像这样,我有一个包含10个部分的表(比如说),每个部分有一个行/单元格,在此行/单元格中我有几个行/单元格,可以水平滚动

I also enabled allowMultipleSelections in custom cells of UITableViewOfMainTableVCell 我还在UITableViewOfMainTableVCell的自定义单元格中启用了allowMultipleSelections

Now I wanted to keep track of selected indexes of selected rows so I took an NSArray and saved indexPaths in it with this delegate method of UITableViewOfManiTableVCcells 现在, I wanted to keep track of selected indexes of selected rows因此我使用了NSArray并使用UITableViewOfManiTableVCcells的委托方法在其中保存了indexPaths

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSMutableArray *selectedIndexesOfOneRow = [NSMutableArray arrayWithArray:[tableView indexPathsForSelectedRows]];
NSLog(@"number of selected Indexes = %d",[selectedIndexesOfOneRow count]);
}

-(void)tableView:(UITableView *)tableView didDeSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSMutableArray *selectedIndexesOfOneRow= [NSMutableArray arrayWithArray:[tableView indexPathsForSelectedRows]];
NSLog(@"number of selected Indexes = %d",[selectedIndexesOfOneRow count]);
}

MY PROBLEM :- 我的问题

suppose if I select 4 cells/rows in a row/cell of MainTableVC the NSMutableArray *selectedIndexesOfOneRow has the right count BUT if I select cells of another a row in another section of MainTableVC with even keeping previous cells selected the count starts from beginning 假设如果我在MainTableVC的一行/单元格中选择4个单元格/行,则NSMutableArray * selectedIndexesOfOneRow具有正确的计数BUT,如果我选择在MainTableVC的另一部分中选择另一行的单元格,甚至保持先前的单元格处于选中状态,则计数从头开始

the problem is with indexPathsForSelectedRows, it only keeps track of Selected Rows of single UITableView, since i have separate UITableViews for each Row of Each Section of MainTableVC NSMutableArray always gets reset when select cells from different row 问题在于indexPathsForSelectedRows,它只跟踪单个UITableView的选定行,因为我对MainTableVC NSMutableArray每个节的每一行都有单独的UITableViews,当从不同行中选择单元格时总是会重置

MY QUESTION 我的问题

since every Row/Cell calls the same UITableView delegate method, how will I prevent it, I want to store all the selected indexes of cells in whole UI 由于每个Row / Cell都调用相同的UITableView委托方法,因此如何防止这种情况,我想在整个UI中存储所有选定的单元格索引

I could make an array of array to store indexes of different different indexes but how would i get those differently 我可以制作一个数组数组来存储不同索引的索引,但是我将如何获取它们呢?

"HINT" I could do somthing like this, in my MainTableVC, Since i have a tableView inside Cell of MainTableVC i can put an NSArray *array = [cell.tableView indexPathsOfSelectedIndex], but again in which delegate method of MainTableVC should i put this, and also i have checked, Since i am not selecting any whole row of MainTableVC(UITableViewController), its delegate methods didSelectRowAtIndexPath and didDeSelectRowAtIndexPath never get called “提示”我可以在MainTableVC中做这样的事情,因为我在MainTableVC的Cell内有一个tableView,所以我可以放置一个NSArray * array = [cell.tableView indexPathsOfSelectedIndex],但同样应该在其中将MainTableVC的委托方法放在其中,而且我已经检查了一下,因为我没有选择MainTableVC(UITableViewController)的任何整行,所以它的委托方法didSelectRowAtIndexPath和didDeSelectRowAtIndexPath永远不会被调用

You can assing tag to your tableView, then check tag to distinguish all tableView. 您可以将标签关联到tableView,然后检查标签以区分所有tableView。

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag==1) {
        //Do what ever you want with table having tab =1
    }else if (tableView.tag==2) {
        //Do what ever you want with table having tag =2
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
 // NSLog(@"user selected %@",[dataArray objectAtIndex:indexPath.row]);
NSString *listOfFacebookIDs = @"";
NSArray *indexPathArray = [self.mTableView indexPathsForSelectedRows];
for(NSIndexPath *index in indexPathArray){
    //Assuming that 'idFriends' is an array you've made containing the id's (and in the same order as your tableview)
    //Otherwise embed the ID as a property of a custom tableViewCell and access directly from the cell
    //Also assuming you only have one section inside your tableview
    NSString *fbID = [dataArray objectAtIndex:index.row];
    if([indexPathArray lastObject]!=index){
        listOfFacebookIDs = [listOfFacebookIDs stringByAppendingString:[fbID stringByAppendingString:@", "]];
    }else{
        listOfFacebookIDs = [listOfFacebookIDs stringByAppendingString:fbID];
    }
}
  NSLog(@"Your comma separated string is %@",listOfFacebookIDs);
}


- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"user de-selected %@",[dataArray objectAtIndex:indexPath.row]);
}

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM