No data at all is showing in UITableView - ios

I have created numerous Xcode projects to see if this was a single project problem but no. The problem that I am getting is that when I populate a UITableVIiew with either local data or data that is stored in a Parse database it does not show.
I have tried re-installing Xcode, cleaning my project and walking through the code/project to see if I'd made a mistake but everything looks in place.
An example is that I created a UITableViewController with a UIImage in the cell and when I build and run the project it does not show up.
Here is an example:
Thanks in advance.

You should implement two required methods of UITableViewDataSource.
Something like that:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
return cell;
}

This would go quicker if you just posted the available code... Annnnyway...
The reason I ask is because if you've only done this in storyboard, and it's a dynamic table, the table looks to your numberOfRowsInSection method to see if it should be populating the table or not. If this is 0, then you'll get no rows no matter what you put into the storyboard.
Also, if you've got a cell identifier of cell in your cellForRowAtIndexPath, but you haven't identified your storyboard cell as cell, then you'll still get blank cells.
Furthermore, if you have implemented these two methods correctly, you need to ensure that this table is hooked up to that class as it's dataSource, either via storyboard or programatically.
But these are all just guesses because I would need to see code to figure it out.

If you are using a UITableViewController, have you changed the cells to static?

You said you don't have any custom code other than what Xcode provides. That is your problem.
You need to implement the UITableViewDataSource methods in order to install your data into your table view. Xcode does not do that. The code Xcode puts into your view controller tells the table view that there is no data. You need to report that there is at least 1 section, and at least 1 row in that section. You then need to write code for cellForRowAtIndexPath that installs data from your model into your cells.
Nikita lists 2 of the 3 methods that you must implement before you table view will do anything.

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!

Infinite Scroll on iOS with Swift

I'm an iOS newbie and I would like to know how to detect when the user scrolls and reaches the bottom of an UITableView so I can load new data into the table.
I would also like to know where such a method should be implemented (the tableview's class or the view controller in which this tableview exists)
Cheers!
You can use -(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath to and check if the last cell will be shown in the TableView's data source.
iOS' TableViewController takes care of that automatically. It asks its datasource only for the currently visible rows of the table (tableview.cellForRowAtIndexPath)
See the documentation for the UITableViewDatasource Protocol Reference
If you've got a "known dataset" (as in, you don't need to make a network call to fetch new data), then like #zizoft said, it'll be handled automatically in tableview.cellForRowAtIndexPath
If, however, you've got an "unknown dataset", (as in, you'll need to pull down data from the internet), you'll need to do something a bit more interesting - #ansible's suggestion would be appropriate in that case.

How to create a view for settings of my iPhone apps in Xcode storyboard

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.

Why can't I change the custom class of my UITableView?

