On my UITableView I want to check how many times user has tapped the same row.
If user has tapped the same row three times I want to delete the row from the UITableView.
Can anyone please lead me to the solution? I tried doing:
for (NSIndexPath *indexPath in self.tableView.indexPathsForSelectedRows) {
count = count + 1;
NSLog(#"rowCount %d indexPath.row %#", count, indexPath);
}
But this doesn't increment the count number of times the row was tapped by user.
Assuming you want to delete the row only if the user select three times a row the same cell.
Create another variable lastSelectedRow where you keep the last selected row. (create underneath the implementation line)
#implementation myViewController
{
NSInteger = lastSelectedRow;
}
Next, you should verify that the row is equal to the last one selected, increment and check if the row have to be deleted:
for (NSIndexPath *indexPath in self.tableView.indexPathsForSelectedRows) {
// If the last selected row is the same, increment the counter, else reset the counter to 1
if (indexPath.row == lastSelectedRow) count++;
else
{
lastSelectedRow = indexPath.row;
count = 1;
}
// Now we verify if the user selected the row 3 times and delete the row if so
if (count >= 3) [tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
NSLog(#"rowCount %d indexPath.row %#", count, indexPath);
}
Hope it helps.
Create an NSMutableDictionary property, where the key's are the indexPath's and the value is the current count of taps. e.g.
#property (nonatomic,strong) NSMutableDictionary *rowTaps;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (!self.rowTaps) {
self.rowTaps = [NSMutableDictionary dictionary];
}
[self.rowTaps setObject:[NSNumber numberWithInt:[(NSNumber *)[self.rowTaps objectForKey:indexPath]intValue]] forKey:indexPath];
if ([(NSNumber *)[self.rowTaps objectForKey:indexPath]intValue] == 3) {
// Perform Delete Action - Delete row, and update datasource
}
}
Perform your check on the dictionary each time the selection occurs on a cell, then perform the necessary action.
Create the following property in the interface:
#interface ViewController () <UITableViewDelegate,UITableViewDataSource>
#property (assign,nonatomic) int counter;
#end
In your implementation you can increment the counter as follows:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.counter++;
NSLog(#"the count is %i",self.counter);
}
Create a NSMutableDictionary and set the key as the cell index, then for the value set the count (1 at first). When the count reaches three then you can do what you need.
//fake cell index (from indexpath)
NSNumber * pretendCellIndex = [NSNumber numberWithInt:4];
//Dict to track ocurrences
NSMutableDictionary *index = [[NSMutableDictionary alloc]init];
//If the dict has the index, increment
if ([index objectForKey:pretendCellIndex]) {
//Get value for the index
int addOne = [[index objectForKey:pretendCellIndex] intValue];
addOne++;
//Add back to index
[index setObject:[NSNumber numberWithInt:addOne] forKey:pretendCellIndex];
//Your condition
if (addOne>=3) {
//do what you need
}
}else{
//Havent seen so add
[index setObject:[NSNumber numberWithInt:1] forKey:pretendCellIndex];
}
Related
I intended to add items to a UITableView when this method was called. New items are successfully added (but there's a problem, mentioned below), but I think its odd because I thought "begin updates" to "end updates" was supposed to handle this (Inserting a new row). The initial if condition I had put in never ran so the whole area never got executed. I only realized this recently and updated the condition to what it is now. Now the if block get executed and it crashes the app.
When it is commented out like it is now... New items are added but the newNameOfItem replaces any existing cell labels.
I would like this to add x(newNumberOfItems) new items preferably into a new section each time its called. How can I achieve this?
- (void)addNew:(NSString *)newNumberOfItems :(NSString *)newNameOfItem
{
if(!self.numberOfRows){
NSLog(#"Initially no of rows = %d", self.numberOfRows);
self.numberOfRows = [self.numberOfItems intValue];
NSLog(#"Then no of rows = %d", self.numberOfRows);
}
else
{
self.numberOfRows = self.numberOfRows + [newNumberOfItems intValue];
NSLog(#"New no rows = %d", self.numberOfRows);
}
NSLog(#"run = %d", self.run);
Begin updates if statement ...
/*if(self.secondRun){
NSLog(#"run = %d it it", self.run);
[self.tableView beginUpdates];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.numberOfRows-[newnumberOfItems intValue] inSection:0];
[self.tableView insertRowsAtIndexPaths:#[indexPath]withRowAnimation:UITableViewRowAnimationBottom];
[self.tableView endUpdates];
}
*/
[self.tableView reloadData];
}
...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"UITableViewCell" forIndexPath:indexPath];
cell.textLabel.text = self.nameOfItem;
return cell;
}
...
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.numberOfRows;
}
If you want to add new items to a new section every time you call -addNew…, you should create an NSMutableArray of sections where each member is an array of objects representing row data (in this case seems like you'd want NSStrings that represent the item's name. The structure would look something like:
mySections = #[ #[#"section 0 row 0 name", #"section 0 row 1 name", …], #[#"section 1 row 0 name", #"section 1 row 1 name", …], …]
Then in numberOfRowsInSection: return mySection[indexPath.section].count.
Each label has the same value because you're setting every single label for every cell that you dequeue to self.nameOfItem. It's doing exactly what you told it to do. If your intent is to set a different text for every section/row, you have to fetch that text from somewhere. If you created a mySections array as above, you could:
cell.textLabel.text = mySections[indexPath.section][indexPath.row] ;
A note about -addNew:…: simply adding new items to your mySections array will not cause the tableview to update. As you know above, [self.tableView reloadData] will do this for you. However, it will reload the entire table instead of just updating the rows that you added. To do this more efficiently (and with a nice animation), you instead use [self.tableView beginUpdates/endUpdates]. In the case above, where you're adding entire sections and not just rows, you should use insertSections:withRowAnimation:.
In my app, I have a tableView, which contains 5 sections, each section contains only one row, I have inserted a customView on each row, The view consists of name and mobile number, when a section is selected, the name and mob. no. should of the "corresponding section" be passed to the next view.
I have inserted the screen shot below.
It seems you want to identify the section of the row for which didSelectRowAtIndexPath is invoked. You can get the section information in an NSIndexPath object itself by accessing 'section' property
Here is the class reference : https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSIndexPath_Class/Reference/Reference.html#//apple_ref/doc/c_ref/NSIndexPath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
int section = indexPath.section;
//...
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
//get the name and phoneNo from currently selected cell.
NSString *name = cell.yourCustomView.nameLabel.text;
NSString *phoneNo = cell.yourCustomView.phoneNoLabel.text;
if(indexPath.section == 0)
{
//code for section 1 selected
YourNextViewController *nvc = [self.storyboard instantiateViewControllerWithIdentifier:#"YourNextViewControllerIdentifier"];
nvc.nameProperty = name;
nvc.phoneNoProperty = phoneNo;
[self.navigationController pushViewController:nvc animated:YES];
}
if(indexPath.section == 1)
{
//code for section 2 selected
}
if(indexPath.section == 2)
{
//code for section 3 selected
}
if(indexPath.section == 3)
{
//
}
if(indexPath.section == 4)
{
//
}
}
You can use this logic.
1) According to your views, each section have phone no & Name. So you can keep into one array which contains dictionary object.like below
[
{
name : "Chandru"
phoneNo: "90xxxxxxxx"
},
..........
]
2) So you can use this in cellForRowAtIndexpath: as, cell.nameLabel.text = [self.yourArray objectAtIndex:indexPath.section] objectForKey:#"name"];
3) So you can pass it to next view as nextVC.selectedPersonDetail = [self.yourArray objectAtIndex:indexPath.section] in didSelectRowAtIndexPath:
where selectedPersonDetail should be public property of NSDictionary. Otherwise you can use public method to hold as private variable.
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.
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.
The user can select/deselect cells in each section, so to track all the selected cells in the didSelectRowAtIndexPath is not useful:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"row = %i, section = %i", indexPath.row, indexPath.section);
}
What i want instead is to save time and resources, so to let the user check/uncheck whatever he wants, and when he click on a button (to go to next view), i need to collect all selected cells in the whole table view. I tried to do like this:
-(IBAction)nextView:(id)sender{
themesChoosed=[self.tView indexPathsForSelectedRows];//tView is the outlet of my UITableView and themesChosed is an NSArray declared in the .h file
for (int i=0; i<=[themesChoosed count]; i++) {
NSIndexPath *thisPath = [themesChoosed objectAtIndex:i];
NSLog(#"row = %i, section = %i", thisPath.row, thisPath.section);
}
}
But this doesn't return me the selected cells, it shows me always: row = 0, section = 0 as if i am only selecting the first cell in the first section.
Any thoughts on how to do so?
Take in .h file
NSMutableArray *selectedArray;
in .m
viewDidLoad
selectedArray=[[NSMutableArray alloc]init ];
in tableviewDidSelect
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//According to section take value from table and get string selectedString.
if([selectedArray containsObject:selectedString])
{
[selectedArray removeObject:selectedString];
}
else
{
[selectedArray addObject:selectedString];
}
}