how to identify which cell in tableview clicked - objective-c

I created tableview that read from url (any cell has name of book) and when clicked any cell gone detailView.
in detail view exist UIScroll that read any image from url and I create it.
but my problem this place. I don't know what read images in UIScroll related any cell.
this is URL : ".../mamal/book.php?p=%d&b=%d"
(p and b display number page and number book)
I want when clicked first cell (book1) compiler gone next page and display images related book1
and etc.

You have to use UITableView protocols <UITableViewDataSource,UITableViewDelegate> in your .h file then use below delegate method which you give which cell is clicked
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"Clicked cell-> %d", indexPath.row);
}

You can also identify the currently selected row using the property indexPathForSelectedRow.
i.e if you want to know the index path of currently selected row at any point of time without using the delegate methods (not saving it during selection) you can use
tableView.indexPathForSelectedRow
If you want to pass around the value between multiple view controller you can declare an instance variable in the view controller you want the value to be passed and store tableView.indexPathForSelectedRow in this variable during initialization. A quick search on so should help you with that.

Related

UITableView removing checkmark from cell on search

I am using a UITableView to display some items and i have implemented a UISearchBar to this UITableView.
If the user presses the cell, a checkmark appears (accessory type of the cell is changed) and when pressed again the checkmark accessory disappears.
My problem is when the user searches using the search bar, all the checked cells are not checked anymore, no more checked cells at all.
I've been trying to fix this and searching for it for a while but no results ...
Any help ??
Thanks you.
That is because your table view where you put the checkmarks and the table view that you see after you enter something in the search bar is not same. To have the same check marks on the search table view cells, you need to explicitly add the checkmarks.
I can't see the way you are adding and removing check marks. So, in theory, what you need is a way to know which cells have check marks. So, let's assume you have an array of items and a parallel array which holds the 1 or 0 to signal if the items have the check marks when 1 means check marked and 0 means no check mark.
Let's call this parallel array, checkMarkSignalArray. Now, in your cellForRowAtIndexPath method, you can do something like-
if(self.resultSearchController.active){
cell.textLabel.text = [self.filteredItemsArray objectAtIndex:indexPath.row];
if([self.checkMarkSignalArray objectAtIndex:indexPath.row]){
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}

Handling tableview cell selection from other view controllers

I have a tableView with custom table cells which displays a list of sayings. While selecting a cell (say "saying1") from the tableView, the cell will get selected and will moves to next view controller for showing an explanation of selected saying. In that view controller two buttons "Previous" and "Next" are there for navigating to previous and next saying. So user may select next button for seeing next saying (say "saying2"). But the problem here is, In this stage when i navigate back to my tableview, the cell selected will be "saying1" not "saying2".
In order to solve this problem, i collected the currently selected sayings ("saying2's") index from array (ie,index will be 1) and passed it to the previous tableView through segue and converted that value into NSIndexpath. After this step i don't know what to do. Please help me in solving this.
You can specifically select a cell by calling following method present in UITableView class :
selectRowAtIndexPath:animated:scrollPosition:
refere to following link : https://developer.apple.com/library/ios/documentation/uikit/reference/UITableView_Class/Reference/Reference.html#//apple_ref/occ/instm/UITableView/selectRowAtIndexPath:animated:scrollPosition:

Array with selectItemAtIndexPath of a UICollectionView

I'm trying to programmatically select cells in a UICollection view. I'm new to Obj-C and I'm not quite sure how to use the selectItemAtIndexPath property. I can grab an array of the images the user has previously selected. It is just an array of numbers corresponding to named images. But I'm not sure how to use that information with selectItemAtIndexPath.
I've looked for examples of someone using
- (void)selectItemAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UICollectionViewScrollPosition)scrollPosition
But I'm not sure how to use it with my UICollectionView so I can have the right cells selected when the view loads and thus be highlighted. I have multiple selection on.
You should call selectItemAtIndexPath: for each cell you want to highlight, like so:
[self.collectionView selectItemAtIndexPath:path animated:NO scrollPosition:UICollectionViewScrollPositionNone]
Note that for one item (and one only!) you probably want to set the animated property to YES and provide a scroll position (one item only because otherwise you're going to be making a lot of needless animation calls).
You'll first need to get the index paths of the cells you want to select. The index path consists of two numbers: the section the cell is in and the row number (or order, if your collection view doesn't have rows) of the cell inside that section.
If you store the index paths of the cells the user has selected in an array then you can just iterate through. Otherwise you'll need to find the index path out, using a UICollectionView method such as indexPathForCell.

Get state of UISwitch in custom UITableViewCell's

I am using a custom tableviewcell in one of my view's. I have decided to add a UISwitch to this cell to enable the user to delete multiple row's at once.
Normally when the user selects a row and taps my delete button I have a UIAlert pop up for confirmation and after that the alert clickedButtonAtIndex method handles the outcome. In that method I get the indexpath ( [self.myTableView indexPathForSelectedRow] ) and delete (or not) accordingly.
So basically as the title states instead of using the indexPath to fall into my delete statement, I need to check to see if self.myTableView.myCell.mySwitch.on is TRUE. Can anyone point me in the right direction for doing this? I will need to iterate through all rows in the tableview and for each row where the state is on it needs to be removed.
Thanks.
you should iterate through your cells using a nested loop ( or a single loop if you have only one section) and cellForRowAtIndexPath:.if you see a cell's switch.on = TRUE save it's indexPath in an array by calling indexPathForCell:. after you're done iterating through your cells, call deleteRowsAtIndexPaths:withRowAnimation: with the array previously created.

How to populate the table view cell with values entered in another view controller?

I have a custom table view cell, which consists of four labels and an image view. I have another UIViewController which consists of two text fields and one text view.
When the values are entered in the text fields and text view of this controller, and the save button is pressed, the values must be saved on to my custom table view cells' text labels. How is this possible?
Update your data source object with the values and call reloadData function on tableview instance
- (void)reloadData
Call as below
[myTableView reloadData];
EDITED:
First : you could use the delegate concept of Objective-C,
Second: you could store the text value in user default using NSUserDefault and access that in other view controller,
See the very good blog post on NSUserDefault .
iPhone Programming Tutorial – Saving/Retrieving Data Using NSUserDefaults
Capture the Values entered in the Text Inputs and save them in different NSString variables. Set those values for the Next ViewControllers Input.
For example, declare a NSString in second View Controller Interface and #synthesize that. Before moving from first controller to second controller, set the value for that respective variable.