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];
Related
This is what I am doing so as to set the focus on the same point after UITableView beginUpdates :
CGPoint offset = tableMessageDetail.contentOffset;
// CGPoint textPoint = [self.view convertPoint:cell.txtReply.frame.origin toView:nil];
[tableMessageDetail beginUpdates];
[tableMessageDetail endUpdates];
[tableMessageDetail setContentOffset:offset animated:NO];
//[tableMessageDetail scrollRectToVisible:CGRectMake(cell.frame.origin.x, cell.frame.origin.y, cell.frame.size.width, cell.frame.size.height) animated:YES];
But the position after endUpdates, when the cell is too high, comes to the top of cell. I have tried with the commented approach as well, but that too is not perfect.
Giving a thought, hope this helps:
Remove all animations on the tableView's layer before setting the contentOffset.
[tableView.layer removeAllAnimations];
[tableView setContentOffset:offset animated:NO];
and then,
[tableView scrollToRowAtIndexPath:cell.indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
I have a UITextView in a UITableViewCell. I'm trying to dynamically size the textView.
-(void)textViewDidChange:(UITextView *)textView
{
CGFloat fixedWidth = textView.frame.size.width;
CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
CGRect newFrame = textView.frame;
newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
textView.frame = newFrame;
[self.myTableView beginUpdates];
[self.myTableView endUpdates];
}
After every char I input, the whole view jumps to the top, then jumps right back down to where I was at.
The solution for that, is probably to take away begin/endUpdates. When I do that, the textView sizes itself correctly, but the cell's height doesn't change.
Qustion:
How can I make the textView and the tableVIewCell dynamically size itself?
You need to reload only that particular cell in which the TextView is added.
Try this
[self.myTableView beginUpdates];
[self.myTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPathOfYourCell] withRowAnimation:UITableViewRowAnimationNone];
[self.myTableView endUpdates];
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 ;)
I am able to add a 'line separator' above my uitableviewcells manually (out-of-box iOS isn't cutting it for this). So I have created an array of UIViews, called _separatorLines, I add one UIView from each index to the uitableviewcells' contentView in my 'cellForRowAtIndexPath' with
UIView *v = [_separatorLines objectAtIndex:indexPath.row];
[cell.contentView addSubview:v];
however when i select a cell, i would like all other cells' line separators to turn red.
in 'didSelectRowAtIndexPath' I have:
{
for(unsigned int i = 0; i < _rowsInSection-1; i ++)
{
//make all other rows have a red content view
if(i != indexPath.row)
{
NSIndexPath *I = [NSIndexPath indexPathForRow:i inSection:1];
UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:I];
UIView *separatingLineView = [_separatorLines objectAtIndex:I.row+1];
separatingLineView.backgroundColor = [UIColor redColor];
[cell.contentView bringSubviewToFront:separatingLineView];
[separatingLineView setNeedsDisplay];
[cell setNeedsDisplay];
[cell.contentView setNeedsDisplay];
}
}
[self setNeedsDisplay];
Instead I do not see the contentView of the uitableviewcells changing.
For me I think the issue was the line
NSIndexPath *I = [NSIndexPath indexPathForRow:i inSection:1];
UITableView starts on section '0' (just like the rows start on '0').
And also because my UITableView is the delegate, I stopped using
[self tableView:tableView cellForRowAtIndexPath:I];
and replaced it with
[self cellForRowAtIndexPath:I];
Not sure if that had anything to do with it
This is my story:
I have a problem with the size of my IUTableViewCell. When I add several cell, the cell auto resizing.
any answer will be appreciated :)
That my code to resize:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPathInCellTable:(NSIndexPath *)indexPath {
CustomCell *cell = (CustomCell*) [self tableView:tableView
cellForRowAtIndexPath:indexPath];
CGSize size;
// SIZE HEIGHT TEXT
size = [cell.color.text sizeWithAttributes:
#{NSFontAttributeName:
[UIFont systemFontOfSize:12.0f]}];
// SIZE HEIGHT FOR CELL
CGRect frame = [cell frame];
frame.size.height += size.height;
[cell setFrame:frame];
// SIZE HEIGHT IMG
CGRect frame = [cell.img frame];
frame.size.height = 69;
frame.size.width = 69;
[cell.img setFrame:frame];
if (indexPath.row == 0) [self setHeightTableView:0];
_tableHeightConstraint.constant += cell.frame.size.height;
return cell.frame.size.height;
}
There some screenshot :
the first time i add a cell everything is fine
the same for the second cell everything is fine
And there the problem comes
You call CustomCell
*cell = (CustomCell*) [self tableView:tableView
cellForRowAtIndexPath:indexPath];
to get the a cell, which is wrong, because there is no cell created yet (so its nil). tableView:heightForCell:atIndexPath: get called before the cell was created. The best solution would be to have a module abject to save the height needed for your cell or make some similar calculations
Use a prototype cell to get the sizing instead of using tableView:cellForRowAtIndexPath:
http://www.samrayner.com/posts/dynamic-tableview-cells/