Reputation: 437
I want to get the values of multiple selection of tableview on clicking a button.I checked lot of stackoverflow questions but unable to get the answer. I know this is already achieved by many people so i request someone to guide me about getting the values from tableview. I am able to select mulitple values from tableview.I am able to see checkmark.
Below is my code
.h file
@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
@property (strong, nonatomic) IBOutlet UITableView *specTableView;
@property (nonatomic, retain) NSArray *arForTable;
@property (nonatomic, retain) NSMutableArray *arForIPs;//to capture ids
- (IBAction)btnActionToGetValues:(id)sender;
.m file
- (void)viewDidLoad {
self.arForTable=[NSArray arrayWithObjects:@"value1",@"value2",@"value3",@"value4", nil];
self.arForIPs=[NSMutableArray array];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.arForTable count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if([self.arForIPs containsObject:indexPath]){
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
} else {
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
cell.textLabel.text=[self.arForTable objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if([self.arForIPs containsObject:indexPath]){
[self.arForIPs removeObject:indexPath];
} else {
[self.arForIPs addObject:indexPath];
}
[tableView reloadData];
}
//button action for getting values
- (IBAction)btnActionToGetValues:(id)sender {
NSLog(@"ids %@",arForIPs);
}
I am getting this output
ids ( " {length = 2, path = 0 - 1}", " {length = 2, path = 0 - 2}" )
My expected output is
ids ( "Value1", "Value2" )
If any one has already worked on this please let me know.
Upvotes: 0
Views: 126
Reputation: 5887
What you are seeing in your log is the indexPath
value of your cells. The length
value is how many items are in the path (2) and the path
value is showing the row and section of the cell the user tapped.
If you will use the index path value to get a reference to the cell; you can ask the cell if it has a checkmark turned on. You are also holding the values in your arForTable
array. So, you use the index path from the tableView
to ask the arForTable
array for the value. I might write the didSelectRowAtIndexPath
like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSString *valueFromArray = [[self arForTable] objectAtIndex:[indexPath row]];
if([self.arForIPs containsObject:valueFromArray]){
[self.arForIPs removeObject:valueFromArray];
} else {
[self.arForIPs addObject:valueFromArray];
}
[tableView reloadData];
}
Of course, this will cause your cellForRowAtIndexPath
to break. So you will need to update that as well. Perhaps replace this:
if([self.arForIPs containsObject:indexPath]){
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
} else {
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
cell.textLabel.text=[self.arForTable objectAtIndex:indexPath.row];
with something like:
NSString *valueString = [[self arForTable] objectAtIndex:[indexPath row]];
if([self.arForIPs containsObject:valueString]){
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
} else {
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
cell.textLabel.text=valueString;
Often in a tableView
you will find that the array (arForTable
) holds the data and since the index
property of the array matches the row
property of the tableView we can use that match to query the array for the data that corresponds to the tableView row that we are displaying.
Upvotes: 0
Reputation: 1
I guess you can create one new array,example:
NSMutableArray *arr = [NSMutableArray array];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if([self.arForIPs containsObject:indexPath]){
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
[arr addObject:self.arForTable[indexPath.row]];
} else {
[cell setAccessoryType:UITableViewCellAccessoryNone];
[arr removeObject:self.arForTable[indexPath.row]];
}
cell.textLabel.text=[self.arForTable objectAtIndex:indexPath.row];
return cell;
}
- (IBAction)btnActionToGetValues:(id)sender {
NSLog(@"ids %@",arr);
}
Upvotes: 0
Reputation: 82766
try this
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if([self.arForIPs containsObject:[self.arForTable objectAtIndex:indexPath.row]]){
[self.arForIPs removeObject:[self.arForTable objectAtIndex:indexPath.row]];
} else {
[self.arForIPs addObject:[self.arForTable objectAtIndex:indexPath.row]];
}
[tableView reloadData];
}
on your CellforRowatIndexpath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if([self.arForIPs containsObject:[self.arForTable objectAtIndex:indexPath.row]]){
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
} else {
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
cell.textLabel.text=[self.arForTable objectAtIndex:indexPath.row];
return cell;
}
output you can get
//button action for getting values
- (IBAction)btnActionToGetValues:(id)sender {
NSLog(@"ids %@",arForIPs);
}
Upvotes: 1