Question

I am loading a tableview. Each cell of my tableview should have varying heights depending on the data. In the code below, I am displaying contacts in each row of the table. Each contacts may have multiple phone numbers. I am displaying the contacts with their phone numbers in each cells. So depending on the number of phone numbers of each contact, the height of the cell will change. I have written the following code to display the contacts in the table.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:    (NSIndexPath *)indexPath
{
NSArray *contactsInSection = [sectionsArray objectAtIndex:indexPath.section];
ContactsHelper *contact = [contactsInSection objectAtIndex:indexPath.row];
static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    UIButton *checkBox = [[UIButton alloc] init];
    checkBox.tag = contact.contactID;
    [cell.contentView addSubview:checkBox];
    [checkBox setFrame:CGRectMake(6,14,20,20)];
    [checkBox release];

    UILabel *CellTextlabel = [[UILabel alloc] init];
    CellTextlabel.tag = 222;
    [CellTextlabel setFrame:CGRectMake(40, 5, 200, 20)];
    [cell.contentView addSubview:CellTextlabel];
    [CellTextlabel release];

    UILabel *detailcellTextlabel = [[UILabel alloc] init];
    detailcellTextlabel.tag = 333;
    [detailcellTextlabel setFrame:CGRectMake(40, 24, 200, 20)];
    detailcellTextlabel.font = [UIFont boldSystemFontOfSize:12];
    detailcellTextlabel.textColor = [UIColor grayColor];

    [[cell contentView] addSubview:detailcellTextlabel];
    [detailcellTextlabel release];
}
ABAddressBookRef addressbook = ABAddressBookCreate();
ABRecordRef person = ABAddressBookGetPersonWithRecordID(addressbook,contact.contactID);
UIButton *checkBox = (UIButton *)[cell.contentView viewWithTag:contact.contactID];

if(isActivDeactivButton)
{
    [checkBox setImage:[UIImage imageNamed:@"disabled_checkbox.png"] forState:UIControlStateNormal]; 
   }
else{
   [checkBox setImage:[UIImage imageNamed:@"selected_checkbox.png"] forState:UIControlStateNormal];
 }

[checkBox addTarget:self action:@selector(checkBoxSelected:) forControlEvents: UIControlEventTouchUpInside];

UILabel *editCellTextlabel = (UILabel *)[cell.contentView viewWithTag:222];
editCellTextlabel.font = [UIFont boldSystemFontOfSize:18];

UILabel *detailcellTextlabel = (UILabel *)[cell.contentView viewWithTag:333];


if (sendSMS) {
    NSMutableArray *phoneNumberEntries = [[[NSMutableArray alloc] init] autorelease];
        editCellTextlabel.text = contact.lastName;
    ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);
    numberOfLines = ABMultiValueGetCount(phoneNumbers);


    for (int i = 0; i < ABMultiValueGetCount(phoneNumbers)  ; i++) {
        CFStringRef phoneNoEntries = ABMultiValueCopyValueAtIndex(phoneNumbers, i);
        NSString *phoneNumber = (NSString *) phoneNoEntries;
        [phoneNumberEntries addObject:phoneNumber];  

     //   NSLog(@"email id %@", detailcellTextlabel.text);
        CFRelease(phoneNoEntries);
    }
     detailcellTextlabel.numberOfLines = ABMultiValueGetCount(phoneNumbers);
   [detailcellTextlabel setFrame:CGRectMake(40, 24, 200, 20 + 20 * [phoneNumberEntries count])];
   detailcellTextlabel.lineBreakMode = UILineBreakModeWordWrap;
    detailcellTextlabel.lineBreakMode = UILineBreakModeWordWrap;
    detailcellTextlabel.numberOfLines = numberOfLines;
    detailcellTextlabel.text = [phoneNumberEntries componentsJoinedByString:@"\n"];

   // NSLog(@"email id %@", detailcellTextlabel.text);
    CFRelease(phoneNumbers);

}
CFRelease(addressbook);
return cell;
}

I cant find a way to set the height of the cells correctly. I have implemented

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

delegate method and tried various logics. But I am not able to set the height of the row correctly. One problem seems to be indexpath.section values, which tends to change in both delegate methods. Need help in this case.

Was it helpful?

Solution

You can use: - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

And set the row height according to the section/row of the cell.

For example:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
 switch(indexPath.row){
   case:0
    return 100;
   break;

   case:1
    return 200;
   break;
  }
}

etc....

If you have more then 1 section try this

     - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{   

      switch(indexPath.section){
        case:0
            switch(indexPath.row){
                case:0
                    return 100;
                    break;

                case:1
                    return 200;
                    break;
            }
            break;

        case:1
            switch(indexPath.row){
                case:0
                    return 100;
                    break;

                case:1
                    return 200;
                    break;
            }

            break;
    }
}

Shani

OTHER TIPS

Take a standard height of the tableview cell.For more than that height take the label hieight + tableview cell height(your standard height)

I think you can use this

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

int height = 40 //standard height 
if (tableview cell label height >40){
  height = tableview cell label height; 
 }
return height;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top