Pop UITableViewCell subview over entire screen - ios

I have a tableView, with a UIWebView in each row.
When the webView is tapped, I'd like for it to just expand and take over the entire screen.
However, there a few problems, and I'm not sure which is interfering. First I tried something like this:
webView.frame = self.view.bounds;
webview.bounds = self.view.bounds;
cell.clipsToBounds = NO;
webView.clipsToBounds = NO;
However, the webView seems to have trouble overtaking the cell, doesn't expand to proper width and generally doesn't work. I also thought about manipulating the cell height, but it seems to be more trouble than it should be.
So basically, I'd like to move this webView to the top of the stack and change its frame size.
Otherwise I may just instantiate a new webView which seems like a waste. Any ideas? Thanks

Don't change the frame of the web view. Have the web view tied to all four sides of the cell using constraints (or at least the top and bottom), and when you tap on the cell, and it gets selected, use the indexPathForSelectedRow property in heightForRowAtIndexPath to make that cell as big as the table view.
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([[tableView indexPathForSelectedRow] isEqual:indexPath]) {
return tableView.frame.size.height;
}else{
return 44;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

I don't know why you would put a WebView in a cell.. I'm no expert but wouldn't the webview be recreated everytime you scroll?
You could use SVWebViewController and place the following code in your didselectrowatindexpath to present it modally.
SVModalWebViewController *webViewController = [[SVModalWebViewController alloc] initWithAddress:#"http://google.com"];
[self presentViewController:webViewController animated:YES completion:NULL];

Related

XCode iOS7 - Dynamic cell with UITextView like Reminder App

I need to create an application that have the same behavior as built-in Reminder App. There are some problems to create this:
Cells that have dynamic height and "grow" as the content of the UITextView
Select the touched cell
Refresh the height of the cell when the user has edited the content(on the fly)
I've already solved the dynamic height, using some trick.
The problem that remains is:
How to know which cell the user has selected if the cell is "fully" with the UITextView ?
Right Now i've used the method textViewDidBeginEditing to know the cell and scroll the UITableView to it:
- (void)textViewDidBeginEditing:(UITextView*)textView
{
MemoViewCell* cell = (MemoViewCell *)[self parentCellFor:textView];
if (cell)
{
NSIndexPath* indexPath = [self.tableView indexPathForCell:cell];
currentIndexPath = indexPath;
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}
}
- (UITableViewCell*)parentCellFor:(UIView*)view
{
if (!view)
return nil;
if ([view isMemberOfClass:[MemoViewCell class]])
return (UITableViewCell*)view;
return [self parentCellFor:view.superview];
}
How to refresh the cell height without lost the focus?
To this, i've used this method:
-(void)textViewDidChange:(UITextView *)textView
{
NSMutableDictionary *dataSourceItem = [self.model.dataSource objectAtIndex:currentIndexPath.row];
[dataSourceItem setObject:textView.text forKeyedSubscript:#"body"];
[self.model.dataSource setObject:dataSourceItem atIndexedSubscript:currentIndexPath.row];
[self.tableView beginUpdates];
[self.tableView endUpdates];
}
When the text change, i get the new text, i change it in the Model and i call beginUpdated and endUpdates.
Sure, this works, but all is extremly slow... do you have some idea to to this in a more elegant way and maybe... efficient?
If i call reloadRowsAtIndexPaths: i lost the focus on the cell.
First of all, if you use the delegate method - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; in your dynamic height trick, remember to implement this method along - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath;, it helps a lot in terms of performance.
Then to focus on your specific project, if I correctly understood your goal, what you really want is to update the layout of your table view only when your text view is modified. Currently you're updating it at each text change. You might consider using a condition inside -(void)textViewDidChange:(UITextView *)textView, such as :
if ([textView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height !=
textView.frame.size.height) {
[self.tableView beginUpdates];
[self.tableView endUpdates];
}
The condition given as example will work only if your existing cell layout respects the intrinsic content size of the text view, otherwise you will need to adapt the condition.

Scroll tableview cell directly beneath the navigation bar?

I have a tableview. On tap, a cell expands into a form, and I would like the cell to scroll up to start right below the navigation bar. How do I achieve this?
Here's the starting state:
Currently, after tap:
And here's what I want:
You need something like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Perform cell change height code here
// ...
// After that scroll to this cell
CGFloat sectionHeaderHeight = [self tableView:tableView heightForHeaderInSection:indexPath.section];
CGRect cellFrame = [tableView rectForRowAtIndexPath:indexPath];
[tableView setContentOffset:CGPointMake(0, cellFrame.origin.y - sectionHeaderHeight) animated:YES];
}
scrollToRowAtIndexPath... or scrollToNearestSelectedRow... both take "scroll position" arguments which allow the selection to go to the top, middle or bottom of the table view. That should give you what you want.

Stop UITableView from autoscrolling in iOS 8

I have a tableview that auto scrolls only in iOS 8 when I open a new view using [self.navigationController pushViewController:newViewController animated:YES];
Detailed Problem :
Now I'm going to give a detail of how I'm getting the problem, it's particularly in iOS 8. Take any tableview having around 50 entries, scroll it down to make the 10th entry at the top of the tableview. Then select any item on the tableview. Use the below method to push a view controller on row selection in tableview.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
// Open the New View
NewViewController *newVC = [[NewViewController alloc] init];
[self.navigationController pushViewController:newVC animated:YES];
}
Now you will find that on coming back from NewViewController to the previous ViewController, the tableview autoscrolls some distance. The tableview doesn't stay at the place of scroll it always changes its position automatically.
I had the same problem. I was using the new iOS 8 feature dynamic cell heigh. I was setting:
self.tableView.estimatedRowHeight = 50.0;
self.tableView.rowHeight = UITableViewAutomaticDimension;
The problem was that some cells were much higher than 50. Solution was to provide delegate method estimatedHeightForRowAtIndexPath and return values that are closer to the actual cell height. For example:
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 1) {
return 300.0;
}
return 50;
}
Another solution is to calculate cell heigh using systemLayoutSizeFittingSize inside cellForRowAtIndexPath and cache that value. Inside estimatedHeightForRowAtIndexPath supply cached values but return default if the cell height wasn't cached yet.
Just remove self.tableView.estimatedRowHeight = xxx; It works for me . The problem happens in iOS8 and does not appear in iOS10

