Reloading subview (UICollectionview) - objective-c

In my ViewController I have four subviews, i.e UITableView, UICollectionView, View with UILabels, and View that displays preview image for that item.
By selecting a row in tableview, I am able to change preview image and all labels. However I can not refresh the UICollectionView data. I tried this solution but it just removes and adds views and that changes the layout and preview image disappears.
All I want to do is refresh UICollectionView contents. Is there any simpler way to do this?

Have you tried [self.collectionView reloadData]; ?

To refresh only a portion of the UICollectionView you could also call this:
[self.collectionView reloadItemsAtIndexPaths:#[indexPath]]
where indexPath is the cell you want to reload. You could include multiple index paths in the array as well. To reload the entire first section you could call this:
NSIndexSet *sections = [NSIndexSet indexSetWithIndex:0];
[self.collectionView reloadSections:sections];

Assuming you have correctly hooked up your UICollectionView to a dataSource (UICollectionViewDataSource), you can call [myCollectionView reloadData] to make your collection view call your dataSource's methods (as below) to refresh itself:
– collectionView:numberOfItemsInSection:
– numberOfSectionsInCollectionView:
– collectionView:cellForItemAtIndexPath:
– collectionView:viewForSupplementaryElementOfKind:atIndexPath:

Related

UITableView not drawing Cells correctly "sometimes"

When my app displays the table, sometimes, it displays ALL the content of the database. but when i try to segue to another VC an return from that VC (Lets say, I added a new entry to the Database), it displays this. if you touch below the "Missing" cells, it segue to the correct entry though...
I've already tried using:
[self.cTableView setNeedsDisplay];
[self.cTableView setNeedsLayout];
or
[self.cTableView beginUpdates];
[self.cTableView endUpdates];
or
[self.cTableView reloadData];
but still to no avail. Is there any way of refreshing the tableview graphically?
I can't post images due to reputations but the image that I'm going to post looks like the TableView gets CUT going down.
Image:
https://dl.dropboxusercontent.com/u/1336061/StackOverflow/Untitled.jpg
Are you re-using your cell correctly?
Check the cells reuse identifier. see that the cells are not being allocated every iteration (in cellForRow delegate).
If the objects that provide data to the "Data Source" of the table change, you might as well need to call:
[table reloadData];
OR
Load the new elements with their indexes (look at the UITableView headers to use the right delegate.

Table View Checkmark if Task Complete

I'm working in Objective C. I have a UITableViewController with about 25 cells that push to a UIViewController. When the user hits back, I want to see if the user entered the correct data for the given cell. (I have a working bool , we'll call it isCellComplete for now). If isCellComplete is true, I want to add a checkmark as the accessory to the cell. I've been trying to put the test in cellForRowAtIndexPath but that method does not seem to run and refresh the cells everytime the view appears. Anyone have suggestions?
You should look into the UITableView method reloadRowsAtIndexPaths:withRowAnimation:. This is much more elegant than reloading the whole table view. And if you don't want an animation, you can specify UITableViewRowAnimationNone and it will look just like reloadData but be much more efficient.
You should do the check in tableView:cellForRowAtIndexPath: and if the check passes, set the cell's accessory to UITableViewCellAccessoryCheckmark. Then when you tell the table view to reload the appropriate row(s), it'll automatically call tableView:cellForRowAtIndexPath: on the data source and update that cell.
You could just call
[self.tableView reloadData];
in
-(void) viewWillAppear:(BOOL)animated
And that will make cellForRowAtIndexPath be called again when the view appears

Combined UITableView with other elements - how to create and use the view?

Ive a project close to doing everything I need it to do. Its got a Main page which has four buttons that allow you to choose an option. Then a tableview page is launched from those options and displays a parsed XML feed from a website. You can then select one of the options in the table to see a detail view of the item, enquire about it, etc.
My problem is I need to add more elements to the TableViewController page, other than the tableview itself. I want a customized back button (not the navigation controller standard) plus some labels, images, etc.
In the TableViewController xib, the tableview itself fills the page. I cant resize it to add more elements above it. I can add a 'view' window seemingly above the tableview and put things in it. But it seems to add the view to the tableview. This means that when I scroll the table, the other elements like new back button, scroll away as part of the table.
So I'm led to wonder whether I need this page not to be a tableviewcontroller, but a viewcontroller, with a tableview inside it, as well as my other view with buttons, etc. Is that the right way to go? But if thats the case, then how do I instantiate the tableviewcontroller within code? Because my page will not be of that type anymore - it will be just a viewcontroller. But in code Im making a tableviewcontroller .. slightly scared by that route tbh.
Any illumination on this would be much appreciated, as various searches have left me none the wiser. Thanks.
To customize it, this is the way to go:
Change your class to be a view controller instead, which implements the TableViewDelegate and TableViewData Source protocols.
In the view didLoad of you controller, create the table view, set its delegate, data source, and any other properties you wish and add it as a subview to your view.
tableView = [[[UITableView alloc] init] autorelease];
tableView.delegate = self;
tableView.dataSource = self;
// .. Other customization
[self.view addSubview:tableView];
I suggest doing this programatically rather than IB.
Instead of a UITableViewController, you want a UIViewController that has an IBOutlet UITableView. Drag and drop a UITableView component from Storyboard and hook it up, and position it as needed on the screen. This UIViewController should implement the UITableViewDelegate and UITableViewDataSource methods.
Edit: Remember that a UITableViewController is just a subclass of UIViewController so you don't really have to get rid of all your code. :) You only need to make minor tweaks to your header and main file, and change the Storyboard to be a UIViewController instead of UITableViewController as I mentioned above.

Animate section footer changes in grouped UITableView

I know that you can animate insertions of table view cells and section but sometimes I need to change the footer text to one with a different text or to one that is 'nil'. I need to animate it when a UISwitch is toggled.
Right now I am using [tableView reloadData] but this is ugly and changes the section footer too sudden and without any animations. Apple somehow animates this for example when you turn on Personal Hotspot the section footer is slightly changed and this change is animated.
How can I achieve the same effect?
Assuming you've already written the code in -tableView:titleForFooterInSection: to adjust in response to the UISwitch, you could just send an empty update block to the table view to get it reload with animation.
[self.tableView beginUpdates];
[self.tableView endUpdates];

Iphone sdk : Where to put code for a grouped table

I have a root view controller with no nib file,
I tried adding this at cellForRowAtIndexPath as that passes in a UITableView as tableView.
So I put :
tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
It ran with no error but it didnt seem to change anything.
Your code snippit won't change anything, because the -tableView:cellForRowAtIndexPath: method is only called when an existing table view needs to know how to draw its content. No existing table view, no getting called.
Remove that line from your -tableView:cellForRowAtIndexPath: method, and define a UITableView object either:
In the XIB file (this is the easiest approach)
In the initialisation code for your view controller
As #FenchKiss Dev wrote, if you set up the table in code you need to add it as a subview to an existing view, for it to be displayed.
Did you add the tableView to your view ?
[self.view addSubview:tableView];