1

I want to expand a cell in my table view when I select it. Initially it should look like this:

And on expanding, it should give more detail like this:

This is how I'm creating the cell initially:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * MyIdentifier = @"MyIdentifier";
    UITableViewCell * cell = [self.table dequeueReusableCellWithIdentifier:MyIdentifier];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:MyIdentifier] autorelease];
        cell.textLabel.frame = CGRectMake(0, 0, cell.textLabel.frame.size.width, 
        cell.textLabel.frame.size.height);
 }
 id key = [[articles allKeys] objectAtIndex:indexPath.section];
 cell.textLabel.text =@"TitleTitleTitle";        
 cell.detailTextLabel.text=@"DeatailDeatailDeatailDeatailDeatailDeatailDeatailDeatailDeatai";
 return cell;
}
jscs
  • 63,694
  • 13
  • 151
  • 195
user928622
  • 185
  • 1
  • 12

1 Answers1

1

I published a sample code at github

Your tabelView:cellForRowAtIndexPath: could look like this.

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * MyIdentifier = @"MyIdentifier";
    UITableViewCell * cell = [self.table dequeueReusableCellWithIdentifier:MyIdentifier];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:MyIdentifier] autorelease];
        cell.textLabel.frame = CGRectMake(0, 0, cell.textLabel.frame.size.width, 
                                          cell.textLabel.frame.size.height);
    }
    id key = [[articles allKeys] objectAtIndex:indexPath.section];
    cell.textLabel.text =@"TitleTitleTitle";        
    cell.detailTextLabel.text=@"DeatailDeatailDeatailDeatailDeatailDeatailDeatailDeatailDeatai";


    //SEE MY NOTE BELOW
    if([indexPath isEqual:selectedIndexPath]){
        cell.textLabel.numberOfLines=0;
        cell.detailTextLabel.numberOfLines=0;

        CGSize constraintSize;
        constraintSize.width = cell.textLabel.frame.size.width;
        constraintSize.height = MAXFLOAT;
        cell.textLabel.frame = [cell.textLabel.text sizeWithFont:cell.textLabel.font constrainedToSize:constraintSize];

        constraintSize.width = cell.detailTextLabel.frame.size.width;
        constraintSize.height = MAXFLOAT;
        cell.detailTextLabel.frame = [cell.detailTextLabel.text sizeWithFont:cell.detailTextLabel.font constrainedToSize:constraintSize];
    } else {
        cell.textLabel.numberOfLines=1;
        cell.detailTextLabel.numberOfLines=1;
    }
    return cell;
}

BUT
You will also need the height of the cell by dynamic in tableView:heightForRowAtIndexPath:, so you should refactor the sizing parts into a helper methods, that writes the sizes of the 2 labels and the height of the cell to ivars, call the method from tableView:heightForRowAtIndexPath:, use the cell height there and the labels sizes in tabelView:cellForRowAtIndexPath:

Zoe
  • 27,060
  • 21
  • 118
  • 148
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
  • hi, its working and getting the effect what i want. thanks for it.but i am not able to add detail text to cell when it expands.how should i show it on expansion? – user928622 Dec 01 '11 at 09:10
  • Are u using a custom cell or a certain per-defined style? – vikingosegundo Dec 01 '11 at 11:34
  • please post the method `tableView:cellForRowAtIndexPath:` in your original question. – vikingosegundo Dec 01 '11 at 11:55
  • see my edit (PS: please leave a comment after altering your question/answer, as otherwise people won't be notified. also post proper formatted code with proper intention) – vikingosegundo Dec 01 '11 at 12:58
  • tabelView:cellForRowAtIndexPath is not get called when row expands.I get called when row get contract.so unable to show as per requirement.:( – user928622 Dec 05 '11 at 10:04
  • see my `tableView:didSelectRowAtIndexPath:` implementation. It updates the tableview -> `tableView:cellForRowAtIndexPath:` gets called. – vikingosegundo Dec 05 '11 at 12:10
  • thanks for help. i called [table reloadData] in didSelectRowAtIndexPath method and its work for me.thanku – user928622 Dec 07 '11 at 05:25