UITableView - gap between main header and section header - ios

Working on a UITableView, with a header in both the main tableview and each section. However, we are getting a weird gap between these two headers. We have tried the solutions given in other SO answers, such as:
self.edgesForExtendedLayout
tableView.contentInset
self.automaticallyAdjustsScrollViewInsets = false
But no luck!
I've put together a screenshot of what's happening:
The issue is the gap between the red and green headers, any help much appreciated, happy to share further snippets of the code if needed. The highlighted areas are matched in the view debugger, so I have confirmed it isn't an issue of extra padding anywhere.
Some relevant code:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let sectionHeader = ShareScreenSectionHeaderView()
let title = viewData.sections[section].sectionTitle
let subtitle = viewData.sections[section].sectionSubtitle
sectionHeader.update(title: title, subtitle: subtitle)
return sectionHeader
}
The header set for the table view is a UIView, set using the tableHeaderView property

Here is the code snippet
#property (weak, nonatomic) IBOutlet UITableView *tblLoad;
#property (strong, nonatomic) IBOutlet UIView *lbltblHeader;
- (void)viewDidLoad {
[super viewDidLoad];
self.tblLoad.tableHeaderView = self.lbltblHeader;
self.tblLoad.delegate = self;
self.tblLoad.dataSource= self;
[self.tblLoad reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (section ==0) {
return 5;
}
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 50;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *v =[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 20)];
v.backgroundColor = UIColor.greenColor;
UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 200, 30) ];
lbl.text = [NSString stringWithFormat:#"I am the %ld Section ",section];
lbl.textColor = UIColor.blackColor;
[v addSubview:lbl];
return v;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *cellIdentifier = #"CellReuse";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] ;
//you can customize your cell here because it will be used just for one row.
}
cell.textLabel.text = [NSString stringWithFormat:#"I am the %ld Cell ",(long)indexPath.row];
cell.textLabel.textColor = UIColor.whiteColor;
cell.backgroundColor = UIColor.lightGrayColor;
return cell;
}
I have created a new test project to testcase ur scenario, but i was not able to replicate as you can see there is no gap between the headerview and section header.

Related

Populating a TableView in ViewDidAppear

I am trying to do what I thought would be simple, but is seems quite complex.
I am trying to create a leaderboard screen.
I have the following:
NSArray* playerNames
NSArray* playerScores
My leaderboard tab is a viewcontroller. Inside, it has a tableview. The tableview has an outlet.
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#interface LeaderboardViewController : UIViewController
{
}
- (void)viewDidAppear:(BOOL)animated;
- (void) viewWillDisappear:(BOOL)animated;
#property (weak, nonatomic) IBOutlet UITableView *leaderboardTable;
- (SimonGameModel*) model;
#end
When the view did load, I get the above 2 arrays (both of same length) from my model. They correspond to the latest scores.
What I need is for each table cell to tave 2 lables so that I end up with a leaderboard that looks something like this:
Tim 200
John 100
Jack 50
etc...
I have been reading apple's docs for nearly an hour and I am confused as to how to do this.
I created a prototype with the labels I want.
Thanks
-(void)viewDidLoad {
[leaderboardTable setDataSource:self];
[leaderboardTable setDelegate:self];
}
you must create your custom cell in this way:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [playerNames count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"leader";
UITableViewCell *cell = [leaderboardTable dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UILabel *labelName = [[UILabel alloc] initWhitFrame:CGRectMake(5, 0,160,44);
[labelName setTextAlignment:NSTextAlignmentLeft];
labelName.textColor = [UIColor blackColor];
[cell.contentView addSubView:labelName];
UILabel *labelValue = [[UILabel alloc] initWhitFrame:CGRectMake(165, 0, 150, 44);
[labelValue setTextAlignment:NSTextAlignmentRight];
labelValue.textColor = [UIColor blackColor];
[cell.contentView addSubView:labelValue];
}
labelName.text = [playerNames objectAtIndex:indexPath.row];
labelValue.text = [playerScores objectAtIndex:indexPath.row];
return cell;
}
It sounds like you have not set your LeaderboardViewController as your tableview's dataSource. LeaderboardViewController will have to conform to the UITableViewDataSource protocol:
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDataSource_Protocol/Reference/Reference.html#//apple_ref/doc/uid/TP40006941
Also, remember to register a UITableViewCell xib or class with your tableview.
You will be populating your cells with data from your arrays in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

UITableView separator gone when using viewForFooterInSection

UITableView showing separator even there is no rows. But when I use viewForFooterInSection the separator is gone. I want it to keep showing up. What should I do ?
Here is my code when I add footer in it :
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"s"];
cell.textLabel.text = [NSString stringWithFormat:#"%d", indexPath.row + 1];
cell.textLabel.textAlignment = NSTextAlignmentCenter;
return cell;
}
// -------------------------- footer code start here --------------------------
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 40;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
view.backgroundColor = [UIColor grayColor];
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 40)];
title.text = #"footer here";
title.textAlignment = NSTextAlignmentCenter;
title.textColor = [UIColor whiteColor];
[view addSubview:title];
return view;
}
this github project may give you some advices:
CLTableWithFooterViewController
Of course need some change if you would like the footer always display on the screen even if with a lot of cells.
remove - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
and - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
use this code to keep the footer down (but always visible) and keep the separator:
#implementation tableView {
__weak UIView *_staticView;
}
- (void)viewDidLoad {
UIView *staticView = [[UIView alloc] initWithFrame:CGRectMake(0, self.tableView.bounds.size.height-50, self.tableView.bounds.size.width, 50)];
staticView.backgroundColor = [UIColor redColor];
//add other code for UILabel here
[self.tableView addSubview:staticView];
_staticView = staticView;
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 50, 0);
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
_staticView.transform = CGAffineTransformMakeTranslation(0, scrollView.contentOffset.y);
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
// this is needed to prevent cells from being displayed above our static view
[self.tableView bringSubviewToFront:_staticView];
}
I guess you just have a single section,right? Before you using viewForFooterInSection,it just fills blank cells all the way down.But when you call viewForFooterInSection,tableview will put a footer at the end of the section.Since you don't have the second section,it stops draw any cells.So you can make a blank second section,it will fill blank cells to the second section.

