I have PersonCell with xib and .h, .m files and person's characteristics. So, depending on PersonCell.state, I want to show different number and kind of characteristics.
I want to use a specific Parameterview with titleLabel and constantLabel, so i can add those parameterViews with for-in array. I can't use constraints in code, coz of cell reuse.
How to do that? I see only stupid way - add every characteristic view to PersonCell and depending on the state, show and hide them. But there are 50+ different characteristics.
You could try use a tableView instead of the array of views. For example, the PersonCell could have a tableView inside, where each cell contains a ParameterView.
From the array you already have, you could implement UITableViewDataSource in the PersonCell. I think this would be a pretty simple solution when the PersonCell depends so much on the state.
Let me know if you have questions or you need more specific information.
Related
Imagine this scenario:
I've 10 different and custom UITableViewCell: one with a textfield,
one with a button, one with some labels, one with a textview, one
with an imageView and so on.
I've a ViewController with a tableView where I wanna display these cells.
The number of cell displayed can vary based on some conditions (and also the height, the background color and other parameters)
The user can interact with these cells
What is the best way to design this in respect of the MVC and maintain the ViewController lightweight and maintainable as possible?
How to take advantage of Swift language in doing this?
Is there any famous and consolidate design pattern to apply?
i will try to share some of my experience:
Create separate custom UITableViewCelll as per requirement like : textfield, textview, imageview, label etc. this class must not dependent on data calculation it is only for cosmetics UI. that means there must not be any method like updateCellWithData:(someDATAObj). This logic must go in some cetegory as discussed below.
Register separate custom UITableViewCelll with your tableview.
Create separate class (NSObject) as datasource and delegate for your UITableView.
Use category to populate data in your custom UITableView Cell. some thing like updateCellWithData:(someDATAObj).
Use constant file for your constants like height for tableView Cell, reuse identifier names, notification name.
try with some code atleast, then we can help you with best.
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.
Is it possible - and how - to talk to any other visible UITableViewCell from within another cell inside a UITableView?
I have two kinds of cells, lets say blue and red ones. the distribution of the two kinds of cells inside the listview is randomly. the problem I need to solve is: I want to make all visible red cell communicate.
Thnx!
The easiest way to communicate without passing pointers, defining protocols and delegates is always NSNotification.
You can think of NSNotification as a kind of "switchboard" which allows you to pass custom messages across your app, without worrying about "connecting" all dots.
You post notifications with postNotification,
you must set observers and their target methods in the object in which you want to handle the notifications.
You could set different cells to different UITableViewCell derived classes, and post/receive notifications among them.
Remember to:
- remove observers before the object they contain gets deallocated (e.g. in viewWillDisappear for viewControllers, etc).
- don't add your observers twice.
Maybe for some cases it would be enough to use the simple
let selectedCell = tableView.cellForRowAtIndexPath(indexPath)
to solve the problem!
I have a custom UITableViewCell class that I use to display quite a complex set of data.
Essentially the cell displays a Match object. But in doing so it displays information about the two Teams, the score, the time elapsed and so on.
Thinking about MVC and clean code.
Should I just pass in the Match object and let the cell do everything? Or is it better practise to expose the different elements of the cell (team1NameLabel, team1ScoreLabel, team2NameLabel, etc...) and set them all individually in the UITableViewController?
The first way makes the UITableViewController cleaner but then I'm relying on the UITableViewCell to "know" about the Match class, the Team class etc...
The second way makes more work for the UITableViewController but then makes the UITableViewCell a "dumb" display. All it does is then lay out the information within the cell. It doesn't know anything about the information it is displaying.
I would follow these rules:
The cell should just have the outlets for displaying the various bits of data. It is a view so it should not contain any logic.
The controller should get the Match data, parse and make calculations if necessary, and populate the cell. It is a controller, so that is its primary function in a MVC context.
IMO it is better and more MVC-like to pass the Match object to your table view cell.
Lot of the code you find on the internet(even Apple examples if I remember well) is not doing that. You can see many times a configureCell method within the view controller that is called in tableView:cellForRowAtIndexPath:.
I prefer to pass the model object object to the cell, it makes my view controller code simpler and also it is simpler to unit test: when I test my view controller I only verify that the model object is passed to the cell, and then in the table view cell tests I verify that the test of the labels is set to the expected values. Someone may say that this is making the view knowing about the model, but I don't see any big problem on that.
Both ways are fine, but personally I would go for second option, i.e. table view exposing #property and, if necessary, outlets.
However, if you really want to go for the first option, I would suggest to have any objects passed to the cell to implements a protocol exposing few methods:
#protocol tableViewCellProtocol
-(NSString*)titleForCell;
-(NSString*)descriptionForCell;
Then you can "pass the protocol" rather than the object.
[mytableCell renderObject:objectImplementingProtocol];
This way you slightly decouple the objects itself, and prepare cell for reuse with other objects.
I want to put different cells in a table, and all of them is kind of complexity, so I want to use nib for these cells. However, i really don't know how to use multiple cells in a table. Can you help me?
You can use as many custom UITableViewCells as you want in a single UITableView. The issue here is: What kind of logic do you want to implement? Only you can define it. You can start by using this tutorial to see how to implement the custom UITableViewCells:
http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html
You can then reply to my answer to tell me what logic do you to implement. For example:
first 3 cells one with a type of UITableViewCell, then 3 with different UITableViewCell.
Alternate between cells (for instance, 4 different UITableViewCells in a row)
You need to specify what you need. :)