Joetjah
Joetjah

Reputation: 6132

Dynamic textview in dynamic custom cells

I'm having some trouble here.

I've got a tableview, with 7 cells, all of them are made using .xib-files. Some of them contain labels, two contain a textview.

The current situation is that all the labels are shown correctly. The height of the cells are the same as the .xib-file states.

The problem is, the textviews are acting up strange. It might have to do something with the fact that I want them to be dynamic. Sometimes, it gives a long story in that textview. Sometimes, only one word. Therefore, the textview should be dynamic, and so should the cell's height be.

The code looks like this:

DetLocDescrCell *locDCel = (DetLocDescrCell *)[tabelView dequeueReusableCellWithIdentifier:nil];
if (locDCel == nil) {
    [[NSBundle mainBundle] loadNibNamed:@"DetLocDescrCell" owner:self options:nil];
    locDCel = detLocDescrCell;
}
[locDCel.locDescrLabel setText:[geselecteerdItem objectForKey:@"LocationDescription"]];
CGRect descrFrame;
descrFrame = locDCel.locDescrLabel.frame;
descrFrame.size.height = [locDCel.locDescrLabel contentSize].height;
locDCel.locDescrLabel.frame = descrFrame;
descrFrame.size.height = descrFrame.size.height + 10;
locDCel.frame = descrFrame;
[listCells addObject:locDCel];

This code creates a custom cell from the class DetLocDescrCell, loads the data, check whenever the height should be adapted or not, adapts the height, then adds the cell to the list of cells. The list of cells is used again in cellForRowAtIndexPath and heightForRowAtIndexPath. But, the height isn't adapted, and the textviews aren't getting the correct text. Does anyone have any idea?

Upvotes: 0

Views: 950

Answers (4)

Karthikeyan
Karthikeyan

Reputation: 1790

i m not sure whether this is the right way , but i done this way,

The situation is custom cell height & heightforrowatindex cell's height should be same so i tried

calculate the height of textview content in heightforrowatindexpath

if(indexpath.row == 3) // containing textview 
{
   CGSize textSize = [txtviewstring sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(190, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap]; 
}

then in your cellforrowatindexpath - calculate the same height & initalize height to custom cell textview's height cell.txtView.frame = CGRectMake (10,10,190,txtsize.height).

then both u r txtview height & cell height will be equal

Hope this idea helps!

Upvotes: 2

Anil Kumar
Anil Kumar

Reputation: 654

I'm not sure about your question. If it is to increase the size of textview on the cell, it is not possible because the cell of the tableview was already created and loaded. It wont go back to the delegate method tableview:heightForRowAtIndexPath:indexPath because the indexpath.row will increase when the tableview loads

Check the tableview delegate reference

Upvotes: 0

Praveen S
Praveen S

Reputation: 10393

You can use the delegate method:

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

to set the height before the cell is drawn on the screen.

Upvotes: 0

Related Questions