UITableView only displays one section
You made a mistake, wrong method. Let change numberOfSectionsInTableView(tableView:)
to:
func numberOfSections(in tableView: UITableView) -> Int {
return self.section.count
}
UITableView with multiple sections shows only one section
You don't have a break after you set the count for section 1 and it falls through and is set to zero.
case 1:
NSLog(@"section called"); // This line is logged
count = 1;
break; // always put break!
UITableview displays only 1 section
Change
nummberOfSetions
To
numberOfSections
why can I only present my first section in TableView Swift ?
You haven't implemented the numberOfSections
data source method.
func numberOfSections(in tableView: UITableView) -> Int {
return sectionTitles.count
}
Without this, the table assumes 1 section.
UITableView showing only one cell
Although I like Dhaval D approach more than what I did in a recent project, mainly the observer on table content size, here is what I did under a time crunch.
Table view is in a container view. Container view has a height constraint on it. Table view is pinned on all sides to Container view. Anytime tableview data changes, I called
func updateTableSize() {
self.tableView.layoutIfNeeded()
var tableFrame = tableView.frame
tableFrame.size.height = tableView.contentSize.height
tableFrame.size.width = tableView.contentSize.width
tableView.frame = tableFrame
heightConstraint.constant = tableFrame.size.height
}
Is it possible to scroll only one section of a UITableview and disable others?
You can do that by composing two independent UITableViews
together.
The first one displays your first section and has a fixed frame height. Inside this UITableView
scrolling will be enabled and possible just as usual.
Below that UITableView
, simply add another one. You can do this e.g. in the storyboard by constraining them to each another (first one's bottom equals second one's top). That second UITableView
will display the two remaining sections and you can set a flag to disable scrolling and there you are!
One problem I see is that (especially on smaller devices) there might not be enough space left in the second UITableView
to display the entire content. A workaround for that could be to constraint the upper UITableView
to a height that equals exactly the view's total height
minus The content size of the second UITableView
. You would have to do that programmatically, though.
I hope you get the idea, if not just let me know and I will try to provide screenshots.
-- EDIT --
Maybe I got your question wrong. If you want to have the middle section scrollable you could either use the same approach but with even three UITableViews
or consider adding simple UIViews
above and below the UITableView
. You would have to move the content out of your UITableViewCells
to these views, then.
Another idea that comes to my mind are UITableViewHeaderFooterViews
. You can queue whatever UIView
you want to have sticky above a section as a UITableViewHeaderFooterView
.
As you can see, there's even plenty of options there :)
Show a table view footer on only one section
Are you also returning nil for your viewForFooter method in the other sections?
We will likely need to know how you check if a section has an addToCart button... but here's my guess:
- (CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section {
if (self.isOpen && [self hasCartForSection:section]) {
return 35.0;
}
else {
// One table view style will not allow 0 value for some reason
return 0.00001;
}
}
- (UIView*)tableView:(UITableView*)tableView viewForFooterInSection:(NSInteger)section {
if (self.isOpen && [self hasCartForSection:section]) {
UIView *footerView = [UIView.alloc init];
// Build your footer
return footerView;
}
return nil;
}
- (BOOL)hasCartForSection:(int)section {
BOOL someCondition = NO;
// Do your check
if (someCondition) {
return YES;
}
return NO;
}
Custom UITableViewCell only displays one UILabel, even though there are two on storyboard
Most probably you don't have enough room to fit both labels. You can either set the row height in storyboard by:
- Selecting your tableview
- Go to size inspector
- Change the
Row Height
OR
You can implement tableView(_:heightForRowAt:)
method in your tableView
's delegate. i.e.
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
// return desired height for your cell
return 90.0
}
Related Topics
Why Is Swift Counting This Grapheme Cluster as Two Characters Instead of One
Why Do I Get Source Kit Service Terminated Error
Enum Named 'Type' in Nested Class Fails
Swift Switch Case Compiler Error
Why Is My Libraries Not Able to Expand on The Cocoapods and Shows as Objective-C Not Swift
What's The Difference Between [String] VS. [(String)]
Get Output Frames Failed, State 8196
Sbdata Is Wrong When Sbvalue Comes from a Swift Dictionary
What Does an Underscore "_" Mean in Swift
Save/Copy a File from Bundle to Desktop Using Nssavepanel
Navigationlink Inside .Searchable Does Not Work
Combined Watch Os and iOS Framework
Difference When Declaring Swift Protocol Using Inheritance from Another Protocol or Using Where Self
Does Cocoa Connection Binding to Nstoolbaritem Prevent Deinitializing