collectionview inside tableview on expand collapse with dynamic cell height. objective c - objective-c

i need when i expand-collpase tableview indide tableviewcell i opened collectionview, also i need dynamic height of collectionview according to its item.also tableviewcell height dynamic. currently i am statically chaing height based on expand collpase.I need similar to this image
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (self.selectedIndex == indexPath.row) {
return 150;
}else{
return 40;
}
}
tableview method for adjusting row height, i want it to be dynamically set height. also i have collectionview inside it. for reference i have 2 images it working fine but with static heigt i want it dynamically.[following is the image for reference][2]
[1]: https://i.stack.imgur.com/iJ8Df.png
image when collectionview item is more set, i want it to set dynamically

First,get the cell's model,then calculate with image count,for example:
// cell height is 40
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return model.images.count / 6 * 40
}

Related

AutoLayout for UICollectionView Cells

I have an uicollectionview in my app which shows the photos from the camera roll (using AssetsLibrary) in small square cells by using an uiimageview. My problem is that it all looks fine on an iPhone5/5S but there is huge spacing between the cells when running it on an iPhone 6. The question is: How could I make the cells resize themselves so that the spacing stays the same on all devices? (And the cells grow instead).
After set auto layout for UIImageView to make sure that UIImageView will fit to cell and make the adjustments to the cell size from there
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
int cellWidth = (SCREEN_WIDTH - (3 * PADDING)) / 4;
return CGSizeMake(cellWidth, cellWidth);
}
Make your collection view controller follow the protocol of UICollectionViewDelegateFlowLayout and override the following method. Then you can size your cells based on the size of your view's bounds.
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
// Calculate size based on your view's bounds here
}
Note: If you're also dealing with rotation you'll have to invalidate your collection view layout when the device rotates in order for this method to be called again and recalculate the sizes.
[self.collectionViewLayout invalidateLayout];

UITableView cells doesn't fit UITableView height

I download Json data and then I populate my custom UITableView, the problem is that even if I calculate the height of the tableview based on the amount of elements I download the cells doesn't fit the whole height of the tableview, So my final situation is a tableview with n elements that has height of n*100 but the cells just fits a part of it.
This is the code I used to calculate the height:
//callback that contains the items I download
-(void)productsDownloaded:(NSArray *)items
{
_productsDetails = items;
CGFloat height = 100;
height *= items.count;
CGRect tableFrame = _customTableView.frame;
tableFrame.size.height = height;
_customTableView.frame = tableFrame;
[_customTableView reloadData];
}
I also tried:
_customTableView.alwaysBounceVertical = NO;
_customTableView.scrollEnabled = NO;
But with no results.. How can I tell the cells to fit the height of the cell?
To achieve a scrollable table view, you have to use the tableView:heightForRowAtIndexPath: delegate method to specify the size (height) or your cell, not just change the frame of your table view.
Example:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//TODO: Calculate cell height
return 60.0f;
}
To have all the cells visible on one screen, the total height of the table view cells has to be equal to the height of the table view, thus the height of one table view cell should be:
CGFloat cellHeight = CGRectGetHeight(tableView.frame) / numberOfCells;
However, I really don't understand why you would want to do this. The whole reason of a table view is to have a fixed height for cells and scroll (Like the Settings app does it).

Have access to UITableViewCell from heightForRow

I have the following code:
- (CGFloat)tableView:(UITableView *)_tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
PostTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
return cell.imageView.frame.size.height + 40;
}
What I want to do is be able to access the cell's properties somehow. I want the cell's height to be equal to the height of the image in the cell's image view. I don't think this is working.
Don't try to access the cell. Your table's data source or some other object besides your cell should be managing that image. Ask that object for the image and take its size.
Treating your cell as a data container is tangling up your model, view, and controller layers and is likely to make changes more difficult down the line.

How does manually assigning tableView.rowheight affect subviews of the given Table View?

