gpt4 book ai didi

iphone - 为表格 View 中的单元格设置可变高度

转载 作者:行者123 更新时间:2023-11-28 20:40:18 26 4
gpt4 key购买 nike

我正在加载一个表格 View 。我的 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 

委托(delegate)方法并尝试了各种逻辑。但是我无法正确设置行高。一个问题似乎是 indexpath.section 值,它往往会在两种委托(delegate)方法中发生变化。在这种情况下需要帮助。

最佳答案

您可以使用:- (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;
}
}

沙尼

关于iphone - 为表格 View 中的单元格设置可变高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8819269/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com