I am trying to get the height and some other attributes from a prototype UICollectionViewCell in my storyboard. I need those values in the ViewDidLoad, so before any cell is rendered.
I found this answer for tableview:
https://stackoverflow.com/a/14127936/2637259
But it doesn't seem to work for a collectionview because I need a indexpath.
I also found this answer which seems to solve the problem: https://stackoverflow.com/a/22761931/2637259
But I don't know how to do that in xamarin.
Can anyone enlighten me please?
Edit: The reason I need to have it at ViewDidLoad (or at least anywhere before the cells are loaded) is a UIWebView I have in the cell. I have a html string to load into the cell. So I made a function to calculate the height of the webview. After all cells are calculated I call CollectionView.ReloadData() to load the cells. In the sizeForItemAtIndexPath I need to check if the height of the cell is less than the height of the cell without the webview plus the calculated height of the webview. If not, I need to return the standard cell size. Else I need to return the cell height without the webview plus the calculated height.
You could just create static properties for you custom cell classes like this:
public partial class CustomCell: UICollectionViewCell
{
public static new float CellHeight { get { return 44f; } }
... rest of your class
then you can get the height by just calling
CustomCell.CellHeight;
Hope that helps! Cheers!
Related
I am using a UICollectionView to show an array of images, horizontally and zoom in/out that image in the cell with the help of scroll view using constraints. But when I scroll horizontally it shows me previous cell content in the current cell. I am and not able to identify what is the problem.
CollectionView datasources
CollectionCell Class
try this when you reuse the Cell in cellForItem at
cell.imageView.Image = nil
Please share some code so that we can help you better. Meanwhile, make sure you have the following parts working correctly in your code:
Register correct UICollectionViewCell subclass with correct reuse identifier
Use the method [self.collectionView dequeueReusableCellWithReuseIdentifier:itemIdentifier forIndexPath:indexPath] and pass both the parameters correctly.
In UICollectionViewCell subclass, override prepareForReuse and reset the UI parameters there.
Override prepareForReuse in imageCell class, and set imageView.image to nil.
The contentSize of the scrollView should be as big as imageView.image.size, or just use MWPhotoBrowser
I'm relatively new to swift and iOS. How can I implement than app that looks like the updated instagram. : the first row has a collection of height x, but the rest of the cell have image views of height x+y. Checkout what I mean in this image, my rep is too low to upload an image. Lol :)
Implement the UITableView's delegate method func tableView(UITableView, heightForRowAt: IndexPath)
Then you can return any height for each indexPath. The easiest option would be to make that its own section, and then the rest of the same sized rows another section.
if your cells' height are different, you should in the methord func tableView(UITableView, heightForRowAt: IndexPath) make sure the height of cell according to the modal for the cell.
I have a TableView with 2 prototype cells in it. I want one of them to be static (call it header cell), and the other one to be dynamic (call it description cell).
So I set TableView cell to Dynamic Prototypes and 2 for prototypes cells). I have also set up the TableView and increased prototype cells; and put dummy data for seeing them working.
However, whatever I have done I couldn't resize the Header cell.
First, in Storyboard, I tried changing Row Height for the Header Cell (and I ticked the 'Custom'). It seems like changing the height of the row in the Storyboard; in fact it doesn't after the build.
Secondly, this width and height bit seems disabled.
It's also same for the Content View just under the Header Cell. Width & Height adjusting part is disabled.
By the way, everything is same for the Description Cell. I tried adding it a row height, and it seems like changing in the Storyboard, but after build, no luck. It is just behaving by itself.
I should also highlight that it's giving a bit more room for Description Cell which I return cell.textLabel?.text = "BlaBla" (so it has to write the text itself); but for the other cell where I am dragging/dropping on Storyboard, it just squeezes the height.
What may I be doing wrong? What do you think do I miss?
Edit: (I have a Navigation Bar at the top)
How it looks on Simulator:
How it looks on Storyboard:
As you can see it's narrowing the Header Cell (so messing the design), and increasing the height of Description Cell randomly.
If I can do it with heightForRowAtIndexPath, how should I use this approach for giving different height to 2 prototypes cells. (I want to have Header Cell to be static row height, but Description - Dynamic, or at least not identical height)
You're going to implement - tableView:heightForRowAtIndexPath: in your table view's delegate and return the height you'd like for each row.
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.row == 0 {
return 100
}
return 44
}
I'm using iOS 9.2 and XCode 7.2.
I have a basic UITableView object in which i add different kind of UITableViewCell subclasses.
With one of them, when i set manually the height overriding the method heightForRowAtIndexPath, i don't get any content on the cell.
If i return -1 as height(the default value for the UITable row height), i get my cell showing up correctly. The thing is that i do need a different height for this row because the content is quite big.
here is the code for heightForRowAtIndexPath:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
MenuItem *menuItem = [menuManager menuItemAtIndex:indexPath.row];
if ([menuItem type] == MenuItemTypeEditorHeader) {
return 100;
} else {
return [super tableView:tableView heightForRowAtIndexPath:indexPath];
}
}
MenuItem is a class containing the specific menu object' informations, such as the type. The result is that the cell is showed up at the correct height, but it's empty.
Its not advisable to use heightForRowAtIndexPath anymore - thats old-school. Instead, do this :
Set up autolayout constraints in your cell (if you dont know how to - you need to, its not something you can avoid anymore!)
Create an estimatedRowHeight for autolayout to use, on the tableView. You can set it in the nib/storyboard or programmatically, in viewDidLoad for eg, like this :
self.tableview.estimatedRowHeight = 68.0;
Set your tableview to use 'automatic dimension', like this :
self.tableview.rowHeight = UITableViewAutomaticDimension;
And thats it. If you do these things, then your cells will vary in height according to their constraints. So if one of your subclasses has a height of 150px due to its constraints, that will work perfectly next to another subclass that has a height of 50px. You can also vary the height of a cell dynamically depending on the contents of the cell, for eg when you have labels that expand using 'greater than or equal to' constraints. Also - simply omit the 'heightForRowAtIndexPath' method, you dont need to implement it at all.
Are you calling tableView.reloadData() ?
print the length of menu objects before you call tableView.reloadData().
HeightForRowAtIndexPath just returns height of a row. So may be problem in cellForRowAtIndexPath.
I've a tableView with custom cells.
Each cell has different interface, elements. All they have different sizes.
Each cell has different type of elements and their count.
All these elements are created dynamically, so I'm creating them, making their frames and adding as subviews.
So the problem that heightForRowAtIndexPath executes before cellForRowAtIndexPath where I'm creating the row and constructing its interface and I don't know how to pass calculated height to heightForRowAtIndexPath
How can I count row height before and pass its value to heightForRowIndexPath for correct drawing of my tableView?
If your cell view is really very complex and every component's height are depending on data source. You can try to create the view in heightForRowIndexPath method and then cache the created view to a dictionary in your view controller and use it directly in cellForRowAtIndexPath. In this way you only need to create the view once when user scrolling the table. If the datasource is not changing very frequently, you can reuse the cached view in heightForRowIndexPath as well.
And if the tableview has a lot of rows, you should return an approximate value for height in estimatedHeightForRowAtIndexPath to speed up the loading process of the view controller. Otherwise during loading tableview, it will try to calculate all row's height which may requires a lot of time.
But I really don't think your cell would be so complex. If only some UITextLabels that depends on datasource for the height, you can simply only calculate the height for the label, then add it to other components' height which is fixed.
You really should consider subclassing UITableViewCell and adding your custom logic inside your subclass.
Starting from there, you'll have such options:
Create static method in your cell that will receive all data necessary to draw your cell (e.g heithtForCellWithFirstString:secondString:accessoryImage etc) and calculate height using string size computation methods. Use this method inside heightForRowAtIndexPath.
Use autolayout for laying out subviews of your cell and then set table view's row height property to UITableViewAutomaticDimension. This way you won't need heightForRow delegate method at all. There are plenty of tutorials on this, for example: http://www.raywenderlich.com/87975/dynamic-table-view-cell-height-ios-8-swift
Try to create a subclass of UITableViewCell.
All elements created by you put inside UIView, which is put in a cell.
Create three NSLayoutConstraint, one from UIView.top to top of the cell, the second from UIView.bottom to the bottom of the cell and the third - the height of the UIView.
In the viewDidLoad method of tableViewController set row height as the UITableViewAutomaticDimension
:
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.rowHeight = UITableViewAutomaticDimension
}
In a subclass of UITableViewCell create a method that will calculate the height of the cell and change NSLayoutConstraint, which sets the height of the cell. For example:
:
func adjustHeightOfInnerView() {
let height = 100
self.myInnerCellViewHeightConstraint.constant = height
self.contentView.setNeedsUpdateConstraints()
}
In the method cellForRowAtIndexPath call the adjustHeightOfTableview:
:
func tableView (tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as! myTableViewCellSubclass
cell.adjustHeightOfTableview()
return cell
}