Apologies if this question is a little basic but I've spent several days trying to understand the root cause of this problem without any success.
I am working on an app which relies heavily on UITableView objects. I can successfully use a UITableViewController object and display information in a table but I need to be able to have multiple tables on screen citing data from multiple sources and the UITableViewController seems to be too limited.
I would like to be able to place multiple UITableView objects with the storyboard then create custom class files which manage the tables. Unfortunately when I've tried this, XCODE doesn't let me select these custom classes to manage the tables.
Although I've found some potential workarounds online I want to understand why selecting a new class to govern a table view is not possible.
[I wanted to post images but apparently I can´t until I have a better reputation...]
It depends on exactly what you did. But, you should really take a different approach:
If you can, use a single table view with multiple sections (with headers / footers).
If you can't do that, create a separate table view controller and table view for each section of information that you want. Then, your 'main' view controller should act as the parent and add all of the other table view controllers as children (addChildViewController:) and their views as subviews. This approach will keep your code segregated and organised rather than trying to have one controller manage many disparate views.
.h
{
UITableView *objlefttableview;
UITableView *objrighttableview;
}
.m
viewdidload
{
if(!objlefttableview)
objlefttableview=[[UITableView alloc]initWithFrame:CGRectMake(0, 87, 227, 681) style:UITableViewStylePlain];
if(!objrighttableview)
objrighttableview=[[UITableView alloc]initWithFrame:CGRectMake(227, 87, 263, 681) style:UITableViewStylePlain];
[objlefttableview registerNib:[UINib nibWithNibName:#"View" bundle:Nil] forCellReuseIdentifier:#"leftCell"];
[objrighttableview registerNib:[UINib nibWithNibName:#"ViewR" bundle:Nil] forCellReuseIdentifier:#"rightCell"];
}
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self->objlefttableview==tableView)
{
}
else
{
}
}
so what i just did is i created two tableview objects and then i gave two different custom cell to both of them
If u need more help in this approach do ask
The problem was that where as the UITableViewController object is by default the UITableView delegate and it's datasource, a UIView is not even if it inherits from UITableViewController. I hadn’t specified that and it seems that neither 1 nor multiple tables could function as they had no class governing them set to be delegate and data source.
By specifying in the ViewController’s .h file that it was also the delegate and datasource for the UITableView like below (the delegate and datasource commands should be surrounded by triangle brackets but they aren't displayed on this for some reason):
#interface DHViewController : UIViewController [UITableViewDataSource, UITableViewDelegate]
and in the .m file’s viewDidLoad method specify that it was the data source and delegate for both tableViews like so:
self.tableAnswers.delegate =self;
self.tableAnswers.dataSource = self;
self.tableQuestions.delegate =self;
self.tableQuestions.dataSource = self;
and implementing the necessary methods in the .m file:
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
…both tables can be independently managed and displayed on the same screen.
Thanks a lot to all for your help!
FYI (I think I will still go for #Wain ’s idea of having a single table with section specific content/behaviour - it seems much neater).
1) Add UITableView for your storyboard.
2) Set delegate and data source.
3) Create Outlets (properties in your class) for those tableView's
Then you can work with thouse table views. For example, place label on it, and change it text dynamically in your programm.
Cheers :)

Adding rows to UITableView built from Storyboard

I have a static UITableView built from a Storyboard that works well. I want to fill the first category programmatically, though, from a user-defined file
Simply put, I want to go through all the strings in an array and add them as cells for the rows of the first category. For the second category, I have a series of mildly complex cells (containing a number of labels, textfields, buttons and other controls), defined in the storyboard, that I don't feel like recreating in code.
As far as I understand, the default behaviour for a UITableView built from a storyboard is to use the nib file as an implicit datasource. If I use a custom class as datasource, my second section doesn't work. I have thought of two possible ways to fix this:
Fill my first category from the datasource and delegate the rest to the nib file. Is this possible? Is there some method to programmatically ask the nib to fill my UITableView?
Export my storyboard-built cells into code and paste this code into my datasource. This method has the disadvantage of making my second category harder to modify.
Is one of those two options feasible? Is there another option?
I would use dynamic prototype cells. Then, I would set up the ViewController as the delegate and the dataSource. I would then create a custom subclass of UITableViewCell and connect the elements of the second section to IBOutlets in the custom UITableViewCell.
If the first section wasn't something that could be done with one of the generic cell types, I would also create a custom subclass of UITableViewCell for that section as well.
I would then use the cellForRowAtIndexPath: method to set up the cells with the information that I want in them. So if my first section used FirstSectionCell and my second section used SecondSectionCell as custom subclasses of UITableViewCell my cellForRowAtIndexPath: would look like this:
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.section==0)
{
FirstSectionCell *firstCell = [tableView dequeueReusableCellWithIdentifier:#"First Cell Prototype"];
//Set up the first cell.
return firstCell;
}
else if(indexPath.section ==1)
{
SecondSectionCell *secondCell = [tableView dequeueReusableCellWithIdentifier:#"Second Cell Ptototype"];
//Set up second cell.
secondCell.someLabel.text = #"whatever";
//etc.
return secondCell;
}
else
{
//if you have another section handle it here.
}
}
There are two kinds of table views when you use Storyboards:
Static
Dynamic
You're currently using the former. You define everything in the Storyboard and have very little code.
But you need to change to the latter.
You can still keep your UITableViewCells in the Storyboard; there's no need to do that in code (though you can if it makes things easier). You can refer to the template cells using the "reuse identifer."
Otherwise you've pretty much got it. You'll need to write code to implement the data source and (possibly) more methods of the table view delegate.
It's kind of fiddly switching from static to dynamic. I keep meaning to raise a Radar because I'm sure Xcode could be making it easier to do...

Resources