Infinite Scroll on iOS with Swift - ios

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.

Related

No data at all is showing in UITableView

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.

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.

Refreshing the view of UITableView

I understand the concepts of cell re-usability for Xcode 5.0 table views. However, I have one very weird observation which I don't understand and wish anyone of you could enlighten me here. Thanks.
I have implemented a table view with a search bar utility (just on top of the table view). Under each custom cell (prototype cell), whenever a user clicks on it, it will be marked with a checkmark (UITableViewCellAccessoryCheckmark). The number of cells are more than 10.
Observation:
- Without using any search, marking and unmarking a cell is working as intended. Cells are updated instantly along with their checkmarks.
- When doing a search, from the results given, marking and unmarking a cell is also working as intended.
[Problem] Here comes the weird issue: when cancelling a search, an already marked cell (marked during search) does not refresh itself in the tableview unless scrolling up or down is performed!
And hence, I wrote [tableview reload] at the end of tableview:didSelectRowAtIndexPath: method. Obviously, it doesn't refresh the tableview for me. Without further changing any other code, merely modifying [tableview reload] to [self.tableview reload] under the same method works!
Why is the only addition of "self." able to make the table cells refreshed instantly? I have always thought the first argument, tableView, from the method (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath is as equal to self.tableview. Obviously, my interpretation in this case is wrong.
Thank you. I'm sorry for my lengthy post.
My guess is that this UISearchBar comes from a UISearchDisplayController. Is that correct?
If true, that is a common misconception, but an easy one to understand.
When filtering your UITableView entries and showing results, UISearchDisplayController actually overlays the view with its own tableView, UISearchResultsTableView.
Thus, this overlaid tableView also gets to call data source and delegate methods on your implementation, and this is when the tableView argument from tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath stops being equivalent to self.tableView.
This means that calling [tableView reloadData] during filtering actually asks UISearchResultsTableView to reload its contents, not self.tableView, a property of your viewController.

UITableView accessory view

I have created a UITableView populated by data coming from mysql (using NSJSONSERIALIZATION). Now the problem is one thing. What I retrieved was the product name. I want to have an accessory view (arrow like on the right side of cell). once clicked, new view and load details of that product there.
I know it can be done, but don't know how or where to look for a good tutorial. Any recommendations?
am really new to iOS development.
thanks a lot
Use this tableview delegate.
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
...and in cellForRowAtIndexPath: add this.
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

iOS - How to display the same table view after selecting an item

My iOS app is current transferring control to a detail view when an item in a UITableView is selected. This ia a quiz program, and I'd like to change the app so that it just redisplays the same table view with the correct answer highlighted when a row is selected. What's a good approach for doing this?
Is not clear to me if you know why the detail view is appearing. So I'll explain just in case. If you are giving control to a detail view is because somewhere in your code you are pushing that detail view. It depends on what kind of UITableViewCell you are using. If you are using one of the defaults styles, your detail view is probably been pushed in either:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
or
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
If you are using a custom cell, then you need to look for the method in charge of pushing
that detail view.
I think a good approach would be to:
Remove that pushing wherever it is.
Not to use an `UITableViewCellAccessoryType, if you are using one.
Do something similar to the following on your tableView:didSelectRowAtIndexPath:
Find the row for the right answer in your model array, according to tapped cell.
Use that row number to generate an NSIndexPath.
Use that NSIndexPath to find the correct cell with cellForRowAtIndexPath:
Call setSelected:animated: on that cell to highlight it.
NOTE: If your quiz has more answers than the amount of UITableViewCells that fit in the screen you should scroll your UITableView to the right answer for better UX.

Resources