how to reload supplementary view in collectionView - iphone

Is there an easy way to reload the suppplementary view only in a UICollectionViewFlowLayout? I don't want to reload the whole thing. Is it possible to do so?

After reviewing the documentation, I'm not sure about reloading an individual supplementary view, but you can use reloadSections: to update 1 or more sections in the UICollectionView. (documentation) So when a section header needs updating, you can call:
Objective-C:
[self.collectionView reloadSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 3)]];
Swift:
self.collectionView?.reloadSections(IndexSet(0 ..< 3))
Which happens to call:
- (UICollectionReusableView *)collectionView: (UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
//Place logic for handling update here...
}

If you want to change the layout or size of the supplementary view from the Controller, use[self.collectionView.collectionViewLayout invalidateLayout].If you want to refresh the supplementary view only,use [supplementaryView setNeedsDisplay].Use both if the supplementary view is complex and/or will change its dimensions.

Related

Having trouble with CollectionView in Xcode - display's black screen

Basically I've been writing iOS apps for about 3 days (after reading a book to get the basic knowledge of design patterns and frameworks etc) and 1 entire day has been spent on this. I cannot get my CollectionView to display. I've got a storyboard that starts with a TabBarView and the two tabs (one is a TableView and the other is just a UIView) work, but the third tab (UICollectionView) just displays a black screen even after setting up.
How I set it up:
1) dragged a ViewController to the storyboard
2) made a relationship segue between my UITabBarController to the new ViewController
3) dragged a UICollectionView to the new ViewController
4) dragged 4 UICollectionViewCell's to the CollectionView
5) dragged 4 UILabels into said CollectionViewCell's
6) made a connection between the new CollectionView's delegate & data source and the header file of my CollectionView class (this might be where I went wrong, I did not make the connection to my ViewController.h class as I used this one for my UITableView and thought a new class was necessary)
7) declared the following methods in my CollectionView.m file
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
//Populates each cell using the data given (no data in this case).
//Should return a CollectionViewCell.
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"cell" forIndexPath:indexPath];
return cell;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
//Gets the number of cells in each section.
//Should return an integer value.
return 2;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
//Tells the collection view the number of sections it should have.
//Should return an integer value.
return 4;
}
I get the feeling there is something I missed out, as this logic makes sense to me as a programmer but, having such little experience in Xcode, maybe I forgot to link up this class to my CollectionView or something. Does anybody know where I went wrong?
BTW in case you're wondering, I'm using this CollectionView more of a navigation technique than to display wonderful images or anything, I am going to populate them with generic images later when they actually show up in the emulator.
thanks
It is not displaying anything because default color of label is black, you'll have to change the color of text to any light color to see text over cell.
Now,
->Add a new file which should be subclass of UICollectionViewCell, Ctrl+drag from label to this class.(for eg I took mainViewCell)and label property name I took labelText
->Declare reuseIdentifier for cell from Attribute Inspector,(mainCell, I took)
->Import cellClass to your UIViewController Class,
->Using this method to display any text
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
mainViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"mainCell" forIndexPath:indexPath];
cell.labelText.text = #"What-ever-you-want-display";
return cell;
}
hope this would help...
Did you tell your view controller(the one that contains the collection view) to use UICollectionViewDelegateFlowLayout? For example,
#interface RootViewController () <UICollectionViewDelegateFlowLayout, UICollectionViewDataSource>
Also, have you set the reuse identifier for your cell? In Interface Builder set this in the attributes inspector. Then go to the cellForItemAtIndexPath: method and change this line to use your identifier.
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"MyCell" forIndexPath:indexPath];
Check out UICollectionViewDelegateFlowLayout in the documentation for methods used to size cells and set margins and spacing.
The step 6) might be inadequate. Have you ctrl-dragged the collectionView to the controller icon beneath the main view directly? You would have been prompted in a black dialog box for connecting dataSource and delegate. The connections are made in XCode this way.
Alternatively you could explicitly set the dataSource as self in the controller implementation.
Remove registerClass code from the default view controller code snippet
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];

UITableViewCell Animation

