iPhone: can I add Tableview in cell of another tableview? - iphone

I have to create a UITableView. Suppose there are 2 table views TableView1 and TableView2. TableView2 will contain suppose 2 text boxes. And I have to add TableView2 in one cell of TableView1. Finally I have to access the values of text box from the inner TableView2 in the TableView1.
I am new to iPhone development. In short I have to add a tableview to the cell of another tableview. I don't know whether this is possible. If it is, would someone please provide me some link or code to do it.

This is not look good design. You can also use two text field in same cell and make it more in height also make a horizontal separator. also if you have only two textfields then make them global and declared as property.And add in a particular cell.
Read some tutorial for making custom cells and give some time to learning these tutorials.
See this link and by googling you can easily find more on this topic.

Yes, you can do. Of course, It is not good practice to do like this. Still you want then here is the trick to do that.
Create Two ViewController and inherit them using UITableviewController say ViewController1 and ViewController2.
Now in cell of Tableview1 add ViewController2 object. So Tableview2 would in Tableview1's cell. Now you need to handle inner tableview delegate & Datasource in Viewcontroller2. So you don't need to do lots of if.. else in same view controller.
Now you can take two textfield in Viewcontroller2.h file and assign cell's textField to that in cellForrowIndexPath method. So you can access both the textfield any time you want.
I know my answer is bit confusing but ask me any question if you have.

Related

Using UIScrollView to scroll both TableView and Main Stack

hope everyone is having a great day.
In my ProfileViewController i have 3 views, one that holds the profile information (like photo, full name), then another that has a button, and the last that holds a tableView. All these are in a Main Stack. How can i implement a UIScrollView , that scrolls everything simultaneously ?
I have searched around but could not find something that would work for me.
I think you are in wrong direction!!
What you need to do is
Remove Main(StackView) and add one Single UITableView Or UICollectionView
Create Two different Cell(UICollectionViewCell or UITableViewCell) one for Profile Header and another for podcast and register it with table or collection view
in delegate return 2 for numberOfSection(_:) method and in dequeueReuseableCell method check section and return appropriate cell.

How to insert button into Table view cell Swift

How do add button on each cell for table view cell so that it know which one is from.
Example:
Data have 2 products which is product A and product B, then I store them in array which is [product]. Inside the array, there have their detail which are name and price. So, I have successful making these into the table view.
The Question is how to insert button into the table view cell?
Example:
When button pressed, it know which row is pressed which is when press the 2nd row will be product B.
I'm newbie here. Need help, thanks.
You have two options:
Option one - easy, quick, bad:
create instance of the UIButton with the frame to position where you actually want the button to be placed. add tag property as well as the action. Action would be the same for the each button and you could use that tag to do the different actions.
Option two:
Create subclass of your UITableViewCell. Add all the element you want there. Create action for the button and you can set the target UITableViewCell or UITableViewController. If you choose UITableViewCell to be the target you will have to implement delegate, confront that delegate in the UITableViewController and implement that delegate function there. If you choose controller to be the target, you will have to send it to the cell. You will have to set tag as well.
I would use option two because it is scalable, easly maintained, and you have visual of the cell. Adding more components is easier as well.
I don't thing you need to put button or any other control in UITableViewCell to identify the selected cell.
All you need to do, is to implement following delegate method in to your class where you have implemented UITableView
tableView(_:willSelectRowAtIndexPath:)
tableView(_:didSelectRowAtIndexPath:)
and these methods will let you know selected UITableViewCell indexPath and you can do what you want to do on selection.
Note: do not forget to set your UIViewController as delegate to UITableView (almost like you did in case of DataSource).
Here's a Github repo you can refer, the code is not in good shape though.

Newbie TableView challenges

I am building an app that requires a tableview control so that users can select multiple rows and act upon them in some way. I find it easy to load my data and display it using the UITableViewController but it seems when I do it this way I am unable to place any other controls on the page, such as a toolbar to give the user some actions to perform on the selected rows. I can place a toolbar control on the form in the storyboard, but it doesn't render in the emulator.
Using a UIViewController and placing a TableView on it seems to come with its own set of confusing challenges (that will make total sense once I conquer them).
Is there any advice for a smooth way of getting a table view with toolbar controls? Thanks!
don't use a TableViewController. Use a Standard ViewController, then add a UITableView to it, and adjust the size. This way you will be able to do whatever else you want on that view without limiting yourself to the tableView only functionality.
When you do this make sure you add the datasource and delegate to the connected table. Then add cellForRowAtIndex, number of sections, number of rows, and whatever other delegate methods you need for your table.
Good luck
Is there any advice for a smooth way of getting a table view with toolbar controls?
Yes there are few ways to accomplish this, one way would be adding a footerview to your tableviewcontroller check these
http://developer.apple.com/
UIButtons on tableFooterView not responding to events
Easier way to add this is using storyboard.
Just drag and drop a view in to your tableviewcontroller, then you can adjust the views size and put anything from objects menu to inside of the view.
Now the problem with that is view will not be stable position at the bottom of the page like a tabbar. Lets say you have only 1 row in your tableview, footer view will go up just below that 1 row, lets say you have hundreds of items in your tableview toolbar will be at the bottom of the rows.
The other solutions for your problem would be either create a custom view and adding it to current view or window (this is little bit adbance),but if you want this just google it.
Or just as you said, create a viewcontroller and put a uitableview inside. Dont forget to add <UITableViewDelegate,UITableViewDataSource> to your .h file and then you can call UItableview delegate methods.\
Good Luck.

table view cells with different controls

I was working with the grouped table view , and i wanted different controls for every row i.e switch control for 1st,radio button for 2nd ,checkbox for 3rd and so on.. how can this be implemented programmatically that is without using interface builder
thanks in advance
CharlieMezak said is right, you need to create in UIControls directly in cellForRowAtIndexPath , and add as subviews to contentView of the cell
For reference see the link below
http://www.e-string.com/content/custom-uitableviewcells-interface-builder
the link specifies the code to create cells programmatically as well as using IB.
Table View Programming Guide for iOS
Read the programing guide, and remember to use different CellIdentifier for each type of cell.
This is a pretty vague question.
Obviously you need to provide the cells to the tableview in its cellForRowAtIndexPath delegate/datasource method. So, either in that method or during the initialization of your view controller, build the UITableViewCell instances that you need, adding the various controls that you want to them as subviews and connecting the controls to your view controller so you can detect when they have been changed. Then just return the appropriate cell in the cellForRowAtIndexPath method.
Personally, I think it's a lot easier to use IB in cases like this. Just create an IBOutlet instance variable for each custom cell you want, and return the right cell in cellForRowAtIndexPath.

Load a nib into an UIView?

I need some help.
I've got a segmented control. if segment 0 is clicked it shows a UIView, containing a uitableview.
If segment 2 is clicked it shows another UIView which contains a uitableview as well.
These two tableviews got the same datasource. But i want the second tableview to have an other datasource. SO i thougth of loading another nib into the second uiview containing a the tableview which gets its data from the corresponding .m file.
I don't know wheather this is the best solution. If you've got any other ideas let me know :)
thanks in advance!
I think the best way to do this is to have just one table view. In the cellForRow method have a conditional which queries the state of the segmented control. Depending on the result of that if statement, return the type of cell you want. In addition, in your segmented control callback you'll have to do a [tableView reloadData]
Do you know that you can have the same tableview display different data sets? You don' have to use an entirely different NIB just to change UITableViews so you can display different data. Or am I understanding your question incorrectly?