1

STEP 1 - this is working fine

I have UITableView that loads custom UITableViewCells

STEP 2 - this is kinda works

I change the UITableViewCell height so that all the data contained in the cell within a UITextView is visible

I manage to get the UItextView data and resize it by using the following code:

UITextView *dummy = [[UITextView alloc] init];
dummy.font = [UIFont systemFontOfSize:14];
dummy.text = cell.textView.text;

CGSize newSize = [dummy sizeThatFits:CGSizeMake(270.0f, 500.0f)];

//get the textView frame and change it's size
CGRect newFrame = cell.textView.frame;
newFrame.size = CGSizeMake(270.0f, fmaxf(newSize.height, 60));
cell.textView.frame = newFrame;

//resize the cell view I add +95 because my cell has borders and other stuff...
CGRect cellFrame = CGRectMake(0, 0, 320, newFrame.size.height+95);
cell.cellView.frame = cellFrame;

I then manage to set the UITableViewCell height using the delegate function heightForRowAtIndexPath:

Now when I run the code and scroll up and down the table cells the behavious isn't always as expected... i.e the cell size isn't always the right size, but if I scroll up and down it sometimes loads up the right size again. I am thinking that perhaps the dequeueReusableCellWithIdentifier is the issue

cellIdentifier = @"tableCell";
ctTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

Since I am recycling cells of different size with the same identifier...

Can anybody give me some guidelines on how best to solve this issue?

Thanks!!!

Screenshot1 of when the cell is first loaded

screenshot 2 after scrolling notice how the top cell is rectified

Tim Vks
  • 51
  • 8

2 Answers2

0

have you implemented following method ?

  • (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

https://developer.apple.com/library/ios/documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UITableViewDelegate/tableView:heightForRowAtIndexPath:

you should set height for each row , if they are different from each other

ogres
  • 3,660
  • 1
  • 17
  • 16
0

I would start by adding UITextView dynamically inside cellForRowAtIndexPath method. assumptions(data array contains the content to be displayed inside cell) // Defines

#define CELL_TEXTVIEW_WIDTH = 320        
#define CELL_TEXTVIEW_HEIGHT = 9999  
#define PADDING = 5

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

 static NSString *CellIdentifier = @"tableCell";

ctTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

if(cell == nil){

cell =  [[tableView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

}

 NSString *_data = [data objectAtIndex:indexPath.row];

CGSize _data_contentsize  = [_data sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(CELL_TEXTVIEW_WIDTH, CELL_TEXTVIEW_HEIGHT) lineBreakMode:NSLineBreakModeWordWrap];

UITextView *dummy=[[UITextView alloc] initWithFrame:CGRectMake(5, 5, 290, _data_contentsize +(PADDING * 2))];

// add Font and text color for your textview here

[cell.contentView addSubview:dummy];

return cell;

}

After this calculate the height of the cell under heightForRowAtIndexPath method.

Roshan
  • 521
  • 4
  • 16
  • you can add the textView styles in your ctTableViewCell class awakeFromNib Method – Roshan Oct 24 '13 at 18:05
  • forgot to add the view dummy to the cell content view, will update my answer – Roshan Oct 24 '13 at 18:20
  • Ok guys here is the deal - I tried the above solution and it worked, then I was surprised that doing something dynamically would work and not through the XIB so I reverted back to my solution by recreating a new UITextView and this time it worked. I think this is a xcode or iOS bug... sometimes you just don't understand what is going on. Also in this guest to find a solution I learn't a neat trick that can help debug those I don't get it behaviours when stuff works on phone but not on simulator etc... clean the project with cmd + shift + k – Tim Vks Oct 24 '13 at 19:18