I am contemplating on what is the recommended way to display a notice that there is no data for a specific section in a collectionView/TableView
One way is to create a special cell and to put that in instead of the data cells. That feels odd, since the "empty notice" cell doesn't correlate with the data, which means I would need to spread a lot of conditions in didSelectItem, configuring the cell, etc
Using https://github.com/dzenbot/DZNEmptyDataSet is appropriate only when the entire view is empty, not in a specific section
Another way would be (which is what I do now) to insert a UIView into the place where the data would be as a subview of the collectionview, But this also would require maintenance when reloading the data, scrolling, tapping. Also this requires calculation of where to place the view, which means I need to change it per collectionview, since it is not part of the collectionviewlayout
Is there a recommended pattern to deal with these situations?
Maybe you shouldn't have that section at all if the section contains empty data. Reduce section count by 1 at - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView datasource method and recheck the cell to be displayed during - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath datasource method.
In reply to you, I think you would have some type of "Add" button to add comments, photos, friends, etc. Make the option to add a comment or photo less obtrusive than taking an entire tableView row, and omit the comment or photo section, if there are no comments or photos.
If you put an "Add comment" or "Add photo" row while displaying details, users will likely get tired of unnecessarily reading that, as they use the app.
Note that tableViews generally handle insertion in editing mode, and when not editing, no section is shown if it is empty.
Related
I have a question regarding set up table view with hard coded data. Sometime developer face with situation when you need to display data that is received not form DATA MODEL layer, but stored as hard coded information.
What I mean
For example when you build Settings screen, there can be some UITableViewCell like:
CustomTableViewCellName - Configuration data: placeholder name/surname, icon.
CustomTableViewCellPhone - Configuration data: phone placeholder, icon.
CustomTableViewCellLogout - Configuration data: logout/sign out text, icon.
Let's assume I have 3 table view section and each section contains 5 different cells that I described above as example of different cases.
if we will implement all these cells in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.section)
..... A LOT OF CASES THAT CONFIGURE EACH CUSTOM CELLS
As you see there is no DATA MODEL only placeholders and icon images that will be stored in UITableViewCell as a default data for the labels, textviews image views and etc. I don't say about model here as about model USER for example that stores user name, surname and etc. I mean that I need to store some default data somewhere, maybe in a dictionary. Actually a placeholder is just a placeholder, though it has a TEXT and TEXT is a data =)
In this case we have data for configure default state of views and data that will use for example as model USER.
What I worry about that I will have a big Switch and very Massive View Controller.
I would like to create few methods that will represent sections with a switch using indexPath.row in them:
- (UITableViewCell *)cellsForSection1:(NSIndexPath *)ip
- (UITableViewCell *)cellsForSection2:(NSIndexPath *)ip
- (UITableViewCell *)cellsForSection2:(NSIndexPath *)ip
and move these methods to Interactor that will return for me a cell. I know that Interactor should prepare only data and push it over Presenter to a View.
Maybe I overthink a problem. I just don't want to have a big switch in VC =) I understand that there is no right solution. But maybe someone faced with it.
One more time in my case the data are the default cells that I want to configure somewhere to prevent overload view controller. Yea maybe it looks crazy that cells are data, but look at ViewController that contains 1000 lines of code for configuration cellForRowAtIndexPath is also crazy =)
If your data is static, then instead of Switch cases I will prefer to use enumerations. Use Enums and in its body give tag to your each section and then in cellForRowAtIndexpath enumerate this Enum and display your data accordingly.
Consider creating a seperate class to act as your tableview data source and then have your view controller instance one. That way you avoid having an overly large VC class and your code is more easily unit testable.
switch(model.id)
case cellKind1:
cell = [tableview dequeue..];
//do
cell.data = model
//don't do like this
cell.title = model.title;
cell.abc = model.abc
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!
I am new in iOS programming and I would like to create I view for the settings of my app that looks like iPhone's one (like in the picture). Some of rows will call other views when taped and other will not.
What is the best way to do that? I thought about TableView with prototype cells, but is there a best way to do it? or a best-practice for what I want to do? Or maybe a tutorial online?
The fast way in Interface Builder:
Use a UITableViewController, make it STATIC and use the GROUPED style (all in IB).
You can setup the cells to show disclosure indicators (or not) in IB also.
You can segue directly from the rows or the UITableViewController to where you want to go.
If you segue from the UITableViewController, implement the "didSelectRowForIndexPath" method and call "performSegueWithIdentifier" accordingly.
A structure like this is best by UITableView.
First you select how many sections you want, and customize each section with a data structure that you have to be filled with (Probably an array.)
Then you fill up each rows inside
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
method, and call your value from the array/dictionary that you have.
for going to a next view when clicked upon
Use the method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
Hope this helps
The best way to do that is using static UITableViewCell.
See UITableView Programming.
The optimal solution here is undoubtedly UITableView. This is because firstly, you have the need to display a list of options that would have external links to other pages and UITableView is designed and used for this purpose.
In an addition to that, if you want, you can also expand and collapse the rows of your parent TableView into a Child TableView i.e a UITableView as a subview of its parent UITableView.
Put up a UITableView and populate it with UITableViewCell. That will be just fine with the requirement you have.
Hope this helps.
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 have a question regarding a design convention.. See I have this tableview filled with editable information. Editable as in changing the text in the right detail of the cell, not as in deleting or moving a cell. I wonder how to design/model this, the only Apple product that I know has this feature is the contacts app. The solution there is to make lots and lots of groups, but this does not fit my problem at all, partly since I already have groups. The simplest way would just be to have the right detail be a text field and enable it in edit mode, but that would of course be a stupid solution since no visual feedback is given..
Any ideas on how to design/model this, or how Apple would like to have it?
EDIT:
To be more clear in what I mean, this is a screenshot explaining what I have said. Once again, my problem is how to show the table cells when they are in edit mode. All values are changeable, and none of them have any kind of presets to choose from, they are all based on text written by the user. This part of the app is basically a CRM system, where you can edit all the information about yourself.
What you are trying to do is pretty standard for Dynamic Prototype cells (as opposed to the Static Cells you probably used to make that screenshot). You're best bet is probably going to be to just watch a couple of these tutorials.
To give a quick summary, you're going to put all the strings you want to show in the Value part of your screenshot into an NSArray. There are UITableViewDelegate and UITableViewDataSource methods that will automatically handle putting the ith item in the array into the ith cell (so the 3 item in the array will go into the 3rd cell, etc.). To allow the user to edit things, you're going to want to make a whole new second screen (this is where you're UITextfields will be). When the user goes back from the second screen to your table screen, you replace items in the array with whatever the user entered in the UITextfield, then tell the UITableViewDelegate methods to reload the table with the new Values.
Most of the tutorials I linked to probably aren't going to have anything about having multiple Groups, so I'll add a bit on that here (what follows will probably only make sense if you watch the tutorials first, so I'd suggest following along with the tutorials, then coming back here afterward and making the changes I'm about to suggest). The NSIndexPath that is sent to the tableView:cellForRowAtIndexPath method contains information on the cell row and section. Each "Group" is it's own section (I'm really not sure why Apple decided to use two different names for the same thing, but so it goes). The easiest way to do this is to have different arrays for each section (so you'll have lines for NSMutableArray *firstSectionArray = [[NSMutableArray alloc] init];, NSMutableArray *secondSectionArray = [[NSMutableArray alloc] init];, and so on). Then, at the very top of your tableView:cellForRowAtIndexPath method, you put some if statements in to see what section of the table you are "building", and use values from the correct array accordingly. Something like:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0)
{
// bunch of stuff from the tutorials here to create "cell"
[cell.detailTextLabel.text = firstSectionArray objectAtIndex:indexPath.row];
// bunch more stuff
}
else if (indexPath.section == 1)
{
// bunch of stuff from the tutorials here to create "cell"
[cell.detailTextLabel.text = secondSectionArray objectAtIndex:indexPath.row];
// bunch more stuff
}
// else if (keep going for however many sections you have)
return cell;
}