Getting all the selected cells values - ios

My purpose is when the user click on a button in view1 (to go to the view2), i will send all the selected cells from UITableView just checked in view1 to the view2.
So before passing the table to next view, i wanted to test if i am on the right track, but seems not. This is my code:
-(IBAction)goToView2{
//get all section selectioned items into an array
themesChosed=[tView indexPathsForSelectedRows];//tView is the outlet of my UITableView and themesChosed is an NSArray declared in the .h file
for (int i=0; i<=[themesChosed count]; i++) {
NSLog(#"%i",[[themesChosed objectAtIndex:i]integerValue]);
}
}
This always returning me a single 0.
2012-01-13 15:52:06.472 MyApp[876:11603] 0
Although i selected several items on the table.

The objects in themesChosed are NSIndexPath objects. You'll want to access the row and section properties like so:
-(IBAction)goToView2{
//get all section selectioned items into an array
themesChosed=[tView indexPathsForSelectedRows];//tView is the outlet of my UITableView and themesChosed is an NSArray declared in the .h file
for (int i=0; i<=[themesChosed count]; i++) {
NSIndexPath *thisPath = [themesChosed objectAtIndex:i];
NSLog(#"row = %i, section = %i", thisPath.row, thisPath.section);
}
}

First NSIndexPath is not an integer, it is an object with row and section methods, so that is probably why you get a zero. Are you sure you have allowsMultipleSelection set to YES?

If you are not getting index values then best approach wound be as below to access index number:
NSArray *indexes = [self.tableView indexPathsForSelectedRows];
for (NSIndexPath *path in indexes) {
NSUInteger index = [path indexAtPosition:[path length] - 1];
[selectedOptionsArray addObject:[optionHolderArray objectAtIndex:index]];
}

Related

When a row is selected in the table, do something on the other table?

I have three tables in a view, I need to implement this kind of structure;
I need to control if the row is selected in the first table, then adding rows of the third table to other view. when i press the button, the data of the selected and the rows in the third table added to view will be sent.
Firstly, i am unable to recognize whether the row is selected or not,
Secondly, when i try just to check it works, my data dictionary sent is empty, that should not.
My code is as below;
-(void) shareComment : (id)sender
{
int ndx = [[tableViewPharmName indexPathForSelectedRow] row];
ProdForLiterature *temp = [pharmsTable objectAtIndex:ndx];
[sendData setObject:temp.productID forKey:#"ProductName"];
[sendData setObject:temp.productName forKey:#"ProductId"];
NSMutableArray *customerId = [[NSMutableArray alloc]init];
for(DoctorListCell *cell in tableViewDoctorName.visibleCells)
{
if([cell.plusButton isHidden])
{
if(cell.customerId!=nil)
{
[customerId addObject:cell.customerId];
}
}
}
[sendData setObject:customerId forKey:#"CustomerId"];
}
For the first question. To know if a row is selected you can use this method.
NSIndexPath *path = [tableView indexPathForSelectedRow];
int rowSelected = path.row
For the second question, try first logging the temp object to see if it is empty.
NSLog(#"temp:%#",temp);
Also, check after this line.
[sendData setObject:customerId forKey:#"CustomerId"];
With another log to see if sendData contains information or it is initialised.
NSLog(#"sendData:%#",sendData);
Let us know what results you get.

How to get the values of selected cells of a UITableView in one string

i'm pretty sure this is really simple. But i can't get to make this work. I have a UITableView where i display dynamically a list of facebook friends, thanks to their FBID. Basically, i would like to return the FBIDs of the friends i selected, in one string separated with commas. If possible, in a IBAction, so i can pass them into parameters of a php.
For example, let's pretend i have a list of 4 friends, A B C D, and their ids are 1,2,3,4.
If i select A and C, i would like to have a string which says 1,3.
All i managed to do is to display the id of the friend i select, one at a time.
Here's my code :
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
rowcount = [[tableView indexPathsForSelectedRows] count];
indexer = [idFriends objectAtIndex:indexPath.row];
aapell = [NSString stringWithFormat:#"%#", indexer];
NSMutableString * arrayIds = [[NSMutableString alloc] init];
[arrayIds appendString:aapell];
NSLog(#"ids: %#", arrayIds);
}
Thank you in advance, i'm sure this is not complicated.
-(IBAction)gatherFBIds
{
NSString *listOfFacebookIDs = #"";
NSArray *indexPathArray = [self.tableView 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 = [idFriends objectAtIndex:index.row];
if([indexPathArray lastObject]!=index)
{
listOfFacebookIDs = [listOfFacebookIDs stringByAppendingString:[fbID stringByAppendingString:#", "]];
}
else
{
listOfFacebookIDs = [listOfFacebookIDs stringByAppendingString:fbID];
}
}
NSLog(#"Your comma separated string is %#",listOfFacebookIDs);
//Now pass this list to wherever you'd like
}
The problem is that you are not using the information in indexPathsForSelectedRows. That is telling you all the selected rows. Instead, you are just looking at indexPath.row, which is the one row most recently selected.
What you need to do is cycle through indexPathsForSelectedRows and gather up the info for every row that is currently selected (I'm assuming you've enabled multiple selection here).
Use the following code to get the selected rows in a table view. :)
NSArray *selectedRows=[tableView indexPathsForSelectedRows];
NSMutableArray *rownumberArray=[[NSMutableArray alloc]init];
for (int i=0; i<selectedRows.count; i++) {
NSIndexPath *indexPath = [selectedRows objectAtIndex:i];
NSInteger row = indexPath.row;
NSNumber *number = [NSNumber numberWithInteger:row];
[rownumberArray addObject:number];
}
// rownumberArray contains the row numbers of selected cells.

UITableView delete section - header stays visible

After using [UITableView deleteSections:withRowAnimation:] on a section which is out of view - the section header remains visible.
On this image, we see the visible part of the tableview
On the next image, we see the whole tableview - AISLE 2 is hidden until the user scrolls down, it contains only one row:
When I scroll down and delete the last row, AISLE 2 section header remains visible, even though I used deleteSections. if I delete a row from AISLE 1, the section header remains on the same place, and by scrolling down I can still see it.
Furthermore, when trying to scroll down so that AISLE 2 header is in the view, the UI acts as AISLE2 is NOT part of the tableview, and immediately scrolls me back up. Which means - this is a garbage view that is obviously not part of the table, since I removed it. for some reason, iOS doesn't remove this view, but de-associates it from the table.
Any ideas?
Try [tableview reload] with making numberofsections as 1.
Where is your data coming from and how do you know how many sections there are at the start? I think your problem can easily be resolved by explaining this.
It looks like you may have a multidimentional nsmutablearray where each index is an aisle and each object contains the products for that isle? Or you may have a different array for each aisle?
When you delete a cell, simply check how many cells are left, and call [self.tableView reloadData];
For (hypothetical) example;
if you have arrays of your Aisles:
NSMutableArray *aisleOne = [[NSMutableArray alloc] initWithObjects:#"Product1", #"Product2", nil];
NSMutableArray *aisleTwo = [[NSMutableArray alloc] initWithObjects:#"Product1", #"Product2", nil];
NSMutableArray *aisleThree = [[NSMutableArray alloc] initWithObjects:#"Product1", #"Product2", nil];
NSMutableArray *aisleFour = [[NSMutableArray alloc] initWithObjects:#"Product1", #"Product2", nil];
and you add them to one array:
NSMutableArray *aisleArray = [[NSMutableArray alloc] initWithObjects:aisleOne, aisleTwo, aisleThree, aisleFour, nil];
then, call this code when you delete a cell. It will remove all empty Aisles from the aisleArray (which needs to be globally defined):
NSMutableArray *tempArray = [[NSMutableArray alloc] initWithArray:aisleArray];
for (int i=0; i<[tempArray count]; i++) {
if ([[tempArray objectAtIndex:i] count] == 0) {
[aisleArray removeObjectAtIndex:i];
}
}
[self.tableView reloadData];
For this to work, these two methods should be:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [aisleArray count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[aisleArray objectAtIndex:section] count];
}
(untested)
Just get the table haderview for that section and remove it from it's superview and tableView is not managing it.
UIView *mysteriousView = [tableView headerViewForSection:indexPath.section];
[tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic];
[mysteriousView removeFromSuperview];
Hope this helps!
Few yers later I'm facing same issue... Deleting last section doesn't remove last section header from table view.
I noticed however that the remaining header has frame exactly just below normal table view content.
So the workaround (in swift but you can easily translate it to Objective C) that worked for me is something like this:
DispatchQueue.main.async {
let unwantedViews = self.tableView.subviews.filter{ $0.frame.minY >= self.tableView.contentSize.height}
unwantedViews.forEach{ $0.isHidden = true }
}

NSMutableArray last Object

I'm trying the iCarousel, there is a function that deletes the view's index and the array's index,
NSInteger index = carousel.currentItemIndex;
[carousel removeItemAtIndex:index animated:YES];
[imagesArray removeObjectAtIndex:index];
I'm deleting it until one item remains, then if one item remains I want to insert the duplicate of it. I tried this:
insertItemAtindex:carousel.currentItemIndex but it's inserting the last View.
But what I wanted is to insert carousel's/imagesArray last object/index in my view. How can I implement it, or how can I determine an NSMutableArray's last object that is left in the view?
carousel.lastObject !!!!!!!!!!
You can check for last object using -
int count = [yourArray count];
[yourArray objectAtIndex:count - 1]; //this will always return you last object

Print all items in NSMutablearray

My program has a tableview, I want to show selected items from the tableview in a textview
I have following code which shows only the last item of the NSMutablearray in textview:
I guess problem is the for loop but couldnt figure it out.
stands is NSMutablearray, selectItems is NSMutablearray, selected items is UItexview.
- (void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath {
[selectItems addObject:[stands objectAtIndex:indexPath.row]];
for (NSString *yourVar in selectItems) {
selectedItems.text=yourVar;
NSLog (#"Your Array elements are = %#", yourVar);
}
[self.mytableView reloadData];
}
I try the following but it crashes:
[selectItems addObject:[stands objectAtIndex:indexPath.row]];
int length = [selectItems count];
for(int i=0;i<=length;i++){
selectedItems.text= [selectItems objectAtIndex:i];
}
how can I show all the items of NSmutablearray in a texview ?
- (void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath {
[selectItems addObject:[stands objectAtIndex:indexPath.row]];
NSMutableString *exString = [[NSMutableString alloc]init];
for (NSString *yourVar in selectItems) {
NSLog (#"Your Array elements are = %#", yourVar);
[exString appendFormat:#"\n%#",yourVar];
}
selectedItems.text=exString;
[self.mytableView reloadData];
}
What about something like this? It takes each object, puts a newline in it, then adds it to exString to form one long chain of the objects in your array.
Make sure you properly allocated and inited selectItems. Just adding objects does not create the NSMutableArray. Then you also need to concatenate your elements instead of overwriting the UITextView over and over again.

Resources