How to distinguish which item has been selected in UITableViewCell - ios

I'm trying to create a project similar to Instagram. When you select user, it brings you to user VC. When you select comment, it brings you to comment VC.
I have a custom UITableViewCell that has many items in it just like Instagram as mentioned above. What I'm trying to achieve is when user selects any item within a UITableViewCell regardless it is a label, or image, it will do segue to another VC for detailed information.
I'm referring to this post to achieve it, but my concern is how am I going to recognise which has been selected? Because I have an unknown repetitive count of rows.
I don't really need codes as they can most likely be found with thorough searching, though it would be more helpful. But I just need structure because I'm totally lost on how I should do this.
I'm doing this in Swift programming.
Thanks for helping.

You can insert UIButtons into UITableViewCells. Those UIButtons won't conflict with the UITableViewCells tap gesture. I would subclass UIButton so you could pass that row and item data to that button when created

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"%ld",(long)indexPath.row);
if (indexPath.row==0)
{
[self performSegueWithIdentifier:#"abc" sender:self];
}
if (indexPath.row==1)
{
}
}

Related

How to create a repeatable view in iOS and XCode

I am somewhat new to iOS, but am experienced in Android.
I have an app I am working on and it needs to populate a page with your "history" of past people you've interacted with, and it shows their picture, name, rating, and some other information.
This needs to populate in a vertical list, maybe a table? See the image below...
Now, in android, I would create a custom class with a layout that houses the picture, name, information, rating, and what not in one xml file, and in the activity I would call that class in a for loop, grabbing all the users and then programmatically it would add each view one after another, with their own unique user information until there is no more users to populate with.
How exactly can I do this in iOS and xcode? Do I need to make an XIB and add the picture, name, rating, and info place holders in that, and create a custom class for it that I would use to run in a for loop as well? I am a little stuck on how to do this with iOS.
Any help is much appreciated, and I can provide any additional information! Thanks :)
In iOS, you probably want to use a UITableView, with each row being a custom subclass of UITableViewCell. You can either create the layout for those cells in a separate XIB, or put the whole lot, tableView and "prototype" cells in a storyboard. You can achieve a lot without even subclassing, so fire up a dummy project in XCode and play (using one of Apple's templates gives you a good start). Enjoy.
What you probably want is to use a UITableView.
You don’t do the for-loop yourself. What you do is implement a set of delegate methods that the table view calls back to.
You can create your prototype cell in your XIB or Storyboard. When you add a Table View to the layout, you can then add a cell to that table view, and that cell will be your prototype. It looks like you only need one prototype cell, but you can create as many as you need. In Interface Builder you give the prototype cell a “reuse identifier”, which is just an arbitrary tag you use to refer to the prototype in your code. Your prototype cell can be your own subclass of UITableViewCell, or if you don’t need any custom code in it, you can just use UITableViewCell.
Then you implement several delegate methods. One is where you set the number of sections in the table view; it looks like you will only have on section.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tv
{
return 1;
}
Then you tell it how many items are in the table view. Assuming you have the objects you want to display in an array, you just return the length of the array.
- (NSInteger)tableView:(UITableView *)tv numberOfRowsInSection:(NSInteger)section
{
return self.objects.count;
}
Then, for each item in the array, cellForRowAtIndexPath will be called. Make that method return the actual cell. You call dequeueReusableCellWithIdentifier to retrieve your prototype cell, using the reuse identifier you assigned in Interface Builder. Then use the corresponding object to set up the UI elements in your cell.
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)i
{
UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:i];
Thingy *item = self.objects[i.row];
cell.textLabel.text = item.name;
return cell;
}
That should be enough to get you started with the documentation, now that you have the overview of what you need to implement.
The first thing you have to do in switching from Android to iOS is to learn the terminology. Then you'll know what to search for on Google, SO, etc.
What's you're looking to do is create a UITableView.
Here is a link to a super basic 'how-to' to get you started with tableviews.
http://www.appcoda.com/uitableview-tutorial-storyboard-xcode5/
Once you've got the basics down, you'll want to take that a step further with learning how to customize the UITableViewCell within your tableview, so you can accomplish the look you've detailed in the question.
http://www.appcoda.com/customize-table-view-cells-for-uitableview/
I'm not sure I can help anymore than that at the moment. Jump in, learn tableviews, and start searching on OS to answer the million other questions you'll have a long the way.
Good luck!

