UITableView subclass with default style grouped - ios

I have no clue how can I set grouped style as default to UITableView subclass. My goal is to get grouped TableView (ofc) but by something like that.
TableSubclass *myView = (TableSubclass*)some.other.view
Is this possible? I will be greatful for any advice.
UPDATE
I have ios 6 app with custom UITabBar and custom more section.
My "custom" code
- (void)viewDidLoad
{
[super viewDidLoad];
firstUse=true;
UINavigationController *moreController = self.moreNavigationController;
moreController.navigationBar.barStyle = UIBarStyleBlackOpaque;
self.moreNavigationController.delegate = self;
if ([moreController.topViewController.view isKindOfClass:[UITableView class]])
{
UITableView *view = (UITableView *)moreController.topViewController.view;
view = [view initWithFrame:view.frame style:UITableViewStyleGrouped];
UIView* bview = [[UIView alloc] init];
bview.backgroundColor = [UIColor blackColor];
[view setBackgroundView:bview];
moreTableViewDataSource = [[NXMoreTableViewDataSource alloc] initWithDataSource:view.dataSource];
view.dataSource = moreTableViewDataSource;
}
}
It's working great under 6 but under 7 UITableView don't respond but when I remove
[view initWithFrame:view.frame style:UITableViewStyleGrouped];
It respond again but I lose grouped style.

Are you sure that the table view is not yet configured?
from UITableView Class Reference
When you create a UITableView instance you must specify a table style,
and this style cannot be changed
Have a look how to create and configure UITableView.
Generally you use UITableViewController and then in:
- (void)loadView
{
UITableView *tableView =
[[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]
style:UITableViewStylePlain];
...
}

Related

can't show headerVIew in tableVIew correctly

I create 2 custom tableViews when I use this code:
UITableView *mainTableView=[[MainTableView alloc]init];
[self.view addSubview:mainTableView];
The headerView I create didn't show.
UITableView *mainTableView=[[MainTableView alloc]init];
self.view=mainTableView;
This code shows the right view. What happened to my headerView?
UPDATE:MainTableView.m
+(instancetype)createMainView
{
return [[self alloc]init];
}
-(instancetype)initWithFrame:(CGRect)frame
{
self=[super initWithFrame:frame];
[self addmainTableView];
[self addHeadImageViewAndPageControl];
[self addTitleLabelAndNavigationButton];
return self;
}
#pragma mark -add view to main view
-(void)addHeadImageViewAndPageControl
{
UIScrollView *headImageView=[[UIScrollView alloc]init];
headImageView.frame=CGRectMake(0, 0, yScreenWidth, yScreenHeight/3);
headImageView.backgroundColor=[UIColor redColor];
self.tableHeaderView=headImageView;
//add pagecontroll
UIPageControl *headPageControl=[[UIPageControl alloc]initWithFrame:CGRectMake(-100, yScreenHeight/3-30, yScreenWidth/3, 25)];
headPageControl.numberOfPages=5;
headPageControl.pageIndicatorTintColor=[UIColor lightGrayColor];
headPageControl.currentPageIndicatorTintColor=[UIColor whiteColor];
headPageControl.currentPage=1;
[self.tableHeaderView addSubview:headPageControl];
}
this is my headerView.
The problem of your code is not set frame for Tableview. Put the code below before you add tableView to self.view.
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
or
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

UITableView contentSize wrong when using UISearchBar

UITableView is reporting a bigger contentSize than expected when using a UISearchBar. With zero cells, the expected content height would be zero. Instead, the following code outputs 612 in iPhone 4-inch running iOS 7.
#implementation HPViewController {
UITableView *_tableView;
UISearchBar *_searchBar;
UISearchDisplayController *_searchController;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
_tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:_tableView];
_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
_searchController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
_searchController.delegate = self;
_searchController.searchResultsDataSource = self;
_tableView.tableHeaderView = _searchBar;
}
- (void)viewDidLayoutSubviews
{
CGSize contentSize = _tableView.contentSize;
NSLog(#"%f", contentSize.height); // 612.000000
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 0; }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { return nil; };
#end
Commenting the line that sets the header view makes the code output 0, as expected.
Also, if I assign an empty UIView as the header, the contentSize will be correct and match the height of the header. The problem only happens with UISearchBar.
Is there any way around this? Am I doing something wrong?
Placing the UISearchBar inside a container UIView mostly fixes the problem.
- (void)viewDidLoad
{
[super viewDidLoad];
_tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
_tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:_tableView];
UIView *searchBarContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
_searchBar = [[UISearchBar alloc] initWithFrame:searchBarContainer.bounds];
[searchBarContainer addSubview:_searchBar];
_searchController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
_searchController.delegate = self;
_searchController.searchResultsDataSource = self;
_tableView.tableHeaderView = searchBarContainer;
}
Unfortunately the UISearchBar glitches in some scenarios that I couldn't isolate yet. I opted to calculate the contentSize manually by adding the height of all cells.
Starting from iOS11, UITableView uses estimate row/header/footer height to calculate initial contentSize by default. In fact, if you tap on the search bar and dismiss the keyboard, content size will have the correct value.
To fix this behavior, set estimate row, header and footer height to 0 in IB instead of Automatic:
or just do it programmatically:
self.tableView.estimatedRowHeight = 0;
self.tableView.estimatedSectionHeaderHeight = 0;
self.tableView.estimatedSectionFooterHeight = 0;
Add this delegate method
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
return [[UIView alloc] initWithFrame:CGRectZero];
}
Maybe your view for footer is groing up because there is no cells to show.

