Im working on tableview single multiple checkbox functionality. I have done the Multiple checkbox in tableview. But i have no idea about the single selection checkbox in tableview. can anyone help me.
Thanks in advance.
Keep in a variable the current selected choice.
On tableView:cellForRowAtIndexPath: display checked image if the current indexPath.row is equal to this variable, otherwise display the image for unchecked.
Then, in tableView:didSelectRowAtIndexPath: - store the new selected cell in this variable and do a [tableview reloadData].
Use allowsMultipleSelection property of UITableView.
For Single selection
self.tableView.allowsMultipleSelection = NO;
For Multiple selection
self.tableView.allowsMultipleSelection = YES;
Related
How can I implement a UISwitch in every cell of a UITableView, in order to use the tableView as a sort of checklist, which I can then gather the checked items to send to a backend, to 'purchase' the items?
The list of items may change and is located on the app backend, I have already implemented the parsing of the items and the building of the tableview, but I'm just not sure how to assign the switches when the length of the tableview is uncertain.
I'm new to Objective-C and XCode, and couldn't find any answers anywhere, so thanks for any help.
I am assuming here that your data is stored in an array.
In each UITableViewCell, where you have added the UISwitch, set it's tag to same as it's indexPath.row.
mySwitch.tag = indexPath.row;
You can easily get the index of the object for which you had set the switch, using the the tag stored.
[myArray objectAtIndex:mySwitch.tag];
I have TableView and I'm placing UIScrollView with images inside every cell.
My problem is, when I click the UIScrollView, the code that should run on didSelectRowAtIndexPath is not running.
I know why, but I don't know how to pass the click to the relevant cell.
I tried using TAG so self.scrollViewer.tag = [indexPath row]; ' and adding UITapGestureRecognizer for every scrollView, but it's not accurate... somtimes it loads the data of the previous or next cell...
if the previous or next cell is little showing a little bit, the tag changes.. so I have to be exactly on the current cell for the correct tag.
Thanks in advance!
I think what you want is pointInside, hitTest:withEvent
This thread will likely help.
I have a UITableView that displays contacts from AddressBook. I have the multi-selection editing style enabled. When I present the tableView, it works as intended. The user taps a row and the red checkmark to the left side of the cell fills in. Great.
I save these selections in an array property on the presenting view controller. When I show my table again, I would like for these cells to remain selected (i.e., not blue highlighted, just with the red check mark still filled in). In cellForRowAtIndexPath: I get the object for the cell at that index path, test whether the presenting view controller's array contains that object, and if so [cell setSelected:YES];
It seems to me that this should result in the same selected appearance as when the user touches a cell (just the red check mark). Instead, the red check does fill in but also the cell highlights blue momentarily and the text turns white permanently, creating the impression that the contact's name has disappeared. I'm happy to post code if it's helpful but this seems like maybe just a quirk of UITableView that someone might be familiar with?
EDIT: This image shows the kind of checkmarks that I have (and want). I can't piece together though why when I call [cell setSelected:YES]; it behaves differently than it does in response to a touch. What you're looking at here is after I call [cell setSelected:YES];. And it looks almost how I want but the names have disappeeared. And I don't want the cell to highlight when the list appears.
If I understand the visual effect you're looking for, you should be getting it by using cell.accessoryType = UITableViewCellAccessoryCheckmark; rather than [cell setSelected:YES];.
This is resolved. In think I was making problems for myself by trying to setSelected in cellForRowAtIndexPath. Maybe the table view's reusing of the cells was making it act crazy?
The solution for getting the cells to select correctly between presentations of the multi-selection style table was to store the selected indexes at viewWillDisappear. I gave the presenting view controller a property NSArray *selectedIndexes, and in viewWillDisappear called
[self.presentingViewController setSelectedIndexes:[myTableView indexPathsForSelectedRows]];
Then in viewWillAppear, I put that array into an ivar, iterated through it and called [myTableView selectRowForIndexPath:[selectedIndexes objectAtIndex:i] animated:YES];
That did the trick. Thanks to user523234 for the comment. Helped get me thinking on the right track.
I have dragged an item into my tableview object.
When the dragged item hover over an item in tableview,
the item is redrawn with selection background. The image is as under
The row of the tableview is not selected, when i checked selectedRow method.
My requirement is when an item hover any item i should control its selection
and the background selection thereof.
Thanks,
iSight
To 'control' the selection of a TableViewCell when 'hovering' over such an item you will have to call this method:
- (NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point
It's a method from the UITableView Class and uses the local coordinate system of your tableView. As is mentioned in the Apple Doc: http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableView_Class/Reference/Reference.html
When the method is called you will have a NSIndexPath returned. With it you can select the cell/item at that particular path.
Mind, selecting a path manually doesn't call the didSelectRowAtIndexPath delegate-method so if you want a certain method called from that delegate you will have to do that manually as well!
This method is prolly called from the touchedDidMove method (where you also do the drag&drop) so getting the point needed for the method shouldn't be a problem for you.
You will figure it out :)
Good luck.
I have 5 cells in a tableview that are all custom. Meaning, I've created a xib with a tableviewcell and created a corresponding cellController. The xib has two labels named name and size. The user taps a row, triggering didSelectRowAtIndexPath. From there a timer is started. At some point the timer finishes. Here I need to assign text to the selected cell's name label. How do I get the selected cell reference and keep it for assigning? Or, is there a better way to do it?
The UITableView instance method
-(UITableViewCell*) cellForRowAtIndexPath: (NSIndexPath*)indexPath
will allow you to get a pointer to the cell.
Simply store the row+section info from NSIndexPath argument of the didSelectRowAtIndexPath event. Then, when the timer finishes, build a new NSIndexPath and call cellForRowAtIndexPath. Also be prepared for it to return nil if the cell is no longer in view.