I'm having trouble trying to scroll up a tableView when the user start editing a UITextField displayed in one of the cells.
My problem is that I can't find a way to know the position of the UITextField in the view. If I try to do activeTextField.frame.origin.y, I get the position of the TextField inside the cell, but not inside the tableView.
The way I build the view is the following :
I've got a TableViewController, which creates cells using a custom class that initializes the UITextField. Then my TableViewController set the UITextField delegate to itself, so I can trigger the textFieldDidBeginEditing event. But if I try to NSLog the y origin of the TextField, I always get the same value for each UITextField, which is its position in the cell.
Any idea on how I can solve this?
It's always confusing with these coordinate system changes ...
This should work to get you the point in the table view coordinate system:
CGPoint textFieldCenter = textField.center;
CGPoint pointInTableView = [tableView convertPoint:textFieldCenter fromView:textField.superview];
If you want to have it in a different view, you should replace tableView with the view you want to have the coordinate in.
And if you want to get the cell in the table the textField was in:
NSIndexPath* path = [tableView indexPathForRowAtPoint:pointInTableView];
UITableViewCell* cell = [tableView cellForRowAtIndexPath:path];
To get the position of your textfield should use the UIView method convertRect:toView:
convertRect:toView: Converts a rectangle from the receiver’s
coordinate system to that of another view.
(CGRect)convertRect:(CGRect)rect toView:(UIView *)view
example :
CGRect textFieldFrame = [self.tableview convertRect:yourTextField.frame toView:self.view];
this should help you out
This is simple and easy way of doing what you want
Yes, try using this method :
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated
When you prompt the keyboard, scroll the tableView to the selected cell it should bring the cell to the top of the screen.
try this
CGPoint locationinView=[cellView convertPoint:location fromView:tableView];
here location is the point of textField in cellView.
Related
I want to get the position of UITableVIewCell relative to the screen, not relative to tableview. So, if I scroll the tableview, the position is updated.
You should use two steps to achieve this:
Get the CGRect information for a selected cell/header or footer by using one of these methods of UITableView:
- (CGRect)rectForHeaderInSection:(NSInteger)section;
- (CGRect)rectForFooterInSection:(NSInteger)section;
- (CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPath;
2. Convert the CGRect to table's container view by using UITableView's convertRect method. Eg. to get the current pos of the first header you can use this code:
CGRect rect = [mytable convertRect:[mytable rectForHeaderInSection:0] toView:[mytable superview]];
If you want these above dinamically you might want to include this code in UITableView's delegate method:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView_ {.....
One example what I've done using similar techniques is at github - how to make table header row fixed:
https://github.com/codedad/SO_Fixed_TableHeader_iOS
Hope it helps you!
You need to get position from child to parent:
float x = cell.frame.origin.x;
float y = cell.frame.origin.y;
UIView* parent = cell.parent;
while(parent != nil)
{
x += parent.frame.origin.x;
y += parent.frame.origin.y;
parent = parent.parent;
}
Use the -[UITableView rectForRowAtIndexPath:] method. Keep in mind that if you attempt to get this when the cell is off screen you will get unexpected results. You can then convert it to your view controller's view (which i think is what you mean by screen) with the -[UIView convertRect:toView:] method.
Ref: https://developer.apple.com/library/ios/documentation/uikit/reference/UITableView_Class/Reference/Reference.html#jumpTo_49
https://developer.apple.com/library/ios/documentation/uikit/reference/uiview_class/UIView/UIView.html#jumpTo_65
I have UICollectionView with UICollectionViewCell created in storyboard. I customized cell by adding to it UIImage. After user taps cell I want to show image from particular cell increased by size. To implement that I added UIImageView to view, situated it on top and made:
[previewImage setAlpha:0]; and [previewImage setHidden:YES]; in -viewDidLoad. At -didSelectItemAtIndexPath: I want to set value of image in this UIImageView to the appropriate in touched cell. I tried
previewImage.image = [(CustomCell *)[collectionView cellForItemAtIndexPath:indexPath] image].image;
but it don't works. In logs I see that previewImage.image returns null. So how can I retrieve image value from cell in right way?
Solved by setting previewImage.image this way:
UIImage *preImage = [(CustomCell *)[collectionView cellForItemAtIndexPath:indexPath] image].image;
[previewView setImage:preImage];
hey here you just use the bellow delegate method of UICollectionView
#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
// TODO: Select Item
}
For More info see this UICollectionview-example
i hope this help you..
I want to select a table view row when the person reaches the center of the table view. Basically whenever the table reaches a point y I want the cell at that point to be selected.
I imagine it is similar to uiPickerView.
In your UITableViewController you should implement -scrollViewDidScroll: like this:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (prevCell != nil)
self.prevCell.selected = NO;
CGPoint offset = self.tableView.contentOffset;
CGPoint point = offset;
point.y += self.tableView.center.y;
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:point];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
cell.selected = YES;
self.prevCell = cell;
}
This is tested and works. prevCell is a property of type UITableViewCell.
You must get the current offset of the scroll view for the table view and add this to the position of the cell you want (in this example, I take the cell in the center of the table view).
Then, using -indexPathForRowAtPoint: we grab the index path for the cell at the point and using -cellForRowAtIndexPath: we get the cell for the index path. Set it to selected and store it for later use (to deselect it on the next scroll).
have you tried
[[yourTableView cellForRowAtIndexPath:[yourTableView indexPathForRowAtPoint:wantPoint]] setSelected:true]
?
I think it can be achieved by getting all visible indexpaths, then data on visible indexpaths. The best answer you may see your own cz you know your problem well. Go through this class reference:
https://developer.apple.com/library/ios/#documentation/uikit/reference/UITableView_Class/Reference/Reference.html
I am using Xcode's SplitView template.
I have placed a bunch of text fields on the 'detail' side of the view however I forgot about the keyboard taking up half the screen.
How do I move this detail view up when the keyboard comes onto the screen?
I used this solution from the always-useful Cocoa With Love when I could not use a UIScrollView.
If you want to add a UIScrollView you your hierarchy, it gets even easier. Just insert the UIScrollview in the hierarchy above the text fields and use this code when the user taps the text item to begin editing:
UIScrollView* v = (UIScrollView*) self.view ;
CGRect rc = [textField bounds];
rc = [textField convertRect:rc toView:v];
rc.origin.x = 0 ;
rc.origin.y -= 60 ;
rc.size.height = 400;
[self.scroll scrollRectToVisible:rc animated:YES];
Good Luck!
I had that problem once. My app used a table view to show the fields, so the solution I came up with is to listen the textViewDidBeginEditing to perform a scrollToRowAtIndexPath on the table view:
- (void)textViewDidBeginEditing:(UITextView *)textView{
UITableViewCell *cell = (UITableViewCell*) [[textView superview] superview];
[self.myTableView scrollToRowAtIndexPath:[self.myTableView indexPathForCell:cell] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}
If you don't have a table view but a scroll view I think you could take same similar approach but telling the scrollview to scroll to the section you need just as #MystikSpiral told.
My goal is to have the UITableViewCells fade in/out when they are approaching the bounds of the UITableView and about to be covered/revealed.
The approach I have been trying is to get the coordinates of the UITableViewCell during a scroll event. The problem is that every cell seems to be at 0,0. I have tried converting the coordinates to the parent table and view, but they still come out at 0,0.
So in general, if anyone knows a way to get the coordinates, or of a better way to go about fading UITableViewCells in and out based on their position, I would greatly appreciate any advice you may have.
Thanks for your time,
Joel
The first step is to use
CGRect rectInTableView = [tableView rectForRowAtIndexPath:indexPath];
which will report the CGRect of a cell within the tableView. However, this value does not change as the tableView scrolls. It is the position relative to the first row of the table (and not the first visible row). To get the position of the cell on the screen you have to convert it to the superviews coordinate system using
CGRect rect = [tableView convertRect:rectInTableView toView:[tableView superview]];
So the following line does it all
CGRect rect = [tableView convertRect:[tableView rectForRowAtIndexPath:indexPath] toView:[tableView superview]];
Swift 4
// Consider the indexPath at row 1, section 0.
let rectWithinTableView : CGRect = self.tableView.rectForRow(at: IndexPath(row: 1, section: 0))
let rectWithSuperViewCoordinates : CGRect = self.convert(rect: rectWithinTableView, to: self.tableView.superview)
Why not an overlay with a partially transparent gradient PNG in a UIImageView that's less translucent at the top and bottom?
Messing with cell drawing in table scrolling is going to take a big performance hit.
You can call
- (CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPath;
to get the rect of any given cell. This will contain it's coordinates in the origin struct within the rect.
I suspect the cells are held within 'cell sized' subViews of the UITableView so you are seeing a frame relative to that view.
I don't have an actual an answer for you but, I would suggest checking out UIScrollView's delegate class: UIScrollViewDelegate. It responds to – scrollViewDidScroll: and you can manually work out your offset from that. UIScrollView is a superclass of UITableView.
You can convert points (such as your origin) to another view's co-ordinates using UIView's - (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view method.