Why UITableView works but UIView doesn't

I'd like to pop an view with textInput box and button. I can't understand why UITableView works but UIView doesn't.
- (void)viewDidLoad
{
[super viewDidLoad];
self.containerView = [[UITableView alloc] initWithFrame:CGRectMake(20, 100,200,200)];
[[self view]addSubview:self.containerView];
}
Use above code I can saw the view. But if I change to:
- (void)viewDidLoad
{
[super viewDidLoad];
self.containerView = [[UIView alloc] initWithFrame:CGRectMake(20, 100,200,200)];
[[self view]addSubview:self.containerView];
}
I will get nothing, a black screen. Nothing displayed.
Could someone tell me the reason? Thank you very much.
self.containerView = [[UIView alloc] initWithFrame:CGRectMake(20, 100,200,200)];
self.containerView.backgroundColor = [UIColor redColor];
try this code
.

Creating UITableView in init failed

I'm trying the following in Xcode 4.3.2. I've created a single view application. My ViewController implements UITableViewDelegate, UITableViewDataSource.
In ViewController.m:
UITableView *tv = [[UITableView alloc] initWithFrame:CGRectMake(0, 10, 120, 100)
style:UITableViewStylePlain];
tv.delegate = self;
tv.dataSource = self;
[self.view addSubview:tv];
I implemented numberOfSectionsInTableView, numberOfRowsInSection, cellForRowAtIndexPath and didSelectRowAtIndexPath. It works fine, and displays "welcome" in two rows. But If i place the UITableView creation in the init, then it is not working.
- (id)init
{
self = [super init];
if (self) {
UITableView *tv = [[UITableView alloc] initWithFrame:
CGRectMake(0, 10, 120, 100) style:UITableViewStylePlain];
tv.delegate = self;
tv.dataSource = self;
[self.view addSubview:tv];
}
return self;
}
Two things:
if you're loading from a xib file or storyboard, the initializer used is initWithCoder:.
the view controller's views are not loaded until they're needed, which means viewDidLoad: rather than any init... method. (Though, viewDidLoad: is triggered by a reference to the view property.)

Can I force a UITableView to hide the separator between empty cells? [duplicate]

This question already has answers here:
Eliminate extra separators below UITableView
(34 answers)
Closed 6 years ago.
When using a plain-style UITableView with a large enough number of cells that the UITableView cannot display them all without scrolling, no separators appear in the empty space below the cells. If I have only a few cells the empty space below them includes separators.
Is there a way that I can force a UITableView to remove the separators in the empty space? If not I'll have to load a custom background with a separator drawn in for each cell which will make it harder to inherit behavior.
I found a somewhat similar question here, but I can't use a grouped UITableView in my implementation.
For iOS 7.* and iOS 6.1
The easiest method is to set the tableFooterView property:
- (void)viewDidLoad
{
[super viewDidLoad];
// This will remove extra separators from tableview
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
}
For previous versions
You could add this to your TableViewController (this will work for any number of sections):
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
// This will create a "invisible" footer
return 0.01f;
}
and if it is not enough, add the following code too:
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
return [UIView new];
// If you are not using ARC:
// return [[UIView new] autorelease];
}
For Swift:
override func viewDidLoad() {
super.viewDidLoad()
tableView.tableFooterView = UIView() // it's just 1 line, awesome!
}
You can achieve what you want by defining a footer for the tableview. See this answer for more details:Eliminate Extra separators below UITableView
Using the link from Daniel, I made an extension to make it more usable:
//UITableViewController+Ext.m
- (void)hideEmptySeparators
{
UIView *v = [[UIView alloc] initWithFrame:CGRectZero];
v.backgroundColor = [UIColor clearColor];
[self.tableView setTableFooterView:v];
[v release];
}
After some testings, I found out that the size can be 0 and it works as well. So it doesn't add some kind of margin at the end of the table. So thanks wkw for this hack. I decided to post that here since I don't like redirect.
Swift Version
The easiest method is to set the tableFooterView property:
override func viewDidLoad() {
super.viewDidLoad()
// This will remove extra separators from tableview
self.tableView.tableFooterView = UIView(frame: CGRectZero)
}
For Swift:
self.tableView.tableFooterView = UIView(frame: CGRectZero)
For newest Swift:
self.tableView.tableFooterView = UIView(frame: CGRect.zero)
If you use iOS 7 SDK, this is very simple.
Just add this line in your viewDidLoad method:
self.yourTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
Setting the table's separatorStyle to UITableViewCellSeparatorStyleNone (in code or in IB) should do the trick.
I use the following:
UIView *view = [[UIView alloc] init];
myTableView.tableFooterView = view;
[view release];
Doing it in viewDidLoad. But you can set it anywhere.
The following worked very well for me for this problem:
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
CGRect frame = [self.view frame];
frame.size.height = frame.size.height - (kTableRowHeight * numberOfRowsInTable);
UIView *footerView = [[UIView alloc] initWithFrame:frame];
return footerView; }
Where kTableRowHeight is the height of my row cells and numberOfRowsInTable is the number of rows I had in the table.
Hope that helps,
Brenton.

Resources