UITableView does not bounce/scroll after UISearchBar is added to view - ios

I have a UITableView filled with rows of data. There are only 3 or 4 rows of data which do not reach the bottom of the screen, but the table still bounces vertically as expected when swiping up and down. After adding a UISearchBar to the top row/header of the UITableView in Interface Builder, the table no longer bounces initially. However, after navigating to a different view and returning to the same view, the table once again bounces. Why does the UITableView not bounce when the view is initially loaded, and how do I make it so that it does bounce?
- (void)viewWillAppear:(BOOL)animated {
[table reloadData];
[super viewWillAppear:animated];
}
- (void)viewDidLoad {
...
[table reloadData];
[super viewDidLoad];
}
- (void)viewDidUnload {
...
self.table = nil;
[super viewDidUnload];
}

I was able to solve this problem by implementing the answer on this question.
The key was to add self.tableView.bounces = YES in the viewDidLoad method.

Related

Custom UITableViewController inside Container View

I am trying to insert a custom UITableViewController inside a Container View. The Container View is placed inside a cell of a static UITableView as shown in the figure below.
http://i.stack.imgur.com/A762B.png
I simply want a method to combine static with dynamic cells in the same screen.
In the Identity Inspector when the field Class is empty (i.e. a standard UITableViewController) it works showing an empty dynamic table inside the cell. But when I put my custom class name (that extends UITableViewController) in that field I get a NSInternalInconsistencyException:
[UITableViewController loadView] loaded the "Enx-aT-Rum-view-zY2-9U-Z6d" nib but didn't get a UITableView.
These are the contents of MyCustomUITableViewController:
#implementation MyCustomUITableViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 2;
}
#end
I have to admit that I still don't understand all the logic behind Container View, but I just want to show only one view inside (not doing any swapping or else).
Any help would be appreciated, thanks!
Let me point out the issues here first-
Problem 1: you can not have an controller inside another controller. That means, inside your Static tableview, you can not have a dynamic tableview Controller.
Solution 1:You can have a dynamic TableView.
Problem 2: You can not have a static Tableview inside a View Controller. If you want a Static Tableview then you need to have a UITableViewController rather than the UIViewController.
Solution 2: You just need to delete the ViewController you have and replace with a UITableViewController.
Now to achieve one controller where you can have a dynamic TableView inside a static tableview, you will need to implement you dynamic table's datasource inside the Static Table's ViewController which would be a very bad practise.
The static table should not need to know anything about your dynamic Table, at least about the data that will be populated in your dynamic table. However, if you want a dynamic tableview inside your static tableView then your static tableview's controller needs to implement the UITableViewDatasource.
So, you may want to re-think about the structure.

Custom UITableViewController inside Container View: NSInternalInconsistencyException

I am trying to insert a custom UITableViewController inside a Container View. The Container View is placed inside a cell of a static UITableView as shown in the figure below.
I simply want a method to combine static with dynamic cells in the same screen.
In the Identity Inspector when the field Class is empty (i.e. a standard UITableViewController) it works showing an empty dynamic table inside the cell. But when I put my custom class name (that extends UITableViewController) in that field I get a NSInternalInconsistencyException:
[UITableViewController loadView] loaded the "Enx-aT-Rum-view-zY2-9U-Z6d" nib but didn't get a UITableView.
These are the contents of MyCustomUITableViewController:
#implementation MyCustomUITableViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 2;
}
#end
I have to admit that I still don't understand all the logic behind Container View, but I just want to show only one view inside (not doing any swapping or else).
Any help would be appreciated, thanks!
After banging my head on the desk for so many hours I managed to resolve this.
I had to alloc/init the tableview of MyCustomUITableViewController. I override the loadView method of MyCustomUITableViewController:
- (void) loadView{
self.tableView = [[UITableView alloc]init];
}
And it is good to go!

Autolayout and Collection View Issue

