简体   繁体   中英

In Objective-C, How to initialize NSMutableArray with objects in another NSMutableArray using tableView reference

Developing for MacOS, I have an NSMutableArray namesArray[] that contains 3 String objects. namesArray[] is represented in a tableView, where user can select multiple cells, each cell represents a single object. I am trying to initialize a second NSMutableArray savedNamesArray[], and add objects from the original namesArray[] based on the selected cells in my tableView using this method:

NSMutableArray *savedNamesArray = [[NSMutableArray alloc] initWithObjects:[namesArray objectsAtIndexes:[_tableView selectedRowIndexes]], nil];

The problem is, no matter how many objects I select, only one gets added to the new NSMutableArray. Any suggestions?

You're adding one object, an array, to savedNamesArray . Use initWithArray: instead.

NSMutableArray *savedNamesArray = [[NSMutableArray alloc] initWithArray:[namesArray objectsAtIndexes:[_tableView selectedRowIndexes]]];

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