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.
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);
}
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);
}
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.
Related
Even though it's common question but I'm not getting how to fix this.
I referred Selecting one UITableViewCell selects the other cells at that same position when you scroll up or down But i didn't understand.
I'm having allMumbaiArray (using in cellForRowAtIndexPath) and tableview alllMumbaiTableview. By using following code i can select and deselect the row.
UPDATED as per EI Captain's solution
#property (nonatomic, strong) NSMutableArray *arrIndexpaths;
tableView Methods -:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CellIdentifier"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"CellIdentifier"];
}
cell.textLabel.text = [allMumbaiArray objectAtIndex:indexPath.row];
if ([self.arrIndexpaths containsObject:[NSNumber numberWithInt:indexPath.row]])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else{
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.view endEditing:YES];
NSString *testing =[allMumbaiArray objectAtIndex:indexPath.row];
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[self.arrIndexpaths addObject:[NSNumber numberWithInt:indexPath.row]];
NSLog(#"%#",testing);
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];
tableViewCell.accessoryType = UITableViewCellAccessoryNone;
[self.arrIndexpaths removeObject:[NSNumber numberWithInt:indexPath.row]];
}
Issue : 1) when i select first row after scrolling the tablewview another row get selected.How to Fix this?
2) After selecting and deselecting row continuously, how can i maintain the only selected row values ?
Here is the solution
make one mutable array that stores selected index of tableview cell..
#property (nonatomic, strong) NSMutableArray *arrIndexpaths;
cellForRowAtIndexPath
if ([self.arrIndexpaths containsObject:[NSNumber numberWithLong: indexPath.row]])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
didSelectRowAtIndexPath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];
tableViewCell.accessoryType = UITableViewCellAccessoryCheckmark;
[self.arrIndexpaths addObject:[NSIndexSet indexSetWithIndex:indexPath.row]];
}
didDeselectRowAtIndexPath
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];
tableViewCell.accessoryType = UITableViewCellAccessoryNone;
[self.arrIndexpaths removeObject:[NSIndexSet indexSetWithIndex:indexPath.row]];
}
Here is the sample project...
demo project
If your cell are not showing accessory then you just need to reload them
[_ableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationNone];
try this in didSelectRowAtIndexPath and didDeselectRowAtIndexPath.
How do i store all selected cells text label text in a NSMutableArray?
How do i remove the correct cells text from the NSMutableArray when a cell is deselected?
What i have this far:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
static NSString *CellIdentifier = #"Cell";
PFTableViewCell *cell = (PFTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[PFTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if ([self.allSelectedUsers containsObject:indexPath]) {
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
NSLog(#"Yeeees");
}else{
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
// Configure the cell
cell.textLabel.text = object[#"username"];
return cell;
}
This is when i'm selecting a cell:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if ([self.allSelectedUsers containsObject:indexPath]) {
[self.allSelectedUsers removeObject:indexPath];
}
else{
[self.allSelectedUsers addObject:indexPath];
}
NSLog(#"%d", self.allSelectedUsers);
[tableView reloadData];
}
When i check the log it doesn't display anything about the cells text label.
As I can't see how you're getting object instance, I suggest you to get back cell and read the title again.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
// Lazy initialise array
if (!self.allSelectedUsers) {
self.allSelectedUsers = [NSMutableArray new];
}
// Take action
if ([self.allSelectedUsers containsObject:indexPath]) {
[self.allSelectedUsers removeObject:indexPath];
} else {
[self.allSelectedUsers addObject:indexPath];
}
[tableView reloadData];
// Logging all selected users
for (NSIndexPath *sIndexPath in self.allSelectedUsers) {
NSLog(#"%#", [tableView cellForRowAtIndexPath:sIndexPath].textLabel.text);
}
}
You are currently storing NSIndexPath objects, not NSString objects, so it's not exactly what your question is asking. With your PFTableViewController, you have access to the selector objectAtIndexPath:.
for (NSIndexPath *indexPath in self.allSelectedUsers) {
NSLog(#"%#", [self objectAtIndexPath:indexPath][#"username"]);
}
Note: You shouldn't be calling reloadData in your didSelectRowAtIndexPath: responder; change the accessory type for the cell instead.
[[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];
You should also implement didDeselectRowAtIndexPath: to know when a user deselects a row.
i'm trying to do a selection list in UITableView but whenever i tap in a row, two row are marked. For example if i tap the first row it adds a checkmark to row = 0 and row = 11. I think the problem is caused by reuse of cells but i don't understand how solve it.
I attach my code .h:
#interface tabellaViewController : UITableViewController
#property (nonatomic, strong) NSArray *listaingredienti;
#end
and my code .m:
- (void)viewDidLoad
{
[super viewDidLoad];
_listaingredienti = [NSArray arrayWithObjects:#"formaggio", #"crudo", #"pomodorini", #"fossa", #"bresaola", #"funghi", #"salame", #"fontina", #"rucola", #"radicchio", #"peperoni", #"ciccioli", #"squacquerone", #"gorgonzola", #"salsiccia", #"cipolla", #"feta", #"acciughe", #"pecorino", nil];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_listaingredienti count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [_listaingredienti objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow]
animated:NO];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
I've seen UITableView didSelectRowAtIndexPath add additional checkmark at tap where there is a similar post but i already use dequeueReusableCellWithIdentifier:CellIdentifier.
Anyone can help me?
Ok,
i'm just solved my question. Actually, the problem was in reuse of cells, but this code fix it.
Just replace in cellForRowAtIndexPath::
static NSString *CellIdentifier = #"Cell";
with:
NSString *CellIdentifier = [NSString stringWithFormat:#"MyReuseIdentifier %d",indexPath.row];
When I build and run there is nothing displayed. No text just empty cells.
Here is my code:
#import "plungerselection.h"
#implementation plungerselection
#synthesize tableData;
-(void) viewDidLoad
{
tableData = [[NSArray alloc] initWithObjects:#"Turbo", #"Hollow Turbo", #"Single Pad", #"Dual Pad", #"Bullet", nil];
[super viewDidLoad];
}
#pragma mark - TableView Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;{
return [tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:#"MyCell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"MyCell"];
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
}
return cell;
}
#end
Make sure you have wired up the datasource and delegate connections in Interface Builder.
You are only setting the text in the cell if dequeueReusableCellWithIdentifier: returns nil which in this case doesn't happen (and even if it did it will only happen a few times before all the cells that are needed get in the queue). You should set the text outside of the if (cell == nil)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:#"MyCell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"MyCell"];
}
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
return cell;
}
I get an array of another class, and make changes to this class, but when I do a reloadData not much happens.
What is wrong?
This is my code
#pragma mark TesteFaceCarViewController methods
-(void)valores:(NSMutableArray *)array
{
arrayInfracao = array;
NSLog(#"Quantidade :%d", arrayInfracao.count);
[table reloadData];
}
#pragma mark TableView
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(#"Passei aqui!");
NSLog(#"Quantidade :%d", arrayInfracao.count);
return arrayInfracao.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];
}
Infracao *infracao = [[Infracao alloc] init];
infracao = [arrayInfracao objectAtIndex:indexPath.row];
cell.textLabel.text = infracao.dsInfracao;
return cell;
}
You are welcome!
[self.table reloadData];