I'm new in objective C.
I am using an static table view controller to load data related to my Class GAME.
I have 2 sections, in one section are cells related to the game, and in the last section y want to add the players who were in the game, which can be as many as I want. For this I created a button in the last cell of the first section addPlayer.
I want to create a new row in the 2nd section each time I touch addPlayer. how can I achieve this?
Thanks!
PD: Do I need to have the first row of the section 2 added by story board? Don't I?
if you want to add cells each time you add a player you could have, for example, a NSmutableArray which will contain the players and when you create a player you add it to that array and call the table's [yourTable realoadData] method. Then, your UITableViewController cellForRowAtIndexpath: method should loop through that array and add the cells dynamically. Use the array's count to keep track of the indexPath. You might have to keep track of the section so that you add the initial cells of the first section (the Date cell, the Pozo cell and the button cell) Keep in mind you will need to add a prototype cell in the Storyboard and set its identifier so that you may reuse the cell otherwise you will have to keep creating and adding cells to the table, which is not efficient.
Let me know if it works or if you have more questions!
You can't add static rows at runtime, you should go for dynamics.
What you can do, but only if you are using a Table View Controller, is create the maximum number of rows that you need, and override the – tableView:heightForRowAtIndexPath: to return zero height rows, at the indexPath that you don't need to show.
Related
I am making an iPhone app.
In this application I have to make a look like below.
I am not allowed to use collection view.
I am using tableview, and custom cells. Which I am easily able to incorporate. Means taking 3 subviews in Custom cells. And making a look.
Here the problem is In a cell, how do I distinguish each object. so that I can call each object, to set an image on image view.
Is there any Object oriented mechanism to distinguish all 3 objects in a cell ?
Try to get the data as NSArray of NSDictionary containing an array of 3 objects that you want to display on cell.
Assign tag to UImageView in the custom cell.
In cellForRowAtIndexPath, get the 3 objects and apply image using switch case.
Well everything depends on how you get the data from the server
You can use outlet collections which will give you an array of UIImageView and you can assign different tags to the imageview so you can assign to them.
You can use these guides to understand how outlet collections works:
http://nshipster.com/ibaction-iboutlet-iboutletcollection/
http://useyourloaf.com/blog/interface-builder-outlet-collections/
All the standard procedures should work to achieve this but then it only depends on how nice you want to do this.
The straight forward procedure is to expose the outlets of the image views and labels in the cell and assign the correct values to those when dequeuing/creating table view cell.
The first upgrade would be to rather expose 3 setters on the cell to simply set your model to each of them which will then internally set the images and texts inside the cell.
The next thing you may do is to rather insert an array of objects (always sending up to 3 in your case) instead of having 3 setters.
At this point you may actually rather use a collection view INSIDE the cell and make the cell a data source for the collection view. But this is totally optional.
Now since you may still dislike the table view data source you may create another model which contains an array of objects (again up to 3 in your case) and make a system that will distribute your original array of objects into the array of these containers.
If then you need to handle buttons or other touch events they may be handled with collection view delegate or 3 buttons and in both cases I advise you to handle those in the cell and create a custom delegate for the cell which will report the event with the appropriate model.
This together generates the following:
When you receive the data call a container class to distribute your array of objects (into groups of 3) and assign it to your table view data source (view controller usually)
Number of rows is the same as number of containers in the array
Cell for row assigns the container with row index to the cell. It assigns self as a delegate
Cell internally handles the object distribution either via collection view, separated outlets or outlet collections.
Cell handles actions and reports them back to the delegate (- (void)myCell:(MyCell *)cell selectedItem:(MyObject *)item;)
The cell delegate can again handle what to do upon reported actions
Also if you want to avoid a collection view inside the cell you can create a custom view using xib so you do not copy the labels, image views and such. Then simply create 3 of these custom views inside the cell. Also by using inspectable and designable these views will be visible inside the storyboard.
First I'll say that a restriction against using UICollectionView is silly. Are you still targeting ios5?
I'd look at it like this.
make your own view class for the 'subcell' let's use this term for any single instance of the 3 views per cell. I'd subclass UIImageView, adding the label for the name down the bottom and a 'setSelected:' kind of method to highlight when selected via user interaction by drawing differently.
make a UITableViewCell subclass to host and layout up to three of these subcells. I say up to 3 because the last cell may contain 1 or 2 subcells and not 3 if the total people to represent is not divisible by three.
Selection Logic: You'll need to override 'setSelected:' because you want to deselect and select only subcells, you don't want the whole cell to highlight on selection, only a third of it.
You'll also want to implement touchesEnded: in this cell so that you can figure out which of the three subcells was last touched, and you'll need to be able to query or communicate this back to the controller, probably using delegation. If the cell can communicate back whether selection was in subcell 0,1 or 2 then this together with the UITableViewDelegate didSelectAtIndexPath should map to your model nicely - selectedPerson = myArrayOfPeople[ (indexPath.row * 3) + subcellIndex ]
You'll be able to decorate your cells in cellForRowAtIndexPath: in similar fashion..
personOne = model.arrayOfPeople[indexPath.row*3]
personTwo = model.arrayOfPeople[indexPath.row*3 +1 ]
personThree = model.arrayOfPeople[indexPath.row*3 + 2 ]
I need to display a table with in my iPhone app:
neither the number of cells nor the contents are known at compile time, but only at run time.
Views for each cell may differ, one cell has textField and another may have some other view control.
Should I consider Static or prototype cells?
Should I consider tableViewController or viewController with tableview in it?
Any thing I need to consider before I start coding? Thanks in advance
For The issue of dynamic Number of cell at Run time, you can call reload data for table view at any time you have the data source ready.
Prototype Cells should be used with no problem.
Simple Table View will be sufficient for the task.
You have to make cell, either in code or in storyboard, for each type of cell you want, 1 table View can have multiple types of prototype cells, Just name them differently and then make the objects of only the specific cell of which the data is best suited.
It is not that difficult but do handle the data source with extreme care.
Should I consider Static or prototype cells?
If you know all possible subview combinations that your cells might need to display the data appropriately, and they are relatively few, make one prototype for each. for example:
One text field,
Two labels,
One image view and a label,
...etc.
Otherwise, just use the plain-vanilla UITableViewCell (no subclassing) and remove/add subviews at runtime when reusing them (that is, inside the method -tableView:cellForRowAtIndexPath:).
Should I consider tableViewController or viewController with tableview
in it?
The only reason I would choose UIViewController + UITableView over UITableViewController is if -for example- I needed the table view to only take up part of the main view's bounds/screen, and display some other subview in the remainder. Otherwise, you get so much "for free" with UITableViewController that there's just no point in implementing all of that from scratch.
You have to choose prototype cell, u can create different types of cell depending upon your requirement.Any think ok for u, u can create tableview controller or view controller.
I used to use static UITableView but the table is too long and overflow the memory.
I switched a dynamic prototype UITableView with 1 type of cell, which has an UISwitch in it.
One of the cell, when turning on the switch, will turn off the switch of another cell. These cells have fixed index.
The IBAction method is in my UITableViewCell subclass and I don't want to add the UITableView as a property in my UITableViewCell.
How do I achieve the above effect?
I'm planing to use an id or similar to distinguish between the cells as each cell's switch has different effects, that doesn't solve the above requirement.
Thanks,
I would add a block property to your cell which you can use to notify your controller of changes in the switch. See my answer below to a question on this:
How can I get index path of cell on switch change event in section based table view
All your logic can now be implemented in the view controller.
You are best to create a data model in the view controller which the cells simply provide views and controls onto. When you flick one switch and the block fires, update the data model and simply reload the table. Any cells affected will show the new data model positions for their switches. Avoid using one cell to adjust another. Just update the model and reload the cells.
Is it possible to have a UISearchBar on Static Cells, I really need the static cells, and there are like 40 of them, so how can I use the UISearchBar in it?
My Cells are filled with a button (Every name has another button (Every Cell has another name)).
So basically I need to search for the button text onto of each cell, and for that show the cells that contain the search string.
Can someone provide help please?
Thanks for your time and effort.
When you use static cells, as you probably already know, you don't implement the delegate methods in your UITableViewController. However, if you want to only display some of your static cells (or change the ones displayed), you can implement the delegate methods and call super on them after making whatever changes you need.
So by this I mean... If I have a static table view with 1 section, 3 cells, and I only want to display the first cell and the last cell, I would implement - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section, and have it return 2. Then I would implement - (UITableViewCell *)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath, and when it gets called with the index path with section 0, row 0, I'll call the superclass method with section 0 row 0. And when it gets called with section 0 row 1, I'll call the superclass with section 0 row 2. Basically, I'm mapping from indices of rows that will be displayed to the indices in the complete static table as defined in the storyboard.
However, I would suggest that you don't need to use static cells at all, and it will be a lot simpler. You won't have to deal with this mapping to make this work. You would maybe want to use static cells if your cells have UITextFields in them, or some other type of view that could cause problems for reuse. But in your case, a dynamic table would be much better to use because you don't have any content in a cell that would need to possibly be saved if a cell were going to be reused at a different index.
If you go the dynamic table route, you just need to implement the delegate for the search bar, and as you type you will store the list of filtered results in an array stored in an instance variable or property, and reload your table, using that array as the data source.
i want to display name phone number and salary of a employee in an ipad in the form of table with multiple columns for that i take three table views and successfully displayed data in them . the table views are scrolling independently.but i want to implement if one table view is scrolled the second and third must be moved parallel. can any one please tell me how to implement that one....
If you want all your UITableViews dependants and scrolling at the same time, there is no reason to create multiple table view.
You can just create un custom UITableViewCell with the layout and style you want! This will be easier and will consume less resources.
find out position of cell in first table depending on that change position of cell in next table.
You can use following property of UITableView - – scrollToRowAtIndexPath:atScrollPosition:animated: