I'd like to add a UIView above the table, as if it's the header.
I don't want to use UITableViewHeader because it doesn't allow auto-layout.
You can change the UITableView's header view to be a custom view, if you like.
Use it's tableHeaderView property to set it as a custom view. Like so:
tableView.tableHeaderView = YourCustomView()
Or if you don't want to use UITableViewHeader then you can always set tableHeaderView to nil and put a custom view above the table view.
You can create a UIView (your header) in your XIB file and set these constraints:
And set these constrains for your UITableView:
If you are creating the UITableView programmatically, you can create these constraints programatically by using the same idea above.
You can create UIView above tableView。as if the tableview's cell.then overwrite scrollViewDidScroll() method and change frame when tableview scrolling.
Related
I'm trying to implement a subclass of UICollectionViewCell.
I'm getting an array of custom views, let's call it views passed from one object to another, also with an array called views. They are of type CustomView.
The UICollectionViewCell subclass has one property called view of type CustomView.
In CellForItemAt..., I'm trying to set the cell's view property from an array of CustomViews like this:
let customView = array[indexPath.row]
cell.view = customView
However, when I do this, the cells don't show properly; they're empty, and just grey shapes on the screen.
But, when I explicitly set the properties of the cell's CustomView subview, it shows how I'd like it to.
Is there a way to do a layout pass on the cell? Or do you have to just explicitly set properties in UICollectionViewCells instead of just passing in a pre-configured view?
The code is proprietary, so I have to be cryptic, but please let me know if I can clarify
Edit: Please see the comments in the accepted answer for further explanation
You should be adding constraints, or else you are getting the default auto resizing mask constraints. Call cell.setNeedsLayout() to force auto layout to recalculate before the next draw after you assing the voew.
I have a UITableViewCell. How should I add a subview to it? I have seen many example to add subview to the contentView, but why not directly to the cell?
You can do it, but If you want to customize cells by adding custom view, you should add them to the contentView in order to get them animated along the table animations
When you add a custom view to contentView or assigning to contentView handles framing resizing part ,animation part and when editing mode is ON (tableView.editing = YES;) tableview tries to operate with views of itself meaning contentView, accessoryView. So when you want to add something to the table view and let control be given to tableView go for contentView
I have a UITableview subclass.
I need to use custom scrollview ie, 'TPKeyboardAvoidingScrollView' it is used for text fields up when edit the textField.
How can I apply custom scrollView to tableView programatically.
Why don't you make your table view a subclass of TPKeyboardAvoidingTableView instead of UITableView?
I have a table view and its every cell`s width say 1024px , so my requirement is after clicking a cell tableview's width will change logically. And my table view is bit complex.
Every cell contains a custom view which is defined in another class. Please help me..
I do not believe there is a way to modify a single cell's width without changing the width of the table view, only height using tableView:heightForRowAtIndexPath:.
Therefore, you might want to change the width of the custom view inside your cell.
First, to get the cell in question, call cellForRowAtIndexPath: on your table view wherever you need (if you want to change the width on tap, that would probably be in your tableView:didSelectRowAtIndexPath:).
To access your custom view, you may give your custom view a tag number inside of the UITableViewCell by setting the custom view's tag property when creating the cell or in the storyboard. Then, call viewWithTag: on the UITableViewCell instance to get the custom view, and modify the width of its frame property.
Another, perhaps more suitable option would be to use a custom UITableViewCell class for your table view cells. That would mean subclassing UITableViewCell and creating a property for your custom view, which would allow you to access the custom subview through the getter and then change its frame property.
If you need the width of the custom view's container to change (which is currently your cell), simply embed the custom view inside a UIView and modify the UIView's width rather than the UITableViewCell's instance in the same manner described above.
I am implementing a TableViewController. I would like to create the table view with a specific width and height. I tried using initWithFrame and also setting the frame within viewDidLoad. How can I achieve this behavior?
You could just have a regular UIViewController and add a Table View in it.
Then resize it either with the interface builder or by setting its frame.
Don't forget to set the table view dataSource and delegate to the view controller.