我正在加載tableview。根據數據,我的tableview的每個單元格都應該有不同的高度。在下面的代碼中,我在表格的每一行中顯示聯繫人。每個聯繫人可能有多個電話號碼。我在每個單元格中顯示他們的電話號碼的聯繫人。因此,根據每個聯繫人的電話號碼數量,單元格的高度會發生變化。我寫了下面的代碼來顯示錶中的聯繫人。在tableview中爲單元格設置變量高度
- (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值,這兩個代理方法都會改變。在這種情況下需要幫助。
我有數百個聯繫人在我的tableview顯示。切換案例是否正確? – 2012-01-11 12:37:10
您不必切換。你可以用if語句檢查單元格的所有其他屬性。或使用範圍等。 – shannoga 2012-01-11 12:44:17