I have implemented SKSTableView and I used this command:
[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
when I press a cell that is expandable, and it goes on top. Now I'd like that when I scroll the expanded cell which went to top to stay fixed there until it's closed. Can I do that?
In UITableView you have the UIScrollView methods. So you can do something like this:
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
UITableViewCell *cell =[self.myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
CGRect cellFrame = [cell convertRect:cell.frame toView:self.view];
if (self.isStickToTop) {
self.cell.frame = CGRectMake(0, 0, self.cellFrame.width, cellFrame.size.height);
}
else {
self.cell.frame = CGRectMake(0, scrollView.contentOffset.y, self.cellFrame.width, cellFrame.size.height);
}
}
The isStickToTop is your maintenance BOOLvariable that will indicate if the cell stick to top or not. Don't say u don't know ;)
Related
I have a UITextView in custom UITableViewCell. On expanding the UITextView, I want the subviews below the UITextView to fall as per its height and also increase the row height. For this I am doing :
- (void)textViewDidChange:(UITextView *)textView
{
int numLines = textView.contentSize.height/textView.font.lineHeight;
if(numLines < 6)
{
CGRect frame = textView.frame;
frame.size.height = textView.contentSize.height;
textView.frame = frame;
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:(int)textView.tag inSection:0];
MessageDetailCell *cell = [tableMessageDetail cellForRowAtIndexPath:indexPath];
cell.txtReplyHeightConstraint.constant = textView.frame.size.height;
[cell updateConstraintsIfNeeded];
[cell setNeedsLayout];
[cell layoutIfNeeded];
[tableMessageDetail beginUpdates];
[tableMessageDetail endUpdates];
}
}
Everything works fine except that beginUpdates endUpdates makes the UITableViewCell flash. So far as cell content is concerned, I have images as well as text displayed in it. The cell flashed only in case of images.
You have to call beginUpdates before you change the frame.
[tableMessageDetail beginUpdates];
// change cell attributes
[tableMessageDetail endUpdates];
I want that the selected cell will always be at the top most position of tableView.
I can to it via:
[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
but my problem is, when the number of cells, for example, is just 3. When I select cell number 3, it just stays there. Was that the normal behaviour? If yes, then, can you suggest something so that I can achieve my goal? Thanks!
U can use contentInset and setContentOffset property of tableview for example
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat height = 44.0f;//cell height
tableView.contentInset = UIEdgeInsetsMake(0, 0, height * indexPath.row, 0);
//[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES]; //commenting u are setting it by using setContentOffset so dont use this
[tableView setContentOffset:CGPointMake(0, (indexPath.row * height) )animated:YES]; //set the selected cell to top
}
hope this helps u .. :)
You can use this code in the didSelectRowAtIndexPath delegate of table view
CGFloat height = 44.0f;//cell height
tableView.contentInset = UIEdgeInsetsMake(0, 0, height * indexPath.row, 0);
[tableView setContentOffset:CGPointMake(0, (indexPath.row * height) )animated:YES];
you can set UITableView content offset using
[tableView setContentOffset:offsetPoint animated:YES];
or
[tableView setContentOffset:offsetPoint];
and get your selected cell position by
CGPoint offsetPoint = [tableView cellForRowAtIndexPath:indexPath].frame.origin
then you can move cell to your desired offset as bellow
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
float topOffset = 64; //in case of navigationbar
CGPoint offsetPoint = CGPointMake(0, cell.frame.origin.y - topOffset);
[tableView setContentOffset:offsetPoint animated:YES];
}
I am trying to scroll my UITableViewCell up one UITableViewCell position when the selected cell reaches the last position in the UITableView
I have the logic correct for identifying the last visible UItableViewCell of the UITableView. However the code to make the UITableView scroll up one position is not working. This is the code I have written.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
cellSelectedBool = YES;
cell = (CustomFinishingCell *)[tableView cellForRowAtIndexPath:indexPath]; // error
[cell.widthTexField becomeFirstResponder];
// Gets an array of current visible cells in UITableView
NSArray *visibleCells = [tableView visibleCells];
NSLog(#"%#", visibleCells);
// get indexpath of last cell
UITableViewCell *lastCell = [visibleCells lastObject];
NSIndexPath *lastCellIndex = [finishingTableView indexPathForCell:lastCell];
// perform scroll when last visible cell is selected
if (indexPath.row == lastCellIndex.row) {
NSLog(#"BIGGER");
int cellHeight = 44;
[UIView animateWithDuration:.25 animations:^{
[finishingTableView setContentOffset:CGPointMake(finishingTableView.contentOffset.x,finishingTableView.contentOffset.y + cellHeight) animated:YES];
}];
}
you should use:
NSIndexPath *rowToSelect; // assume this exists and is set properly
UITableView *myTableView; // assume this exists
[myTableView selectRowAtIndexPath:rowToSelect animated:YES scrollPosition:UITableViewScrollPositionNone];
[myTableView scrollToRowAtIndexPath:rowToSelect atScrollPosition:UITableViewScrollPositionNone animated:YES];
if (indexPath.row == lastCellIndex.row) {
[finishingTableView scrollToRowAtIndexPath:indexPathatScrollPosition:
UITableViewScrollPositionNone animated:YES];
}
I can get a reference to the cell quite easily
cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
But I need to get a reference to the little handle on the righthand side of the cell when in edit/reorder mode. How can I do this?
you can find it with this code, but the reorder-control is private-api
for (UIView *view in cell.subviews) {
if ([NSStringFromClass([view class]) isEqualToString:#"UITableViewCellReorderControl"]) {
// 'view' is the reorder control
}
}
You need to use the editingAccessoryView property on your cell :
cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
UIView *editingAccessory = cell.editingAccessoryView;
Currently I have a UITableView with custom cells and in each cell there is a UITextField, the problem is that sometimes the UITextField is covered by the UIKeyboard.
So right now I have the Y coordinate for the UIKeyboard and my UITableView is functioning properly with the cells.
So pretty much how can I use that Y coordinate (float), in order to scroll my UITableView to that Y coordinate plus the height of the cell in order to get it right above my UIKeyboard?
Also when I the keyboard hides, how would I reset the UITableView to its normal position that it was in?
Any advice would be appreciated!
Thanks!
CustomCell *cell = (CustomCell*)[[thetextField superview] superview];
CGRect cellRect = [cell convertRect:cell.frame toView:self.view];
float bottomCell = cellRect.origin.y - cellRect.size.height;
if (bottomCell >= keyboardY) {
NSIndexPath *indexPath = [thetableView indexPathForCell:cell];
[thetableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
One or the other of UITableView's
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;
- (void)scrollToNearestSelectedRowAtScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;
I'm guessing that control isn't getting into the if statement. Consider skipping the y-coordinate check in favor of just scrolling the table view regardless of where it is.
CustomCell *cell = (CustomCell *)[[theTextField superview] superview];
NSIndexPath *indexPath = [theTableView indexPathForCell:cell];
[theTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
OK, if you're sure it's getting called, then make sure your indexPath isn't nil, which may happen if cell isn't in the tableView or if it's not a cell.
UITableView adjust itself whenever the keyboard is displayed.
However, it won't do it if you didn't set the UITableViewCellSelectionStyle of your custom cell to UITableViewCellSelectionStyleNone.
i solved by using the below code.
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];