UITableView accessory view

I have created a UITableView populated by data coming from mysql (using NSJSONSERIALIZATION). Now the problem is one thing. What I retrieved was the product name. I want to have an accessory view (arrow like on the right side of cell). once clicked, new view and load details of that product there.
I know it can be done, but don't know how or where to look for a good tutorial. Any recommendations?
am really new to iOS development.
thanks a lot
Use this tableview delegate.
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
...and in cellForRowAtIndexPath: add this.
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

Changing UITableView functionality on button press

I'm trying to figure out the best way to do this - I have a UITableView of items which the user has previously selected and which is stored. When you click an item it takes you to a detail page. What I need is to be able to click a button below the table view which reloads the table and changes the accessory so its a tick instead of a disclosure, then the user can un-tick the items and remove them from the list, before clicking another button which reloads the updated table and restores the disclosure accessory.
Question is, what is the best way to "remember" which way to handle the table reload after the click so it knows which way to display it? Would you use the NSUserDefaults to store a flag on the button click or is there a more elegant way to do it? I guess I could use the status of the button, whether it's in one state or another, but I'm guessing there is something in-built I'm missing.
Hope that makes sense - thanks.
Usually I have some 'model object' and depending on array of those objects I construct table. My cellForRowAtIndexPath looks like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
InterestsCell *cell = (InterestsCell*)[tableView dequeueReusableCellWithIdentifier:kInterestsCellID];
if (cell == nil) {
...
}
Interest *i = (Interest*)[self.interestsArray objectAtIndex:indexPath.row];
cell.myTfSubview.enabled = i.isChecked;
return cell;
}
If you don't have your model then you can create array of BOOL values and store flags there.

Displaying two UITableView

I need to show two UITableView but I'm not sure the right way to do it. I tried to use two UIViewController each with UITableview and load those views to the parent controller's views but I had doubt on this implementation. I can't use UISplitViewController because I need more real estate on the left also I have navigation controller as the root controller. Then I saw Amex for iPad app (check the first screenshot) that seems to be the best way to show the tables. Is it a UIPageViewController? Any suggestions on the implementation? Many thanks.
It looks like an UIPageViewController with 2 UIViewController each with his own UITableView, but I would have to download and install to be sure of what I am saying. Nothing spectacular about it (at least in the implementation part if I am correct in my assumptions), the look, on the other hand, is very nice.
You can put 2+ UITableViews in one viewController. Then in all your delegate and datasource implementations check which tableView is being asked for. For example:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([tableView isEqual:self.tableView1]) {
// return cell for table 1
}
else if ([tableView isEqual:self.tableView2]) {
// return cell for table 2
}
}

iOS - How to display the same table view after selecting an item

My iOS app is current transferring control to a detail view when an item in a UITableView is selected. This ia a quiz program, and I'd like to change the app so that it just redisplays the same table view with the correct answer highlighted when a row is selected. What's a good approach for doing this?
Is not clear to me if you know why the detail view is appearing. So I'll explain just in case. If you are giving control to a detail view is because somewhere in your code you are pushing that detail view. It depends on what kind of UITableViewCell you are using. If you are using one of the defaults styles, your detail view is probably been pushed in either:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
or
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
If you are using a custom cell, then you need to look for the method in charge of pushing
that detail view.
I think a good approach would be to:
Remove that pushing wherever it is.
Not to use an `UITableViewCellAccessoryType, if you are using one.
Do something similar to the following on your tableView:didSelectRowAtIndexPath:
Find the row for the right answer in your model array, according to tapped cell.
Use that row number to generate an NSIndexPath.
Use that NSIndexPath to find the correct cell with cellForRowAtIndexPath:
Call setSelected:animated: on that cell to highlight it.
NOTE: If your quiz has more answers than the amount of UITableViewCells that fit in the screen you should scroll your UITableView to the right answer for better UX.

Resources