I have a custom view for displaying application status messages that I slide over my table view as needed. This worked fine until I started customizing my table view cells.
When I manually assign a row height to my table view cells in initWithSTyle...
self.TableView.rowHeight = 64.0;
...the custom view doesn't show up anymore.
I replaced the code above with a delegate method...
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 64.0;
}
...and now the custom view works as before.
Set rowHeight in the view controller.
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.rowHeight = 64.0f;
}
I think initWithStyle is too early to set the height. Eitherway, since your height is constant, it's still more efficient to set the height once (in viewDidLoad) than set it every time for each cell created (in initWithStyle or heightForRowAtIndexPath).
Did you create a custom cell class inheriting UITableVIewCell , if yes , then the height of customcell is different than the height of default cell.So you need to change the height of row to match your customcell;s in order for it to desplay correctly,Else cellswill overlap.
To Dynamically set height accoring to your customcell:-
YourCustomCell *cell = (YourCustomCell *)[tableView cellForRowAtIndexPath:indexPath];
return [cell.frame.size.height];
maintableView = [[UITableView alloc] init];
maintableView.frame = CGRectMake(0, 0, 320, 480);
maintableView.rowHeight = 100.0;
maintableView.delegate=self;
maintableView.dataSource=self;
It is working in my Xcode. Try like it. I think it will work.

UITableView don't float section headers

Is it possible to not float the section headers for a UITableView with style UITableViewStylePlain?
I'm building AcaniChat, an open-source version of iPhone's native Messages app, and I want to make the timestamps section headers, but they shouldn't float.
I know that section headers don't float for table views of style UITableViewStyleGrouped, but that style looks less like what I'm going for. Should I just use that style and restyle the table view to make it look how I want?
I might do that if I can figure out how to make https://stackoverflow.com/questions/6564712/nsfetchedresultscontroller-nsdate-section-headers.
The interesting thing about UITableViewStyleGrouped is that the tableView adds the style to the cells and not to the TableView.
The style is added as backgroundView to the cells as a class called UIGroupTableViewCellBackground which handles drawing different background according to the position of the cell in the section.
So a very simple solution will be to use UITableViewStyleGrouped, set the backgroundColor of the table to clearColor, and simply replace the backgroundView of the cell in cellForRow:
cell.backgroundView = [[[UIView alloc] initWithFrame:cell.bounds] autorelease];
I guess either you will have to use two kinds of custom tableCells or skip the tableview entirely and work on a plain scrollview to achieve this kind of style.
This can now be done in two quick and easy steps (iOS 6 only):
Change your UITableView style to UITableViewStyleGrouped. (You can do this from Storyboard/NIB, or via code.)
Next, set your tableview's background view to a empty view like so [in either a method such as viewDidAppear or even in the cellForRow method (though I would prefer the former)].
yourTableView.backgroundView = [[UIView alloc] initWithFrame:listTableView.bounds];
Voila, now you have your table view - but without the floating section headers. Your section headers now scroll along with the cells and your messy UI problems are solved!
This works because UITableViewStyleGrouped seems to now work by adding a background view to a plain UITableView, but without the floating section headers.
[N.B. Earlier to iOS 6, individual background images were added to UITableViewCell's.]
Do try this out and let me know how it goes.
Happy coding :)
EDIT: for iOS 7, simply change the table view style to 'UITableViewStyleGrouped' and change the view's tint color to 'clear color'.
You can achieve this by putting the headers into their own sections. First double your number of sections. Then for the even-numbered sections, return your header as the header and zero as the number of rows. For the odd-numbered sections, return nil for the header.
Assuming you're using an NSFetchedResultsController, it would look something like this:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.fetchedResultsController.sections.count * 2;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if ((section % 2) == 0)
{
section /= 2;
id<NSFetchedResultsSectionInfo> sectionInfo = self.fetchedResults.sections[section];
return sectionInfo.name;
}
else
{
return nil;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if ((section % 2) == 0)
{
return 0;
}
else
{
section /= 2;
id<NSFetchedResultsSectionInfo> sectionInfo = self.fetchedResults.sections[section];
return sectionInfo.numberOfObjects;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ((indexPath.section % 2) == 0)
{
return nil;
}
else
{
indexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section/2];
id object = [self.fetchedResultsController objectAtIndexPath:indexPath];
// configure your cell here.
}
}