Table View not formatting properly - xcode

I have just added a table view for my app settings and for some reason it is not formatting properly.
The content is set to static cells and the style is grouped. I've tried messing around with a few things but I can't get it to display properly.
This is how it should look:
If anyone has any ideas as to why this could be happening, I would very much appreciate the advice.

In ios8 tableView has dynamic content.Use rowHeight explicitly to define rowHeight in your code
self.yourTableView.estimatedRowHeight = 44;
self.yourTableView.rowHeight = 44;
if you want to not use auto layout.Write this in viewDidLoad
or
add constraints to your tableViewCells simplet way is to add from constraint inspector as reset to suggested constraints
and write in viewDidLoad
tableView.estimatedRowHeight = 44
tableView.rowHeight = UITableViewAutomaticDimension

Related

Add Table View footer in Storyboard

Does anyone know if you can add a Table View footer in Storyboard?
I was able to add a header, but I can't find a way to add a footer too.
I have a programatic footer right now using viewForFooterInSection:, but I want to put it in Storyboard instead.
EDIT:
I'm able to put a UIView in the Storyboard right here if I drag it on the UITableView.
And if I create an outlet with #IBOutlet weak var footerView: UIView! and set it to self.tableView.tableFooterView = footerView and set self.tableView.sectionFooterHeight = 200 I still can't get anything to show up.
oh, I had a totally same problem and struggle with it almost two hours.
I turned out that I had a code which made a tableView unnecessary separator line clear in viewDidLoad(). When I remove it, it worked file.
In short,
it can be possible that you have this code in viewDidLoad()
tableView.footerView = UIView.init()

NSCollectionView or NSTableView automatic height adjustment of cells

In iOS you could use:
self.tableView.rowHeight = UITableViewAutomaticDimension
to achieve automatic height adjustment of cells.
Is there any way to achieve this behaviour in macOS with NSCollectionView or NSTableView?
Talking about NSTableView... As of El Capitan there was no easy way to do this. Not sure about new Sierra SDK.
From my experience, the most reliable way to regulate table row's height is to use NSTableViewDelegate method:
func tableView(tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
//Here you do some magic measuring actual content of the cell and returning heigh
}
The only positive thing is that some controls have: fittingSize property and can simplify process.
Also, dynamic content change will require you to call noteHeightOfRowsWithIndexesChanged method of table view, to cause that delegate method be called again where your code will recalculate new height.
With macOS 11 and newer, the situation is as follows:
NSTableView can now actually use auto layout to achieve automatic height adjustment of cells. You must properly configure your cell template(s) to use auto layout, and you must instruct the table to use auto layout (can also be done in the storyboard) for row height calculations:
tableView.usesAutomaticRowHeights = true
NSCollectionView is currently (2021) still behind in features compared to the iOS counterpart, and still doesn't support any tableview feature like automatic row heights.

Imageview changes location on runtime - Swift

Hello,
My imageView that I have set in my custom table view cell moves the set image to a different place on run time.
The thing that confuses me is that if I manually set the image in the attributes inspector the image is in the place it is supposed to be. Only when setting the image programatically does this problem arise.
Below is the code I am using my cell creation function to generate the tick or cross.
// Cell Information
if answersBool[indexPath.row] == false {
cell.imageView!.image = UIImage(named: "crossIcon.png")
} else {
cell.imageView!.image = UIImage(named: "tickIcon.png")
}
This is what happens on run time.
This is what the cell looks like in my storyboard.
First of all remove all constraint from your imageView after that select your imageView and click on pin menu then add this five constraint shown in below Image:
Hope it will help you.
You need to constraints for your imageView in storyboard.
Try sticking it to right.

iOS 8 Self Sizing Cells - Allow Zero Height

I am using the self sizing cell feature and it works well until I want to hide a cell completely. I moved away from heightForRowAtIndexPath for this and I setup the following:
override func viewDidLoad() {
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 0
}
However when I have no text for a tableviewcell to render I get the following message:
Warning once only: Detected a case where constraints ambiguously
suggest a height of zero for a tableview cell's content view. We're
considering the collapse unintentional and using standard height
instead.
I really just need a way to hide / show content dynamically. I am using a static tableview for this, so maybe I am approaching this wrong?
I found the solution.
First, for a static table to use self sizing correctly, you can only have one label per table cell. I was trying to put in a lot of content into a cell and only the first label would size the cell. I could be wrong about the rule of one label per cell, and the problem might be constraints / auto layout related. I've watched the WWDC video on this, and the way I had it setup, should have worked with my existing constraints, as they where set to the contentView of the cell.
Secondly, the UI updates needed to be coupled with begin and end rules, and a reload.
tableView.beginUpdates()
//-- You Table UI changes
tableView.reloadData()
tableView.endUpdates()
You can also replace reloadData with reloadRowsAtIndexPaths to be specific to the rows to update, but my instance required all rows to be updated.
I had a similar problem some time ago. I would use dynamic cells, and remove the hidden cells in
numberOfRowsInSection
and in
cellForRowAtIndexPath
You should try to add a property, like "hidden" in your cell content array, and check when you load/reload your data. That works (of course) still fine with Autolayout.

Can I disable autolayout for a specific subview at runtime?