Bringing my lables and buttons to front

I have a TableView in my main view. This table view hides some of lables and buttons in main view. After selecting a row from TableView table has to removeFromSuperView and buttons must be visible.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
txtsearch.text = selectedCell.textLabel.text;
[viewForautoCompleteTableView removeFromSuperview];
[self.view sendSubviewToBack:autocompleteTableView];
}
Still my lables are not visible, thank you.
It's better to hide and show the views rather than remove and add the views to achieve the effect you want.
Make sure all the views have been added properly to self.view. To hide them use:
viewForautoCompleteTableView.hidden = YES;
autocompleteTableView.hidden = YES;
To make them show you can use:
viewForautoCompleteTableView.hidden = NO;
autocompleteTableView.hidden = NO;

resizing a UITableViewCell frame issue

I am trying to resize my UITableViewCell's frame via:
[cell setFrame:CGRectMake(cell.frame.origin.x,
cell.frame.origin.y,
cell.frame.size.width,
cell.frame.size.height+25)];
however, it's not resizing after I do this... why is this?
This is weird as if I add a UIToolBar into the cell, it resizes but when I am adding a UIView, it doesn't:
[cell.contentView addSubview:sideSwipeView];
Here's the long and short of it:
Your cell width is determined by the width of the tableview it's in.
[EDIT: If it's a grouped table view, the cell is 20 - 60 pixels narrower than the tableview width, depending if you're using an iPhone, or an iPad.]
Your cell height is determined by the heightForRowAtIndexPath method.
If you're manually setting the cell's frame, it's going to be useless except when you're using a subclassed cell where you want to add subviews based on the cell's dimensions.
Even in this case, it's recommended to get the cell's frame from the tableview by using rectForRowAtIndexPath:(NSIndexPath*)indexPath method and then setting that frame as the cell's frame (after setting the frame's origin Y as 0).
I'm not quite sure about the UIToolBar, but your subview's frame won't change on changing the cell frame.
Maybe if you could tell us what you're trying to achieve, we can suggest a solution for you?
--------------------EDIT--------------------
So you need to dynamically add a subview to a cell on tapping it and resize it's height according to the new subview. This is gonna get hairy so here goes:
In your .h file declare:
BOOL subviewAdded;
In your .m file, in the init, do:
subviewAdded = NO;
Let's assume that you want the cell's height to be 50 without the subview and 100 with the subview. Accordingly, your heightForRow method should be:
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return (subviewAdded?100.0f:50.0f);
}
This means that initially since subviewAdded is NO, all your cells will have the smaller height.
Now, to add a subview to a cell on tapping it, and to change it's height dynamically, do this in your didSelectRow method:
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//Get the cell at this indexPath
UITableViewCell *thisCell = [tableView cellForRowAtIndexPath:indexPath];
if(subviewAdded)
{
subviewAdded = NO;
for(int i = 0; i < [thisCell.contentView.subviews count]; i++)
{
UIView *thisSubview = [thisCell.contentView.subviews objectAtIndex:i];
[thisSubview removeFromSuperview];
}
}
else
{
UIView *someView = [[UIView alloc] initWithFrame:someFrame];
[thisCell.contentView addSubview:someView];
[someView release];
subviewAdded = YES;
}
NSMutableArray *array = [NSMutableArray array];
[array addObject:indexPath];
[tableView reloadRowsAtIndexPaths:array
withRowAnimation:UITableViewRowAnimationFade];
}
So what's going to happen here is you're adding a subview to this cell you've tapped. Reloading this cell will call heightForRowAtIndexPath and do a nice little fade animation and change your tableview heights.
IMPORTANT: Ideally, you should maintain an array of NSNumbers with boolean values. The array size should be the same size as the number of tableview cells you have.
In heightForRow, you would then check against this array instead of using a single boolean for the whole tableView. This would ensure that you could have different heights for different cells.
That would look something like:
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath {
BOOL thisBool = (BOOL)[[booleanArray objectAtIndex:indexPath.row] boolValue];
return (thisBool?100.0f:50.0f);
}
I didn't post all that code here since it's implied and what I've posted should put you well on your way to doing the boolean array thing.
Anyway, there you are. I just tested this code myself so it works :)
If you want to increase the height of your cell based on some parameter eg. text, image,
you must implement the heightForRowAtIndexPath method of UITableViewDelegate in your code.
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath

Resources