I am stuck with a tableview where cell are displayed in stack format.Any suggestion from expertise how to implement this kind of tableview.
(i.e i need to display the one cell behind another cell. On Tapping any cell, the cell should animate and shows complete contents of cell)
EDITED:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
int defaultCellHeight = 30;
if(isSelected)
defaultCellHeight = 60;
return defaultCellHeight;
}
You can create this type of TableView by giving shadow of UITableViewCell.
Here i give you to Some links that might be helpful in your case.
https://github.com/mystcolor/jtgesturebasedtableviewdemo
https://github.com/OliverLetterer/UIExpandableTableView
https://github.com/binho/TicketSaldo
Follow This is best the Site for all Controller Ever.
https://github.com/bobmccune/Core-Animation-Demos If you look for core animation,then hope this demo works for you.

Adjust UITableViewCell Height and Indentation?

Two UITableViewCell related questions:
In my custom UITableViewCell I loop through an array (of which I do not know how many objects it holds) and add a UILabel displaying some text for each object in that array.
This means I have to adjust the height of the cell so that these labels fit in. How can I do this?
When going into edit mode, I have the cells indent, however I do not want this. I have tried the following:
cell.shouldIndentWhileEditing = NO;
and
-(BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
return NO;
}
Both sadly failed, I have I no idea why. How could I possible remedy this?
Any help is much appreciated with either of these issues, thanks.
You can specify the height for every row with the delegate method [UITableViewDelegate tableView:heightForRowAtIndexPath:].
Just change what the method returns and the reload your table.
This method actually has nothing to do with the indendation of cell content:
Asks the delegate whether the background of the specified row should be indented while the table view is in editing mode.
You can try to set indentationWidth but I never managed to make it work.
Fortunately, it's easy to change everything you want in [UITableView layoutSubviews] method.
Example:
- (void)layoutSubviews {
[super layoutSubviews];
self.contentView.frame = self.bounds;
}
You may also need to set
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
{
return UITableViewCellEditingStyleNone;
}
For the dynamic height part there are plenty of other answers and a quick google found a tutorial here

Is there a way to force a refresh of just a single header in UITableView?

I have a UITableView where I want to force a refresh of just a specific section header row, not the data rows. For example, refresh the header for section 3. I know how to reload an entire section (header plus data), but can just the section header be refreshed?
EDIT: When data changes in one section of the table I want to update information in the header of a DIFFERENT section. Therefore, I need to get a reference to the section header and then refresh it.
If the header is a custom UIView, then you can just call setNeedsDisplay on the UIView rather than on the UITableView.
Well, in my case, after some asynchronous task i need to update my custom section header view.
and I get it to update it's height using :
This Swift 3+ Code
//to reload your cell data
self.tableView.reloadData()
DispatchQueue.main.async {
// this is needed to update a specific tableview's headerview layout on main queue otherwise it's won't update perfectly cause reloaddata() is called
self.tableView.beginUpdates()
self.tableView.endUpdates()
}
UITableView does *not have any such method to reload only the header view/title ...You have to reload the whole section or the table.
I beg to differ. I've run into this while deleting rows with custom section headers and (at least in iOS 6) you can simply call:
[self tableView:self.tableView viewForHeaderInSection:[indexPath section]];
And as long as you have proper code to return a view in that method, you can easily refresh the header for a specific section:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
if ([self tableView:tableView numberOfRowsInSection:section] == 0) {
return nil; //hide the header if there are no rows...
} else {
// configure or refresh the header...
}
}

UITableView fixed header

How to disable scrolling for UITableView header, to be always visible? (To have column titles.)
I am not sure if I am getting what you mean, are you using a UITableViewController?
If you want to have a header that is not scrollable maybe you can try to put a UIView at the top of your tableview and add sublayers to it as needed.
If a tableview with multiple fixed section headers is what you want, I would suggest using different tableviews and adding UIViews on top of each one for the header.
For handling delegate and datasource use NSObject's - (BOOL)isEqual:(id)anObject .
For example:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if ([tableView isEqual:markosFirstTableView])
{
return 1;
}
else
{
return 5;
}
}
Does this help?