How to move certain cells down in UICollectionView - iphone

I would like to move certain UICollectionViewCells down by a fixed height. I tried overriding -layoutAttributesForElementsInRect: and updating the center of some UICollectionViewLayoutAttributes in the array returned by [super layoutAttributesForElementsInRect:rect], but somehow some cells went missing that way. Do I need to override something else?

Implementing this delegate method of UICollectionView you can change the frame for a specific cell :
-(void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath

Try implementing this delegate. It works for me:
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.item == ....)
{
return newSize;
}
else
return oldSize;
}
if you want this move to increase the height of cell be animated, you can call this delegate by
[_yourCollectionView performBatchUpdates:nil completion:nil];
So you can refresh your cell when some action is triggered.

Related

How dynamically change UITableViewCell size for the last cell

I have a UITableView with 16-20 cells inside with dynamic cell size. When a cell expands it self it should also move itself to the top of the screen. I did that using "UITableView setContentOffset" method. It works well except for the last cell in table, it's not able to move itself to the top.
I tried altering the frame & content size of UITableView but none of those were working for me!
Any Idea?
[UPDATE]
Here is a part of code: (it's inside the UITableViewCell, so self is pointing to current cell)
HomeViewController *tempViewController = (HomeViewController *) delegate;
UIView *commentField;
/*Skipping lines of codes manipulating commentField */
//Adding a subview to current cell which needs more space
[self addSubview:commentField];
//Expanding cellSize to EXPANDED_CELL_HEIGHT
//ViewController has access to cell size property and using that to determine each cell size.
[self setCellSize:(EXPANDED_CELL_HEIGHT)];
//Reloading UITableView to reflect the cell size change with animation
[[tempViewController tableView] beginUpdates];
[[tempViewController tableView] endUpdates];
[[tempViewController tableView] setContentOffset:CGPointMake(0, self.frame.origin.y) animated:YES];
and in my view controller (as I said earlier) I'm getting cellSize form cell itself
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [(BaseTableViewCell *)[cellContainer objectAtIndex:indexPath.row] cellSize];
}
You can change the height of the cell using the tableview delegate method heightForRowAtIndexPath: When indexPath.row equals your last row, return the height you would like.
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == i)//i being whatever row index you want to change
{
return 60.0;//or float size you want
}
else
{
return 30.0;
}
}
Or you can just modify the bounds of the cell inside willDisplayCell:. If your using a custom tableview cell, just shrink the subviews to whatever frame you'd like and make the cell background clear etc.

Grouped Style UITableView doesn't clip subviews

I have a design issue with grouped uitableview, I add uiviews to the leftmost side of each cell extending little over the cell bounds and I want them to be clipped. But neither setting clip subviews in IB nor setting clipsToBounds property in code didn't help.
So far I have set clipsToBounds (for both uitableview itself and individual uitabviewcells) in a number of places but none helped:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
...
[cell setClipsToBounds:TRUE];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:defaultIdentifier];
if (cell == nil)
{
cell = [self reuseTableViewCellWithIdentifier:defaultIdentifier withIndexPath:indexPath];
}
...
[cell setClipsToBounds:TRUE];
...
return cell;
}
- (void)viewDidLoad
{
...
[mainTableView setClipsToBounds:TRUE];
}
Is it possible without changing tableview style to plain?
Update: My previous answer was wrong. Here is an answer that actually shows how to do what you're asking.
https://stackoverflow.com/a/401271/350467
.......
Previous Answer
If you're adding views to the cell's contentView (which you should be, unless you have a good reason), you'll want to set cell.contentView.clipsToBounds = YES;

UITableview Edit/delate- cell's content goes out of screen

All,
I am using UITableview and trying to implement an edit/delete function.
When I call this line .
[tblStoreAvail setEditing:TRUE animated:TRUE];
The cell's content goes out of screen.
These functions are never called.
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
If you are using custuomTableViewCell implement the following method with your required frame for the label:
- (void)willTransitionToState:(UITableViewCellStateMask)state
{
[super willTransitionToState:state];
if(state == UITableViewCellStateDefaultMask)
{
[self.lblLocation setFrame:CGRectMake(13,22,302,21)];
}
else if(state == UITableViewCellStateShowingDeleteConfirmationMask)
{
[self.lblLocation setFrame:CGRectMake(13,22,245,21)];
}
else if(state == UITableViewCellStateShowingEditControlMask)
{
[self.lblLocation setFrame:CGRectMake(13,22,245,21)];
}
else
{
[self.lblLocation setFrame:CGRectMake(13,22,210,21)];
}
}
if you are using default TableViewCell then make sure to set delegate for tableView and do like below :
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
yourCustomCell *cell = (yourCustomCell *)[tableView cellForRowAtIndexPath:indexPath];
[cell.lblLocation setFrame:CGRectMake(13,22,302,21)];
return YES;
}
You've got two problems going on, Taimur.
1) you need to set the "delegate" of your table to whatever object (presumably your view controller) that holds those delegate methods. You can do this programmatically or via a connection in the XIB / storyboard.
2) if you're using a custom cell, you need to adjust the font sizes down to hold all the data you want to display.
If you're not using custom UITableViewCell objects to display the data in your table, then start to use them so you can get the formatting you really want to see.

When click on button (button in cell) that particular cell should be changed?

