我正在加载一个表格视图。我的表格视图的每个单元格都应根据数据具有不同的高度。在下面的代码中,我在表格的每一行中显示联系人。每个联系人可能有多个电话号码。我在每个单元格中显示联系人及其电话号码。因此,根据每个联系人的电话号码数量,单元格的高度会发生变化。我编写了以下代码来显示表格中的联系人。
- (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;
}
我找不到正确设置单元格高度的方法。我已经实施
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
委托方法并尝试了各种逻辑。但我无法正确设置行的高度。一个问题似乎是 indexpath.section 值,它在两种委托方法中都会发生变化。在这种情况下需要帮助。