TableView starts in the wrong position initially - ios

This issue has come about as I update an app from iOS 7 to iOS 8. My tableview starts in the wrong position initially. Once you touch it or scroll it. The tableview scrolls into the correct position and works as intended.
The table view is embedded within a UIPageViewController.
UITableView *tableView = (id) [self.view viewWithTag:5];
[tableView registerClass:[BroadcastCell class] forCellReuseIdentifier:CellBroadcastTableIdentifier];
[tableView registerClass:[BroadcastHeaderCell class] forCellReuseIdentifier:CellBroadcastHeaderTableIdentifier];
[tableView setSeparatorInset:UIEdgeInsetsZero];
tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 64.0f)];
tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 0.0f)];
[tableView setDelegate:self];
[tableView setDataSource:self];
tableView.backgroundColor = [UIColor clearColor];
tableView.opaque = NO;
tableView.backgroundView = nil;
tableView.rowHeight = UITableViewAutomaticDimension;
tableView.estimatedRowHeight = UITableViewAutomaticDimension;
Screenshot can be found - https://pbs.twimg.com/media/Byh_5eIIQAA06ok.png:large

I had the same issue. After much effort I found out that in the previous view I was using
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"title-bar-bg.png"] forBarMetrics:UIBarMetricsDefault];
I changed it to
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"title-bar-bg.png"] forBarMetrics:UIBarMetricsDefault];
and the problem was solved.

Select your View Controller and Deselect property 'Adjust Scroll View Insets' on the storyboard (under View Controller section).
OR
Add following lines
self.automaticallyAdjustsScrollViewInsets = NO;

Related

UITableView Scrolling Horizontal issue

this is my code for creating tableView :
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(8, 0, self.view.frame.size.width - 16, self.view.frame.size.height - 8) style:UITableViewStylePlain];
[_tableView registerNib:[UINib nibWithNibName:#"commentCell" bundle:nil] forCellReuseIdentifier:#"commentCell"];
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
_tableView.dataSource = self;
_tableView.delegate = self;
[self.view addSubview:_tableView];
the Problem is that my table is scrolling horizontally I need to know how to deactivate it, I've tried bounce = NO; or contentSize but always same issue my table still allow scrolling horizontally
The Issue was caused by [tableView reaload] function in my viewDidAppear , which reload my table before finishing displaying allCell so this caused inappropriate behaviour

Can't Interact With Controls In Table View Header

I've decided to use a table header view in my app to hold a search bar and a UISegmentedControl. Here is the viewDidLoad of the view controller:
- (void)viewDidLoad
{
[super viewDidLoad];
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 65, 320, 44)];
[self.searchBar sizeToFit];
[self.searchBar setUserInteractionEnabled:YES];
[self setSearchController:[[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self]];
self.mainSegment = [[UISegmentedControl alloc] initWithItems:#[#"YouTube", #"iTunes"]];
[self.mainSegment setFrame:CGRectMake(8, 109, 305, 29)];
[self.mainSegment setSelectedIndex:0];
[self.mainSegment addTarget:self action:#selector(searchTypeChanged:) forControlEvents:UIControlEventValueChanged];
[self.mainSegment setUserInteractionEnabled:YES];
UIView* headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 65)];
[headerView setBackgroundColor:[UIColor clearColor]];
[headerView addSubview:self.searchBar];
[headerView addSubview:self.mainSegment];
[headerView bringSubviewToFront:self.searchBar];
[headerView bringSubviewToFront:self.mainSegment];
[headerView setUserInteractionEnabled:YES];
self.tableView.tableHeaderView = headerView;
self.tableView.userInteractionEnabled = YES;
}
This produces a good result:
However, I can't interact with the search bar or the segmented control. I tried setting userInteractionEnabled to YES as shown above, but the problem still remains. Any ideas?
The height of your header view is 65 points. You're inserting your searchbar at Y=65 so it's beyond the bounds of the header rect. Move your searchbar to Y=0 and your segmented control below it, and it's going to work just fine.
Have a nice day :)
USe delegate methods to set up the headerview.Create and return your headerview in this method.
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
Dont forget to set up the table header height.
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
and the <UITableviewDatasource> in header file to indicate the class is actually having your datasource methods to that table
A must read doc

Black 1px line appearing between selected cells of a clear table

