ProgressView wont update in tableview - objective-c

I am trying to update a custom tableview cell's progressview but the progress view wont update.
I can set the labels on the tableview cell but accessing things like progress bars or activity indicators doesnt work.
I do the following on the custom cell
cell.progressView.hidden = NO;
cell.progressView.progress = 0.5;
[cell.progressView setNeedsDisplay];
This doesnt work even though in the debugger I see the object is of the correct cell type and the progressView is allocated.
I have tried setNeedsDisplay on the cell itself as well, but no luck. What am I missing?

Calling [self tableView:tableView cellForRowAtIndexPath:indexPath]; is most likely the problem because this likely results in another cell being dequeued from the table. So you end up setting up a new cell, not the one currently being display.
Instead, do this:
CustomTableViewCell *cell = (CustomTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
This directly asks the table for the cell. This should return the currently displayed cell if the row is visible.

Related

UITableViewCell change Image without calling Reload (LazyLoading)

I have looked at some sample LazyLoading to load the image whenever the cells are visible and the loading is complete. I noticed they do not call ReloadData How do I change the images without having to call ReloadData or reloading a specific row?
I figured that changing the image data at the address would do it, but it doesn't work.
I tried
for (NSIndexPath *indexPath in visiblePaths)
{
UIBubbleTableViewCell *aCell = (UIBubbleTableViewCell *)[self.bubbleTableView cellForRowAtIndexPath:indexPath];
NSBubbleData *cellData = [aCell data];
cellData.avatar = [self.profileImages objectForKey:[NSString stringWithFormat:#"%#", [cellData posterID]]];
}
However, this does not update the image. I have to call ReloadData after the for loop for it to load the image. If I do not call ReloadData it will not update until the cell goes off the screen again and comes back into view.
Looking at the source code for UIBubbleTableViewCell, it does not appear to handle updates to the data. However, it looks like the the data is re-applied anytime you set the cell's frame. So doing something like this might be a workaround (I don't use this library so I can't test it):
aCell.frame = aCell.frame;

Object/Item out of UICollectionView cells instead of index?

I've been trying to save the state or order of cells in a UICollectionView after they have changed locations in the UICollectionView. When I use indexPathsForVisibleItems I get indexes that are always in the same order. They do not reflect order change. If I could get the object at each indexPath I could at least make an attempt to iterate through them to get the resulting order.
I used this code. And found that it returned a refreshed state of indexPaths in [0,0]. The ordering is always the same because the it tells me what cells have something in them not what is in each cell. I just don't know enough or where to start to get the object/item in each cell.
NSArray *visible = [self.collectionView indexPathsForVisibleItems];
NSMutableArray *rowsArray = [NSMutableArray arrayWithCapacity:[visible count]];
[visible enumerateObjectsUsingBlock:^(NSIndexPath *indexPath, NSUInteger idx, BOOL *stop) {
[rowsArray addObject:#(indexPath.item)];
I need the items or objects at each location. How do I iterate through all the cell locations to retrieve the item there. I plan on using the count of an array used to populate the UICollectionView to loop through all the locations and get the object stored there. There is only one section and cells seem to be storing objects like this <Slide: 0x7585200>
The function above returns NSLogs in this format "<NSIndexPath 0x8490390> 2 indexes [0, 0]"
I need to get what is in that location.
I'm not familiar with NSIndexPath or it's formatting. Even just the command/function would help for me to get an object out. Thanks.
(Addendum) I'm think of using something like [self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]]; Not sure what I can store it in.
CollectionView is a collection of UIViews (call UICollectionViewCell). Each cell has an indexPath (section,row) mean the layout position in collectionView content. In the case you has 1 section, section always = 0.
visibleCells mean all cells visible on screen at that time (visible in scroll frame or collectionView frame).
indexPathsForVisibleItems = indexPath array of visibleCells
cellForItemAtIndexPath is a method to get a collectionViewCell at any indexPath you want, often use to get cell when select:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
NSLog(#"%#",cell);
}
Investigate more from Apple UICollectionView video and example:
https://developer.apple.com/library/ios/#samplecode/CollectionView-Simple/Introduction/Intro.html
https://developer.apple.com/videos/wwdc/2012/

UITableView: load all cells

Is it possible to load all cells of an UITableView when the view is loaded so that they are not loaded when I'm scrolling?
(I would show a loading screen while doing this)
Please, it's the only way at my project (sorry too complicate to explain why ^^)
EDIT:
Okay let me explain you, what I'm definite doing:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = [NSString stringWithFormat:#"Identifier %i/%i", indexPath.row, indexPath.section];
CustomTableCell *cell = (CustomTableCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
NSDictionary *currentReading;
if (cell == nil)
{
cell = [[[CustomTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
UILabel *label;
UIView *separator;
if(indexPath.row == 0)
{
// Here I'm creating the title bar of my "table" for each section
}
else
{
int iPr = 1;
do
{
currentReading = [listData objectAtIndex:iPr-1];
iPr++;
} while (![[currentReading valueForKey:#"DeviceNo"] isEqualToString:[devicesArr objectAtIndex:indexPath.section]] ||
[readingresultsArr containsObject:[currentReading valueForKey:#"ReadingResultId"]]);
[readingresultsArr addObject:[currentReading valueForKey:#"ReadingResultId"]];
//
// ...
//
}
}
return cell;
}
My error happens in the do-while-loop:
"listData" is an array with multiple dictionaries in it.
My problem ist that when I’m scrolling my table slowly down, all is fine, but when I’m scrolling quickly to the end of the view and then I’m scrolling to the middle, I get the error that iPr is out of the array’s range. So the problem is, that the last row of the first section has already been added to the "readingresultsArr", but has not been loaded or wants to be loaded again.
That’s the reason why I want to load all cells at once.
You can cause all of the cells to be pre-allocated simply by calling:
[self tableView: self.tableView cellForRowAtIndexPath: indexPath];
for every row in your table. Put the above line in an appropriate for-loop and execute this code in viewDidAppear.
The problem however is that the tableView will not retain all of these cells. It will discard them when they are not needed.
You can get around that problem by adding an NSMutableArray to your UIViewController and then cache all the cells as they are created in cellForRowAtIndexPath. If there are dynamic updates (insertions/deletions) to your table over its lifetime, you will have to update the cache array as well.
put a uitableview on a uiscrollview
for example , you expect the height of the full list uitableview is 1000
then set the uiscrollview contentsize is 320X1000
and set the uitableview height is 1000
then all cell load their content even not visible in screen
In my case it was that I used automaticDimension for cells height and put estimatedRowHeight to small that is why tableview loaded all cells.
Some of the answers here and here suggest using automaticDimension for cells height and put mytable.estimatedRowHeight to a very low value (such as 1).
Starting with iOS 15 this approach seems not to work anymore. Hence, another way to achieve the table to "load" all cells could be by automatically scrolling to the last cell. Depending on the tables height and how many rows it can show some cells are discarded but each cell would be loaded and shown at least once.
mytable.scrollEnabled = YES;
NSIndexPath* indexPath = [NSIndexPath indexPathForRow:cellCount - 1 inSection:0];
[mytable scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
mytable.scrollEnabled = NO;
If you want to scroll up again just scroll to the top as outlined here.
Following the comment that was made by juancazalla, I found that if you have a tableView that is using automaticDimension, loading all the cells at once can be best achieved by setting estimatedRowHeight to a low value (such as 1).

UITableView reloadRowsAtIndexPaths and cellForRowAtIndexPath

I have a timer which calls the below code to reload a cell in the second section of my grouped UITableView. It works perfectly fine unless I scroll the screen up so that the first section/cell cannot be seen. Once it "springs back" to its correct position, I see that the text which should appear in section 0 has appeared in section 1 and vice-versa.
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:1]] withRowAnimation:UITableViewRowAnimationNone];
Does reloadRowsAtIndexPaths or cellForRowAtIndex path work from the top of the full (visible or invisible) cells, or does it only work starting on the cells that are visible on the screen? In other words, does section 0 actually change depending whether it is visible on-screen or not?
What else would cause cellForRowAtIndexPath to load the wrong data into the cells?
Ok, I played a bit with your project, here are my suggestions:
As you are adding the button to the cell, remember to remove it when you don't want use it (when you refresh the cell, basically you create a new cell, and store the second one to reuse, so if you scroll down and then up, you'll reuse that cell as the cell for the name).
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
[[cell viewWithTag:2] removeFromSuperview];
for this to work, you must set the corresponding tag to the button:
myButton1.tag = 2;
[cell addSubview:myButton1];
And also, add the button directly to the cell, not to the contentView (this just worked for me, I'm not sure why).
EDIT: I uploaded a fixed version of your project, download it.

how to toggle the selection color of a UITableViewCell?

I want to modify a cell in a table view; making it highlight blue only when clicked. when released, the blue color should disappear.
the code:
cell.selectionStyle = UITableViewCellSelectionStyleNone;
in the configuration of the cell, will disable the selection completely.
in all, this is mostly needed when i return from the push view controller. i then dont want to see any selected cells in the table.
Any ideas on how to do this?
thanks
When you touch a row in a tableview (if selection is allowed) the row is rendered in the selection style. If you want the "blue" selection colour to disappear, just deselect the row using:
NSIndexPath* rowPath = [NSIndexPath indexPathForRow:5 inSection:2];
[self.tableView deselectRowAtIndexPath:rowPath animated:NO];
Sounds like you're detecting the selection and doing something with the object backing the row and then returning to the same table, so just do the deselection in the same method as you do the action probably in:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
You can set a cell background color when the user clicks and set it back to white when the user releases.
That code should be useful to solve the 2nd problem
self.clearsSelectionOnViewWillAppear = NO;