UITableView with fixed section headers

Greets,
I'm reading that the default behaviour of UITableView is to pin section header rows to the top of the table as you scroll through the sections until the next section pushes the previos section row out of view.
I have a UITableView inside a UIViewController and this does not seem to be the case.
Is that just the defualt behaviour for UITableViewController?
Here's some simplified code based on what I have.
I'll show the UIController interface and each table view method I've implemented to create the table view.
I have a helper data source class that helps me index my objects for use with the table.
#interface MyUIViewController ()<UITableViewDelegate, UITableViewDataSource>
#property (nonatomic, readonly) UITableView *myTableView;
#property (nonatomic, readonly) MyCustomHelperDataSource *helperDataSource;
#end
//when section data is set, get details for each section and reload table on success
- (void)setSectionData:(NSArray *)sections {
super.sectionData = sections; //this array drives the sections
//get additional data for section details
[[RestKitService sharedClient] getSectionDetailsForSection:someId
success:^(RKObjectRequestOperation *operation, RKMappingResult *details) {
NSLog(#"Got section details data");
_helperDataSource = [[MyCustomHelperDataSource alloc] initWithSections:sections andDetails:details.array];
[myTableView reloadData];
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(#"Failed getting section details");
}];
}
#pragma mark <UITableViewDataSource, UITableViewDelegate>
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if (!_helperDataSource) return 0;
return [_helperDataSource countSectionsWithDetails]; //number of section that have details rows, ignore any empty sections
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//get the section object for the current section int
SectionObject *section = [_helperDataSource sectionObjectForSection:section];
//return the number of details rows for the section object at this section
return [_helperDataSource countOfSectionDetails:section.sectionId];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell * cell;
NSString *CellIdentifier = #"SectionDetailCell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.textLabel.font = [UIFont systemFontOfSize:12.0f];
}
//get the detail object for this section
SectionObject *section = [_helperDataSource sectionObjectForSection:indexPath.section];
NSArray* detailsForSection = [_helperDataSource detailsForSection:section.sectionId] ;
SectionDetail *sd = (SectionDetail*)[detailsForSection objectAtIndex:indexPath.row];
cell.textLabel.text = sd.displayText;
cell.detailTextLabel.text = sd.subText;
cell.detailTextLabel.textColor = [UIColor blueTextColor];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 50.0f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 30.0f;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger) section {
//get the section object for the current section
SectionObject *section = [_helperDataSource sectionObjectForSection:section];
NSString *title = #"%# (%d)";
return [NSString stringWithFormat:title, section.name, [_helperDataSource countOfSectionDetails:section.sectionId]];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 260, 0)];
header.autoresizingMask = UIViewAutoresizingFlexibleWidth;
header.backgroundColor = [UIColor darkBackgroundColor];
SSLabel *label = [[SSLabel alloc] initWithFrame:CGRectMake(3, 3, 260, 24)];
label.font = [UIFont boldSystemFontOfSize:10.0f];
label.verticalTextAlignment = SSLabelVerticalTextAlignmentMiddle;
label.backgroundColor = [UIColor clearColor];
label.text = [self tableView:tableView titleForHeaderInSection:section];
label.textColor = [UIColor whiteColor];
label.shadowColor = [UIColor darkGrayColor];
label.shadowOffset = CGSizeMake(1.0, 1.0);
[header addSubview:label];
return header;
}
The headers only remain fixed (floating) when the UITableViewStyle property of the table is set to UITableViewStylePlain.
If you have it set to UITableViewStyleGrouped, the headers will scroll up with the cells (will not float).
Change your TableView Style:
self.tableview = [[UITableView alloc] initwithFrame:frame
style:UITableViewStyleGrouped];
As per apple documentation for UITableView:
UITableViewStylePlain- A plain table view. Any section headers or
footers are displayed as inline separators and float when the table
view is scrolled.
UITableViewStyleGrouped- A table view whose sections present distinct
groups of rows. The section headers and footers do not float.
Hope this small change will help you ..
Swift 3.0
Create a ViewController with the UITableViewDelegate and UITableViewDataSource protocols. Then create a tableView inside it, declaring its style to be UITableViewStyle.grouped. This will fix the headers.
lazy var tableView: UITableView = {
let view = UITableView(frame: UIScreen.main.bounds, style: UITableViewStyle.grouped)
view.delegate = self
view.dataSource = self
view.separatorStyle = .none
return view
}()
You can also set the tableview's bounces property to NO. This will keep the section headers non-floating/static, but then you also lose the bounce property of the tableview.
to make UITableView sections header not sticky or sticky:
change the table view's style - make it grouped for not sticky & make it plain for sticky section headers - do not forget: you can do it from storyboard without writing code. (click on your table view and change it is style from the right Side/ component menu)
if you have extra components such as custom views or etc. please check the table view's margins to create appropriate design. (such as height of header for sections & height of cell at index path, sections)