Prior to Xcode 5.1 this problem didn't exist for me but now when building my app with 5.1/5.1.1 I'm seeing black horizontal lines appear when my table cells are selected. Both the table view and cells have clear backgrounds and nothing has changed in the code from the time it was working just fine.
In the viewDidLoad method I set the following tableView parameters:
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.backgroundColor = [UIColor clearColor];
self.view.backgroundColor = [UIColor clearColor];
In the cellForRowAtIndexPath method:
ListViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"ListViewCell"];
if (cell == nil) {
cell = [[ListViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"ListViewCell"];
}
return cell;
}
I'm using a UITableViewCell subclass which has the following in awakeFromNib:
UIView *bkgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
bkgView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
bkgView.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.4f];
self.selectedBackgroundView = bkgView;
This code worked fine when building in version of Xcode < 5.1 but now I'm seeing different results. Occurs with both Plain and Grouped styles.
[self.tableView setSeparatorColor:[UIColor clearColor]];

Adding UISearchBar to UITableView

Background: I have a UIViewController which has a UITableView added programmatically. At the top of the UITableView I have a tablHeaderView in which I have placed a UIView. This UIView has a UISearchBar and a segmentedControl. The idea being: that a user can sort the UITableView by some basic categories such as 'Date/Time' or 'Location' etc. They can also search by an item in the programme.
Problem: When I tap the search bar it resizes (which I don't want) and then when I cancel the search it resizes again and stays there until the UIViewController is exited and loaded again.
Code:
-(void)loadTableView
{
usableSpace = [[UIScreen mainScreen] applicationFrame];
usableWidth = usableSpace.size.width;
usableHeight = usableSpace.size.height;
_tableView = [[UITableView alloc] init];
[_tableView setFrame:CGRectMake(0,0,usableWidth, usableHeight)];
[_tableView setDataSource:self];
[_tableView setDelegate:self];
[self.view addSubview:_tableView];
_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, usableWidth, 44)];
_searchBar.showsCancelButton = YES;
NSLog(#"searchBar height = %fl", _searchBar.frame.size.height);
segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Date/Time", #"Location", #"Speaker", nil]];
segmentedControl.frame = CGRectMake(0, (_searchBar.frame.size.height), usableWidth, (usableHeight * 0.075));
[segmentedControl addTarget:self action:#selector(sortList) forControlEvents:UIControlEventValueChanged];
searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;
searchDisplayController.searchResultsDelegate = self;
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, usableWidth, (usableHeight * 0.15))];
[headerView addSubview:_searchBar];
[headerView addSubview:segmentedControl];
_tableView.tableHeaderView = headerView;
[self.view addSubview:_tableView];
self.tableView = _tableView;
}
What I have tried: I have tried setting the size of the SearchBar, not setting its size. Not having it in a UIView in the tableHeaderView, instead just having it there on its own.
Why does it resize at all and how can I get it to stop?
EDIT: I have just tried the following: In storyboard (where the UIViewController was originally created) I have selected the UIVC in question and in attributes inspector I deselected 'Under Top Bars' and 'Under Bottom Bars' and this appears to have fixed the first part of the animation problem. Now, when I tap in the search bar, the search becomes active but the searchBar does NOT resize. However, when I cancel the searchBar the searchBar still animates and resizes as per the last image in my screenshot. What could be causing THAT resizing?
i've been banging my head on this for too freakin long... props to this dude here for the clues.
if you want your UISearchBar to be in the header along with other views and to scroll with the header instead of sticking at the top, then you've got to remove it and re-add it after the search completes. to be clear, build your header like you already do and then throw this in there as well to handle the screwy animations.
-(void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller {
[self.mySearchBar removeFromSuperview];
self.mySearchBar.frame = CGRectMake(0, 0, 320, 44);
[self.table.tableHeaderView addSubview:self.mySearchBar];
}
As Timothy Moose notes here, "It seems that UISearchDisplayController makes an undocumented assumption that the search bar is the only content of the header view"
...yay apple.
Ok, after hours of reading up and trying different things I think I have found out how to have a UISearchBar where I want it and it seems to work ok. I believe the problem was to do with either having multiple views in the tableHeaderView OR the UISearchBar did not like being with another view inside a 'containing' view.
Here is the code I ended up with that allowed me to have a segmentedControl at the top of my UITableView AND a UISearchBar which actually sat at the top of my UIView (to which the UITableView was added).
-(void)loadTableView
{
usableSpace = [[UIScreen mainScreen] applicationFrame];
usableWidth = usableSpace.size.width;
usableHeight = usableSpace.size.height;
_tableView = [[UITableView alloc] init];
[_tableView setFrame:CGRectMake(0,0,usableWidth, usableHeight)];
[_tableView setDataSource:self];
[_tableView setDelegate:self];
[self.view addSubview:_tableView];
_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, usableWidth, 44)];
[_searchBar setShowsCancelButton:YES];
NSLog(#"searchBar height = %fl", _searchBar.frame.size.height);
segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Title", #"Date/Time", #"Speaker", nil]];
segmentedControl.frame = CGRectMake(0, (_searchBar.frame.size.height), usableWidth, (usableHeight * 0.075));
segmentedControl.selectedSegmentIndex = 0;
[segmentedControl addTarget:self action:#selector(sortList) forControlEvents:UIControlEventValueChanged];
searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;
searchDisplayController.searchResultsDelegate = self;
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, usableWidth, (usableHeight * 0.15))];
[headerView addSubview:segmentedControl];
_tableView.tableHeaderView = headerView;
[self.view addSubview:_tableView];
[self.view addSubview:_searchBar];
self.tableView = _tableView;
}
This meant that I had:
UIViewController with a UINavigationController, UISearchBar, UITableView. The UITableView had a segmentedConrol. The UISearchBar sits just under the UINavigationController and is always present. The UISearchBar sits at the top of the UITableViewController and scrolls with the UITableViewCells.
If anyone knows why the UISearchBar acted as it did in the question then I would be grateful if they could leave a comment. I know that this problem (or very similar) has been experienced by MANY people and I haven't found a definitive answer anywhere.