So the basic issue is that when I click the cell, it should go to cell index 1 in the new view controller, but when you have autolayout on. The collectionview content offset change goes away and it's reset. Turn it off, works fine. Autolayout is somehow causing content offset to reset but I'm not sure why or how to resolve this.
Code available here.
https://github.com/HaloZero/AutolayoutCollectionViewIssue
use you code as :--
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self performSelectorOnMainThread:#selector(doyouwork) withObject:nil waitUntilDone:NO];
}
-(void)doyouwork
{
[self snapToCellAtIndex:1 withAnimation:NO];
}
- (void) snapToCellAtIndex:(NSInteger)index withAnimation:(BOOL) animated
{
NSIndexPath *path = [NSIndexPath indexPathForRow:index inSection:0];
[self.collectionView scrollToItemAtIndexPath:path atScrollPosition:UICollectionViewScrollPositionLeft animated:animated];
}
working for my side.
It is not that auto-layout is resetting the content offset, but the subviews have not been positioned when you try to snap the cell.
With auto-layout, the subviews are positioned a bit later (in which the viewWillAppear is called first). Try to use viewDidLayoutSubviews instead for your snapping of the cell:
From the documentation for viewDidLayoutSubviews:
Notifies the view controller that its view just laid out its subviews.
So after all the constraints have been calculated and your subviews have been laid, you can call the snapping method.
- (void)viewDidLayoutSubviews
{
[super viewWillLayoutSubviews];
[self snapToCellAtIndex:1 withAnimation:NO];
}

UITableView - Accessing in Code from ViewWillAppear

I would like to manipulate table cells in my UITableView as it comes back from its detail view, to remind the user what row they last selected. The UITableView was created in IB and there currently isn't a #property or custom class for the UITableView or any other handle. Clearly, it exists when, the user is coming back from the detail view and viewWillAppear is called but I don't see any handle in the debugger for my UITableView.
Any way to simply get a handle to the IB tableView object?
// - The Containing ViewController
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSIndexPath *selected = [self.tableView indexPathForSelectedRow];
if(selected){
//Do something stylish with the last-selected row
NSLog(#"Last row selected was: %#",selected);
}
}
I solved this by creating a class member of NSIndexPath * and set it inside didSelectRowAtIndexPath. Then, when viewWillAppear executes, when the user navigates back fram the table view detail, I have the last-selected row so I can easily highlight it or treat it any way I want:
//The Containing View Controller
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if(self.lastSelectedRow){
[self.tableView selectRowAtIndexPath:self.lastSelectedRow animated:YES scrollPosition:UITableViewScrollPositionNone];
}
}

UITableView with custom cells not entering editing mode

I have a UITableView with custom UITableViewCells.
The table has two sections, the first section has a single row with a UITextField and can only be edited in terms of the text. This section & row cannot be edited from a UITableView perspective
The second section is a list of cells that are generated from an NSArray. These cells are once again custom UITableViewCells comprising of two UITextFields. These cells can be edited from a UITableView perspective, in the sense that the user can delete and insert rows.
In my designated initializer I have specified self.tableView.editing = YES, also I have implemented the method canEditRowAtIndexPath to return YES.
Problem Statement
The table view does not enter editing mode. I do not see the delete buttons or insert buttons against the rows of section 2. What am I missing?
just a suggestion, check whether your controller fit to these requirements :
i use usual UIViewController and it works fine - you need to :
make your controller a delegate of UITableViewDelegate, UITableViewDataSource
implement - (void)setEditing:(BOOL)editing animated:(BOOL)animated
programmatically add EDIT button - self.navigationItem.rightBarButtonItem = self.editButtonItem (if you add EDIT button from builder you will need to call setEditing : YES manually)
Piece of code :)
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
[self.tableView setEditing:editing animated:YES];
}
- (void)tableView
:(UITableView *)tableView didSelectRowAtIndexPath
:(NSIndexPath *)indexPath
{
[self.tableView deselectRowAtIndexPath:indexPath animated:NO];
[self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
// do not forget interface in header file
#interface ContactsController : ViewController<
UITableViewDelegate,
UITableViewDataSource>
Profit!
What if you do [self tableView setEditing:YES animated:YES]; instead of self.tableView.editing = YES;?

Resources