UITableView for iOS

I am very new to Objective-C. I just searched and tried to learn about UITableView.
I have created this UITableView. instead of "a" in all the rows i want it to be in a sequence like "a b c d..." and if i increase the no of rows it should scroll. its not scrolling so here is my code.
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
CGRect frame = self.view.frame;
UITableView *tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
tableView.backgroundColor = [UIColor cyanColor];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cellIDent"];
cell.textLabel.text = #"a";
return cell;
}
#end
set table view Delegates in IB or if u created Programmatically then in viewDidLoad Take one array fill it Dynamically or Statically.Give array count in NumberOfRowsInsection method and reload table view at appropriate place
in your cellForRowAtIndexPath: add these lines
char ch = 'a' + indexPath.row;
cell.textLabel.text = [NSString stringWithFormat:#"%c" , ch];
Check this tutorial, I'm sure it will help you
UItableView Guide
and for scrolling, You don't need to do anything yourselves, as the number of cells increases beyond the provided size, it automatically becomes scrollable and make sure that scrolling is enabled in your UITableView attribute inspector, check the image
Happy Coding
Regards
Use below code.....define alphabetarr in globally then use it in code...
- (void)viewDidLoad
{
alphabetarr = [[NSArray alloc] initWithObjects:#"a",#"b",#"c",#"d",#"e",#"f",#"g",#"h",#"i",#"j",#"k",#"l",#"m",#"n",#"o",#"p",#"q",#"r",#"s",#"t",#"u",#"v",#"w",#"x",#"y",#"z", nil];
CGRect frame = self.view.frame;
UITableView *tableview = [[UITableView alloc] initWithFrame:frame];
tableview.backgroundColor = [UIColor cyanColor];
tableview.separatorColor = [UIColor blackColor];
tableview.delegate = self;
tableview.dataSource = self;
[self.view addSubview:tableview];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [alphabetarr count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 30.0 ;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell!=nil) {
cell = nil;
}
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
cell.textLabel.text = [alphabetarr objectAtIndex:indexPath.row];
return cell;
}

IOS - UISegmented in UITableview

I insert a segmented in uitableview like this url image. If you see the url image, now i'm working ok everything data in uitable,
and...
When i scroll down table i wanna part 1: Image(in url) will scroll scroll to part 2: segmented will stop and if you still scroll uitable part 3: the data will be scroll (keep segmented on the top).
Have you any idea it. Thanks
I just test it, it is working!
Set your UITableView pain
UITableView has two section and each section has only one row.
When edit your section header, make sure only second section has header - second header will be your part2.
first section first row is your part1.
second section first row is your part3.
That's it : )
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = #"test";
return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
#try {
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
switch (section) {
case 0:
{
//nothing
}
break;
case 1:
{
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
lbl.text = #"second";
lbl.textColor = [UIColor blackColor];
[containerView addSubview:lbl];
}
break;
}
return containerView;
}
#catch (NSException *exception) {
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 30;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 1000.0;
}

Resources