iOS dynamically load new cell to the bottom of a tableview - ios

I'm working on an iOS project where I want to dynamically add more cell to the bottom of the tableview once the user reaches the bottom. I currently have this working by calling a method that fetches more data that is added to the array that I'm using to hold my cells. When I'm done adding the objects to the array I call [tableView reloadData]. This works, but it doesn't visually load the cell until the tableview stops scrolling.
I also tried using [tableView insertRowsAtIndexPaths: inSection:withRowAnimation: UITableViewRowAnimationNone]. But I was getting very strange behavior where random cell would disappear and not show back up until I scrolled that section of the tableView off screen and then back to that area.
Is there a way to get the tableView to reload even if the tableView is still in mid scroll when the [tableView reloadDate] is called?
Thank you!

Check out: How to know when UITableView did scroll to bottom in iPhone
neoneye's answer about - (void)scrollViewDidScroll:(UIScrollView *)aScrollView { was exactly what I was looking for. I moved the method call to fetch more cell right after the NSLog(#"load more rows");
Thank you JiaYow for steering me in the right direction!

Related

Swift - Detect UICollectionView cell at specific CGPoint

I have implemented a UICollectionView cell which can be scrolled horizontally. While scrolling, is it possible to detect which cell(index) is passing the first cell(among all visible cells in UICollectionView)??
I don't know whether I cleared my question. You can see the following picture...

why tableview cells are starts flickering when they are going to be reused?

I'm building a chat interface. I'm using multiple types of tableView cells in UITableView. Table view cells are start flickering when new cell is added into tableView from reusablecell. I'm dequeuing tableview cells with following piece of code. How get scrolling smooth with out any flickering.
tableView.dequeueReusableCellWithIdentifier("MessageLessonTableViewCell") as! MessageLessonTableViewCell
The main reason for flickering is that, tableView is getting data to be attached late in time. So when you scroll, it takes time to switch from one type of tableViewCell to another. Plus you might be doing DB operations in cellForRowAtIndexPath which delays in returning tbaleViewCell. For implementing Smooth scrolling refer this

UICollectionView Horizontal Scroll after scrollToItemAtIndexPath:

I have an array of images that is displayed in a collectionView similar to Instagrams profile page. When an image is selected, the indexPath.row gets passed to the next collectionView in which I use the same array and the passed indexPath.row to know what image to display on the entire screen.
- (void)viewDidLayoutSubviews{
[self.view layoutIfNeeded];
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:self.selectedImage inSection:0];
[self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
}
The image comes up but I am unable to scroll horizontally between the rest of the images. Paging is enabled and each cell is large enough to take up the screen. If I comment out the above code, I am able to scroll through all the images. So pretty much how Twitter functions when you select a persons image and are able to scroll through the rest instead of x'ing out and selecting each one individually
I believe viewDidLayoutSubviews is called whenever a collection view cell is reused. So when it tries reusing the cell, it runs the code that forces it to scroll back to that item, meaning that the scroll view never moves :) I think you need to make sure that scroll to the item only gets called once.
To test this out... try putting in a breakpoint in the code you have above, and see if this is getting called again as you try scrolling horizontally.

Dequeued UICollectionViewCell not refreshed

I have an application with a UICollectionView with horizontal scroll of cells (Only one cell is displayed at a time). Each cell hosts a UITableView which displays content (URL links) in each of the UITableViewCell.
The problem is that the first two cells of UICollectionView display the proper content, but starting from the third cell, the contents are displayed from the dequeued cells. The correct content on the dequeued cell's UITableView is only displayed if user scrolls down and then scrolls up to the top.
For example, the contents of the third cell are the content from the first cell.
The cell is a custom UICollectionViewCell that hosts the UITableView. The custom cell implements the UITableViewDataSource, UITableViewDelegate.
I have searched through different forums and sites and found the solution of refresh.
I tried adding the [collectionView reload] to the -cellForItemAtIndexPath: method, but it goes into constant loop. I know that is not the right place, but i do not have any other methods that cause the refresh (only scrolling through the cells loads the next cell's content).
Can somebody please help me to resolve the issue?
You are missing some facts about dequeuing.
When you 3d custom UICollectionViewCell is dequeued, it isn't allocated like when it's displayed for the first time. Instead, your collection view "reuses" 1st cell for displaying 3d. The best moment to clear content in your custom cell class is -(void)prepareForReuse. If you override it and clear it's content, the -cellForItemAtIndexPath: will dequeue "clear" cell which you can further customize.
However, i don't exactly know how to "clear" the tableView, not sure whether it's possible. You have to reloadData on your tableView in -cellForItemAtIndexPath:. Example:
- (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath
{
MyCVCell *cell = [collectionView dequeueReusableCellWithIdentifier:#"MyCVCell" forIndexPath:indexPath];
cell.arrayOfURLsForDisplay = self.arrayOfArraysOfURLs[indexPath.row];
[cell.tableView reloadData];
return cell;
}
Here i mean you have some arrayOfArraysOfURLs in your viewController and you dislay URLs in your custom cell from some arrayOfURLsForDisplay
I had similar problem, instead of using [cell.tableView reloadData] inside the cellForItemAtIndexPath, I called collectionView.reloadData in the prepareForUse method that is overwritten in your custom UITableviewCell

Change cell height while scrolling a UITableView

Is there anyway to change the height of a cell dynamically whilst scrolling a UITableView?
I need to change the height of a number of cells when the scroll position reaches a certain point as the user drags it down past the top. I can do this successfully by issuing a reloadData, however it's very abrupt since the cells just vanish.
I've also tried reloading the cells and running the begin/end updates on the table view, however in both cases the animation sends the scroll view back to the top.
I'd like to animate the cell height change without messing up the current content offset of the drag in progress.
Tim
Use the UITableViewDelegate and implement the method
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
This solve your issue?
If it is not solving your issue, try to explain better your goal, with examples/images.

Resources