Strange Behaviour of Selected UITableViewCell in iOS7

I am porting my application to iOS 7 and having some strange behaviour with my tableViews. When I select a cell, the separators disappear for no apparent reason. Not only that, but the background image for the selected state also shifts about 1 pixel upward, even though its frame does not indicate this. I have none of these issues with iOS 6 or older operating systems.
I define my table view the following way:
statisticsTableView = [[UITableView alloc] initWithFrame:CGRectMake(170.0f, 0.0f, 150.0f, window.size.height) style:UITableViewStylePlain];
statisticsTableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"background.png"]];
statisticsTableView.separatorColor = [UIColor darkGrayColor];
//statisticsTableView.separatorInset = UIEdgeInsetsZero; // <- Makes app crash in iOS6 or older
statisticsTableView.showsHorizontalScrollIndicator = NO;
statisticsTableView.delegate = self;
statisticsTableView.dataSource = self;
The cell is sub-classed and the relevant parts are the following (both regular and selected bacground images are the exact same size and appear correct in iOS6 or older, it is only iOS 7 where the selected BG image appears slightly higher):
UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 150.0f, 70.0f)];
backgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"cell_background_corp.png"]];
self.backgroundView = backgroundView;
UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 150.0f, 70.0f)];
backgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"cell_background_corp_selected.png"]];
self.selectedBackgroundView = backgroundView;
Now, I do know that other people had already asked this question (or, at least part of it), but none of the answers there worked for me. For further reference, I tried these "solutions" and they did NOT work:
Putting this into my didSelectrowAtIndexPath made my cells deselect.
But, I need my selected cell to stay selected, and besides
it looked really awkward with the blinking as swithces between states:
[self.tableView deselectRowAtIndexPath:indexPath animated:NO];
[self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
The other one:
Putting this into the heightForRowAtIndexPath delegate method
as a return value did absolutely nothing:
ceilf(rowHeight);
And finally:
This was my attempt to fix the background image position bug. I modified
the way I added the selected background view in the subclassed tableView cell,
but it did not do squat:
UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 150.0f, 70.0f)];
if (iOSVersion >= 7.0f)
{
backgroundView.frame = CGRectMake(0.0f, 1.0f, 150.0f, 70.0f);
}
backgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"cell_background_corp_selected.png"]];
self.selectedBackgroundView = backgroundView;
I had a similar problem and what fixed it for me was to override layoutSubviews in the UITableViewCell subclass like this:
- (void)layoutSubviews
{
[super layoutSubviews];
self.selectedBackgroundView.frame = self.backgroundView.frame;
}
The important thing is to have the [super layoutSubviews]call first.
By the way, I think the default behavior is intended in iOS7. If you look at the Apple Mail app, you see that the separator line also disappears when selecting a cell and that the active background is drawn where the separator line used to be.

Resources