Repostioning a UITableView - ios

I have a view controller with
sub view(half the main view size)
table view(half the main view size)
navigation bar button
On click of button I want the sub view to hide and table view to show with full main view size....It is working fine with below code, but just not for the first time.
For first time when I click bar button it hides the sub view and resizes the table view but does not show the result on screen(works well for rest all time).
If incase I scroll the tableview before clicking navigation bar for the first time it works fines...I think it has something to do with table view loading which I cant make out
Code
if(isMapVisible==YES) {
[mapView setHidden:YES];
[tblView setFrame:CGRectMake(self.view.frame.origin.x,
self.view.frame.origin.y+self.navigationController.navigationBar.frame.size.height+[UIApplication sharedApplication].statusBarFrame.size.height,
self.view.frame.size.width,
self.view.frame.size.height-self.navigationController.navigationBar.frame.size.height-[UIApplication sharedApplication].statusBarFrame.size.height-self.navigationController.tabBarController.tabBar.frame.size.height)];
isMapVisible=NO;
} else {
[mapView setHidden:NO];
[tblView setFrame:CGRectMake(mapView.frame.origin.x,
mapView.frame.origin.y+mapView.frame.size.height+1,
mapView.frame.size.width,
mapView.frame.size.height)];
isMapVisible=YES;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
activityCell *cell = (activityCell*)[tableView dequeueReusableCellWithIdentifier:#"activityCell" forIndexPath:indexPath];
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath *)indexPath {
return 100;
}

Call your method like this
[self performSelector:#selector(methodToChangeTableViewFrame) withObject:nil afterDelay:0.1f];
-(void)methodToChangeTableViewFrame {
// Here your code
if(isMapVisible==YES) {
[mapView setHidden:YES];
[tblView setFrame:CGRectMake(self.view.frame.origin.x,
self.view.frame.origin.y+self.navigationController.navigationBar.frame.size.height+[UIApplication sharedApplication].statusBarFrame.size.height,
self.view.frame.size.width,
self.view.frame.size.height-self.navigationController.navigationBar.frame.size.height-[UIApplication sharedApplication].statusBarFrame.size.height-self.navigationController.tabBarController.tabBar.frame.size.height)];
isMapVisible=NO;
} else {
[mapView setHidden:NO];
[tblView setFrame:CGRectMake(mapView.frame.origin.x,
mapView.frame.origin.y+mapView.frame.size.height+1,
mapView.frame.size.width,
mapView.frame.size.height)];
isMapVisible=YES;
}
}

Related

the view from viewForFooterInSection animated unexpected when I call insertRowsAtIndexPaths

[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:indexPathsToBeInserted withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
if (section == someSection) {
UIView *footerView = [[UIView alloc] init];
footerView.backgroundColor = [UIColor someColor];
return footerView;
}
else {
return nil;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
if (section == someSection) {
return 10;
}
else {
return 0;
}
}
Nothing really special.
Just provide a footer view and trigger the insert rows method somehow, the footer view will be animated like dropping down every time, you can watch this easier by turning slow animations on in the simulator.
I don't know how to make the animation gone, it's really unwanted.
I've found a similar issue here - UITableView section footer view position after endUpdates
The solution is to change table view style to Grouped.

How to scroll UIScrollView programmatically which is a subview of the custom UITableViewCell?

I could not figure out why the scroll view is not being scrolled whereas the current_set variable is getting increased.What is the exact position to write the code to scroll the UIScrollView when it is a subview of the UITableView?
Here is my code:
The definition for tableView:cellForRowAtIndexPath: method:
- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier=[NSString stringWithFormat:#"regularExerciseCell%li",indexPath.row];
RegularExerciseCell *cell=[tableView1 dequeueReusableCellWithIdentifier:cellIdentifier];
if(!cell)
{
[tableView1 registerNib:[UINib nibWithNibName:#"RegularExerciseCell" bundle:nil] forCellReuseIdentifier:#"regularExerciseCell"];
cell=[tableView1 dequeueReusableCellWithIdentifier:#"regularExerciseCell"];
}
NSLog(#"cellForRow At indexPath %li",indexPath.row);
return cell;
}
Here is the definition for tableView: willDisplayCell:forRowAtIndexPath: method
-(void)tableView:(UITableView *)tableView willDisplayCell:(RegularExerciseCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if(selectedIndex==indexPath.row)
{
cell.routineViewCard.hidden=NO;
//SCROLL VIEW
float scrollView_width=[UIScreen mainScreen].bounds.size.width;
cell.setsScrollView.tag=indexPath.row;
totalSetsInActiveExercise=[array count];
for (int k=0; k<=totalSetsInActiveExercise; k++)
{
[cell.setsScrollView addSubview:[self subviewOfScrollView:scrollView_width]];
}
cell.setsScrollView.contentSize = CGSizeMake(scrollView_width*([workoutViewSetData count]+1),cell.setsScrollView.frame.size.height);
if(condition) //this condition may be true or false depending upon the scenario
{
[self moveToNextSet:indexPath.row and:#"left"];
}
}
else
{
cell.routineViewCard.hidden=YES;
}
}
The method which is actually scrolling the scroll view
-(void)moveToNextSet:(long)sender_tag and:(NSString*)direction
{
NSIndexPath *indexPath=[NSIndexPath indexPathForItem:sender_tag inSection:1];
RegularExerciseCell *cell=(RegularExerciseCell*) [workoutTableView cellForRowAtIndexPath:indexPath];
if ([direction isEqualToString:#"right"])
{
if(current_set!=0)
current_set--;
}
else if([direction isEqualToString:#"left"])
{
current_set++;
}
CGRect frame = CGRectMake((cell.setsScrollView.bounds.size.width*(current_set)),0,cell.setsScrollView.bounds.size.width,cell.setsScrollView.bounds.size.height);
[cell.setsScrollView scrollRectToVisible:frame animated:YES];
}
Instead of using the 'scrollRectToVisible' method, you may try to set 'contentOffset' of the ScrollView.
[cell.setsScrollView setContentOffset:CGPointMake(x, 0) animated:true];
x is the point you want the scrollview scroll to. If you set to (0,0) which is the front.

How to Add search bar in table view

Actually i have stored too many data in table view and now I want to found my data easily so I need a search bar. Can any one give me a little solution?
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifire = #"CellIdentifire";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifire];
}
cell.textLabel.text = [array objectAtIndex:indexPath.row];
return cell;
}
Follow these steps:
Drag and drop search bar on table view header
Set delegate for search bar to your view controller
Use following code to search:
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
[searchBar setShowsCancelButton:YES];
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
//Remove all objects first.
[self searchTerm:searchText];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
NSLog(#"Cancel clicked");
searchBar.text = #"";
[searchBar resignFirstResponder];
[searchBar setShowsCancelButton:NO];
[self showAllData];// to show all data
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
NSLog(#"Search Clicked");
[searchBar setShowsCancelButton:NO];
[searchBar resignFirstResponder];
[self searchTerm:searchBar.text];
}
-(void)showAllData {
//load all data in _tableViewDataSourceArray, and reload table
}
-(void)searchTerm : (NSString*)searchText
{
if (searchText == nil) return;
_tableViewDataSourceArray = //YOUR SEARCH LOGIC
[self.tableView reloadData];
}
Edit:
In case you wanna create Search bar with code then, Initialize a search bar and pass it as headerView Of your table view, inside
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
Dont's forget to return height of view in
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

SWTableViewCell slide programmatically

How can i slide programmatically SWTableViewCell.
//When i click in a cell i want to open the swtableviewcell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
???
}
i have tried:
[[[SWTableViewCell alloc]init]didTransitionToState:2];
and in swtableviewcell.m
-(void)didTransitionToState:(UITableViewCellStateMask)state{
NSLog(#"state: %lu",state);
if (_cellState == kCellStateCenter)
{
[self.cellScrollView setContentOffset:[self contentOffsetForCellState:kCellStateRight] animated:YES];
[self.delegate swipeableTableViewCell:self scrollingToState:kCellStateRight];
}
}
but is not correct. Can you help me?
Thanks,
Mikel
I wanted to be able to show the utility buttons when a table view cell was selected so I added a method in SWTableViewCell.m
-(void)showUtilityButtonsAnimated:(BOOL)animated {
// Force the scroll back to run on the main thread because of weird scroll view bugs
dispatch_async(dispatch_get_main_queue(), ^{
[self.cellScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
});
_cellState = kCellStateLeft;
if ([self.delegate respondsToSelector:#selector(swipeableTableViewCell:scrollingToState:)])
{
[self.delegate swipeableTableViewCell:self scrollingToState:kCellStateCenter];
}
}
and don't forget to add it to SWTableViewCell.h
- (void)showUtilityButtonsAnimated:(BOOL)animated;
Then call that method from within your -tableView:didSelectRowAtIndexPath: delegate method. For example:
if (indexPath.row == 0) {
SWTableViewCell *cell = (SWTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];
[cell showUtilityButtonsAnimated:YES];
}

UITableView, clicks, mimic button pressed

I am working with UITableview, I want the cells to look like a pressed button, how can I do that?
try this ,
// get selected tableview cell and perform some animation on cell. i am using scale up and down animation
#pragma mark Table View Delgate method
- (void)tableView:(UITableView *)tbView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomTableViewCellForTradeList *cell=(CustomTableViewCellForTradeList *)[tbl_TradeList cellForRowAtIndexPath:indexPath ];
cell.transform = CGAffineTransformMakeScale(0.85,0.85);
[self performSelector:#selector(NavigateToTradeBuySellAfterScaling:) withObject:indexPath afterDelay:0.1];
}
// rescale uitableview cell.
-(void)NavigateToTradeBuySellAfterScaling:(NSIndexPath *)indexPath
{
CustomTableViewCellForTradeList *cell=(CustomTableViewCellForTradeList *)[tbl_TradeList cellForRowAtIndexPath:indexPath ];
cell.transform = CGAffineTransformMakeScale(1.0,1.0);
[self performSelector:#selector(NavigateToTradeBuySellAfterScaling1:) withObject:indexPath afterDelay:0.1];
}
//navigate to other view controller
-(void)NavigateToTradeBuySellAfterScaling1:(NSIndexPath *)indexPath
{
TradeBuySellViewController* tradeBuySellController = [[TradeBuySellViewController alloc] initWithNibName:#"TradeBuySellViewController" bundle:nil :entity];
[self.navigationController pushViewController:tradeBuySellController animated:YES];
}
You can try changing the background view of UITableViewCell when clicked. You can get the clicked cell with this method.
UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];

Resources