Domanda

Sto caricando un Tableview. Ogni cellula del mio Tableview dovrebbe avere altezze variabili a seconda dei dati. Nel codice di seguito, sto visualizzazione dei contatti in ogni riga della tabella. Ogni contatti possono avere più numeri di telefono. Io sono la visualizzazione dei contatti con i loro numeri di telefono in ogni cellule. Quindi, a seconda del numero di numeri di telefono di ogni contatto, l'altezza della cella cambierà. Ho scritto il seguente codice per visualizzare i contatti nella tabella.

- (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 trovare un modo per impostare correttamente l'altezza delle celle. Ho implementato

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

metodo delegato e provato varie logiche. Ma io non sono in grado di impostare correttamente l'altezza della riga. Un problema sembra essere valori indexpath.section, che tende a cambiare in entrambi i metodi delegato. Hai bisogno di aiuto in questo caso.

È stato utile?

Soluzione

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

Altri suggerimenti

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;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top