I have a view that needs to have its frame manipulated programmatically - it's a kind of document view that wraps to its content which is then scrolled and zoomed around a superview by manipulating the frame origin. Autolayout fights with this at runtime.
Disabling autolayout completely seems a bit harsh because it could reasonably be used to handle layout for the other views. It seems like what I might want is some kind of "null constraint".
I had the same problem. But I have resolved it.
Yes, you can disable auto layout at runtime for a specific UIView, instead of disabling it for the whole xib or storyboard which is set by default in Xcode 4.3 and later.
Set translatesAutoresizingMaskIntoConstraints to YES, before you set the frame of your subview:
self.exampleView.translatesAutoresizingMaskIntoConstraints = YES;
self.exampleView.frame = CGRectMake(20, 20, 50, 50);
I had a similar issue where Autolayout was overriding some of my frame-setting at run time (I had a dynamic view that in some cases pushed a new view controller...pushing and then pressing Back would reset the initial view).
I got around this by putting my manipulation code in viewDidLayoutSubviews of my View Controller. This seems to get called after whatever constraint mojo gets called, but before viewDidAppear, so the user is none the wiser.
Perhaps just setting translatesAutoresizingMaskIntoConstraints to YES (and not adding additional constraints affecting that view) will let you set the frame without fighting the auto layout system.
In iOS 8 you can set an NSLayoutConstraint to be active or not. So if I'm using interface builder, I add all my constraints to an OutletCollection and then activate or deactivate using:
NSLayoutConstraint.deactivateConstraints(self.landscapeConstraintsPad)
NSLayoutConstraint.activateConstraints(self.portraitConstraintsPad)
The particular application I'm using it for here is having different constraints in portrait and landscape mode and I activate/deactivate based on the rotation of the device. It means I can create some complex layout changes all in interface builder for both orientations, and still use auto layout without the verbose auto layout code.
Or you can activate / deactivate using removeConstraints and addConstraints.
I don't know if this will help anyone else, but I wrote a category to make this convenient because I find myself doing this a lot.
UIView+DisableAutolayoutTemporarily.h
#import <UIKit/UIKit.h>
#interface UIView (DisableAutolayoutTemporarily)
// the view as a parameter is a convenience so we don't have to always
// guard against strong-reference cycles
- (void)resizeWithBlock:(void (^)(UIView *view))block;
#end
UIView+DisableAutolayoutTemporarily.m
#import "UIView+DisableAutoResizeTemporarily.h"
#implementation UIView (DisableAutoResizeTemporarily)
- (void)resizeWithBlock:(void (^)(UIView * view))block
{
UIView *superview = self.superview;
[self removeFromSuperview];
[self setTranslatesAutoresizingMaskIntoConstraints:YES];
__weak UIView *weakSelf = self;
block(weakSelf);
[superview addSubview:self];
}
#end
I use it like this:
[cell.argumentLabel resizeWithBlock:^(UIView *view) {
[view setFrame:frame];
}];
Hope it helps.
You can set the translatesAutoresizingMaskIntoConstraints type Boolean, Value Yes in the User Defined Runtime Attributes of the UIView you want in the xib/storyboard.
In my view I had a Label and a Text. The label had pan gesture. The label moves around fine during drag. But when I use the text box keyboard, the label resets its position to the original location defined in auto layout. The issue got resolved when I added the following in swift for the label. I added this in viewWillAppear but it can be added pretty much anywhere you have access to the target field.
self.captionUILabel.translatesAutoresizingMaskIntoConstraints = true
Open project in 4.5
Select storyboard
Open the file inspector
Under Interface Builder Document uncheck 'Use Autolayout'
You can split across multiple storyboards if you want to use autolayout for some views.
For me it worked to create the subview programmatically, in my case the auto layout was messing with a view that I needed to rotate around its center but once I created this view programmatically it worked.
I've encountered a similar scenario, where I joined a project that was initiated with auto-layout, but I needed to make dynamic adjustments to several views. Here is what has worked for me:
Do NOT have views or components laid out in interface builder.
Add your views purely programmatically starting with alloc/init and setting their frames appropriately.
Done.
This happened to me in a project without storyboards or xib files. All 100% code. I had an ad banner at the bottom and wanted the view bounds to stop at the ad banner. The view would resize itself automatically after loading. I tried every resolution on this page but none of them worked.
I ended up just creating a sub view with the shortened height and placed that in into the main view of the controller. Then all my content went inside the sub view. That solved the problem very easily without doing anything that felt like it was going against the grain.
I am thinking if you want a view that is not the normal size that fills the window then you should use a sub view for that.
Instead of disabling autolayout, I would just calculate the new constraint with the frame you are replacing. That appears to me to be the appropriate way. If you are adjusting components that rely on constraints, adjust them accordingly.
For example, if you have a vertical constraint of 0 between two views (myView and otherView), and you have a pan gesture or something that adjusts the height of myView then you can recalculate the constraint with the adjusted values.
self.verticalConstraint.constant = newMyViewYOriginValue - (self.otherView.frame.origin.y + self.otherView.frame.size.height);
[self.myView needsUpdateConstraints];
For those of you who are using auto layout, please check out my solution here. You should be making #IBOutlet's of the constraints you want to adjust and then change their constants.
if it's xib file:
select the .xib file
select the "File's Owner"
show the Utilities
click on: "File Inspector"
Under "Interface Builder Document" disable: "Use Autolayout"

Resources