There are 1 table on uiview and I want to change the cell height when button pressed other cell's height remain same
Pass the button press event to the view controller through delegate methods and reload the table view as follows.
[self.tableView reloadData];
In view controller (ie., datasource for the table view), implement heightForRowAtIndexPath method and return the height as required.
You can change the cell height in the delegate method
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if(indexPath.row == clickedRow)
return newHeitht;
return cellHeight;
}
you can set some condition in button click and reload the tableview using [tableView reloadData]. this function will be called. return a new height for the particular cell.
-(void)buttonClick {
[self.tableview reloadData];
selectedRow = //do something here
}
and in your UITableview Datasource
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath {
if(indexPath.row == selectedRow)
return selectedRowHeight;
else
return defaultHeight;
}
1) Create a custom UITableViewCell class.
2) Populate you cell however you see fit. I use a 'hydrateWithArray' type function myself.
3) Once the data has been populated, resize and reposition elements using 'sizeToFit' functions to force labels to conform to the size of whatever you put into them. protip: by setting the frame of the label first, and setting the numer of lines to 0... when you fill the text and sizetofit, it will stretch the label vertically only and force the width to stay the same.
4) Create a seperate function (mine is called calculatedHeight) that returns a float and returns the height that you would like the cell to be in the table (based on the repositioned objects from step 3).
- (float)calculatedHeight {
return textLabel.frame.origin.ytextLabel.frame.size.height5;
}
5) In your UITableView class, you'll need to import your tableViewCell class and create a dummy cell object. You're going to use this class to calculate how tall each cell needs to be. Then in the heightOfRowAtIndex method....
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
float height;
if ( !dummyCell ) dummyCell = [[CustomCell alloc] initWithFrame:CGRectMake(0,0,0,0) reuseIdentifier:#"myCell"];
[dummyCell hydrateWithTweet:[tableArray objectAtIndex:indexPath.row]];
height = [dummyCell calculatedHeight];
if ( height == 0 ) height = 50;
return height;
}
This is a pretty simple example so you may need to go crazy with the error checking in your particular use, but this should at least point you in the right direction. Enjoy!
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (isSearching && indexPath.row == selectedIndex) {
return 110;
}
else {
return rowHeight;
}
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:toReloadRows withRowAnimation: UITableViewRowAnimationNone];
[self.tableView endUpdates];
then
[self.tableView beginUpdates];
[self.tableView endUpdates];

UISearchDisplayController changing row height

I've set my UITableView row height to in Interface Builder to 54.0. I have a UISearchDisplayController on that view. When the user taps the search bar in it, the table resizes properly. However, when they start typing (and actually doing the search) the row height decreases. It stays wrong until the search taps Cancel.
I could find no documentation on this behavior on Apple's site.
I've tried setting the row height in UISearchDisplayDelegate delegate calls. This might be the right approach, but I don't know the details and couldn't get it to work.
I've also tried implementing - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;. This worked, but I have thousands of entries in this list and can't take the performance hit.
What's the right way to fix this?
The correct way to do this is to use the following delegate.
- (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView
{
tableView.rowHeight = 54.0f; // or some other height
}
It's called when the UITableView is created or shown.
If you happen to call searchDisplayController.searchResultsTableView in other code when a search is not being performed, it will create the UITableView in advance. If you use the willShow delegate method to set the rowHeight, it will miss the timing (for some reason) to change the rowHeight if the tableView has been created beforehand.
I found it!
Assuming the table is stored in tableView:
- (void)viewDidLoad;
{
[super viewDidLoad];
self.searchDisplayController.searchResultsTableView.rowHeight = tableView.rowHeight;
}
Nothing else is necessary.
Edit: See netmikey's answer for a better solution.
Steve, your solution didn't work for me: The first time the search result would display fine, but when a user was hitting "Cancel" (closing away the search bar) and then reopening it and entering a search term into it, the height would be wrong again.
DigDog's solution of using searchDisplayController:didShowSearchResultsTableView was kinda working, but users saw the cell height "jumping" when the search started.
The solution I found, that fixed both these issues is using:
- (void)searchDisplayController: (UISearchDisplayController *)controller
willShowSearchResultsTableView: (UITableView *)searchTableView {
searchTableView.rowHeight = myTableView.rowHeight;
}
... in the UISearchDisplayDelegate.
Regards,
netmikey
In viewDidLoad:
you can set the search display controller's delegate to self:
- (void)viewDidLoad
{
self.searchDisplayController.delegate = self;
}
Then, implement the table view delegate in your view controller:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableview == self.searchDisplayController.tableView) {
return 55;
} else if (tableView == self.tableView) {
return 44;
}
}
Overriding the method didLoadSearchResultsTableView should do the trick.
- (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView {
tableView.rowHeight = 82.0; // Change height of search result row
}
More explanation from this blog
You need to set both
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 54.;
}
and
self.tableView.rowHeight = 54.;
also in your UISearchDisplayDelegate
- (void)searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView {
tableView.rowHeight = 54.;
}
Otherwise, when "No Result" happened in searching, cell height will fall back to default.
I've been using ios 7.0 and Xcode 5.0. I think you should change you row height in this method:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if (tableView == self.searchDisplayController.searchResultsTableView)
{
tableView.rowHeight = ...;// change your row height here!
return [self.searchResults count];
}
else
{
...
return ...;
}
}
Because every time a tableview try to display its layout, this method is called, so everytime you type a letter into search bar, this method is called,and the row height won't have a chance to change.
None of the solutions worked for me...
Did my own hit and trial... and got it working...
If you want different row heights for normal & search table views...
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
// for normal results
if (tableView==self.tableView) return 54;
// for search results
else return 54; }
And if you want common height...
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 54; }
PS: Do keep in mind, if you have same row height for each row, this method is not very efficient resource usage wise, as the row height is re-calculated for each cell... Although, this is unnoticeable in most cases, I am still searching for a better solution!