我正在加载一个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值,它们在两个委托方法中都会发生变化。在这种情况下需要帮助。
答案 0 :(得分:3)
您可以使用: - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
根据单元格的部分/行设置行高。
例如:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
switch(indexPath.row){
case:0
return 100;
break;
case:1
return 200;
break;
}
}
等...
如果您有超过1个部分,请尝试此
- (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;
}
}
沙尼
答案 1 :(得分:0)
获取tableview单元格的标准高度。如果超过该高度,请使用标签hieight + tableview单元格高度(标准高度)
我认为你可以使用这个
- (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;
}