简体   繁体   中英

Add objects into array from UITableViewCell

Here i need to implement add objects into array for multiple row selections,here is my code

BOOL selectedRow

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

if(selectedRow){
 UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
            cellText = selectedCell.textLabel.text;
            NSLog(@"Airline cellTextC = %@",cellText);

if ([cellText isEqualToString:NameString]) {
                NSLog(@"Selected NameString Index = %@",indexes);

                [nameArray addObject:indexes];

            }else{
                NSLog(@"Unknown Selected Name");
            }

}
NSLog(@"Total Names  = %@",nameArray.description);

}

the above code one section and multiple rows is there ,if am selected a row name string should be add to the Array .It's working fine for only while am selecting one row.But i need add objects from multiple row selections.Can you please suggest me thanks .

您可能想要更新表视图的数据源,并让表视图数据源方法为您更新表视图。

检查是否可以解决您的问题:

[tableView setAllowsMultipleSelection:YES];

if you want to select Multiple rows and add objects to your array i suggest to create 2 NSMutableArray the first one contains all data and the other the selected rows data.

@property (strong , nonatomic) NSMutableArray *myalldatatab;
@property (strong , nonatomic) NSMutableArray *selecteddata;

then use didselectrowsatindexpath and didDeselectRowAtIndexPath to change the mark

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
}

and you can get indexes of selected rows by calling this line :

NSArray *selectedCells = [self.tableView indexPathsForSelectedRows];

you can now iterate selected cells and add objects like this

for (int i = 0; i < [selectedCells count]; i++)
        {
[self.selecteddata addObject: [selectedCells objectAtIndex:i]]
}

Hope it help you :)) happy c0de !!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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