0
我有分組表格視圖,我要在其中顯示聯繫人詳細信息。但細節是動態長度,所以我需要細胞的動態高度。它適用於某些部分,但不適用於重新部分。這裏是我的代碼不顯示分組表格視圖中的動態表格單元格高度iphone
#define PHONE_DETAIL_SECTION 0
#define EMAIL_DETAIL_SECION 1
#define URL_DETAIL_SECTION 2
#define ADDRESS_DETAIL_SECTION 3
#define BIRTHDAY_DETAIL_SECTION 4
#define NOTE_DETAIL_SECTION 5
#define CONSTRAINT_WIDTH 320.0f
#define FONT_SIZE 17.0f
#define MARGIN_SIZE 20.0f
#define NO_OF_SECTION 6
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return NO_OF_SECTION;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
switch (section) {
case PHONE_DETAIL_SECTION:
return [APP_DELGATE.phonenumberDetailLabelArray count];
case EMAIL_DETAIL_SECION:
return [APP_DELGATE.emailLabelDetailArray count];
case URL_DETAIL_SECTION:
return [APP_DELGATE.urlLabelDetailArray count];
case ADDRESS_DETAIL_SECTION:
return [APP_DELGATE.addressDetailLabelArray count];
case BIRTHDAY_DETAIL_SECTION:
if(birthdateString)
return 1;
else
return 0;
case NOTE_DETAIL_SECTION:
if(noteString)
return 1;
else
return 0;
default:
break;
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"section:::%d",indexPath.section);
static NSString *CellIdentifier = @"Cell";
// DetailCell is subclass of UITableCell which
// contains only 2 labels:- lblName & lblValue
DetailCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *cellArray=[[NSBundle mainBundle] loadNibNamed:@"DetailCell" owner:self options:nil];
cell=[cellArray objectAtIndex:0];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
if (indexPath.section==PHONE_DETAIL_SECTION) {
cell.lblName.text=(NSString*)[APP_DELGATE.phonenumberDetailLabelArray objectAtIndex:indexPath.row];
cell.lblValue.numberOfLines = 0;
CGSize suggestedSize = [[APP_DELGATE.phonenumberDetailArray objectAtIndex:indexPath.row] sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:CGSizeMake(CONSTRAINT_WIDTH, LONG_LONG_MAX) lineBreakMode:UILineBreakModeWordWrap];
NSLog(@" 222222 suggested size height ph no => %f",suggestedSize.height);
cell.lblValue.frame = CGRectMake(cell.lblValue.frame.origin.x, cell.lblValue.frame.origin.y, cell.lblValue.frame.size.width, suggestedSize.height);
cell.lblValue.text=(NSString*)[APP_DELGATE.phonenumberDetailArray objectAtIndex:indexPath.row];
}
else if(indexPath.section==EMAIL_DETAIL_SECION) {
cell.lblName.text=(NSString*)[APP_DELGATE.emailLabelDetailArray objectAtIndex:indexPath.row];
cell.lblValue.numberOfLines = 0;
CGSize suggestedSize = [[APP_DELGATE.emailDetailArray objectAtIndex:indexPath.row] sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:CGSizeMake(CONSTRAINT_WIDTH, LONG_LONG_MAX) lineBreakMode:UILineBreakModeWordWrap];
cell.lblValue.frame = CGRectMake(cell.lblValue.frame.origin.x, cell.lblValue.frame.origin.y, cell.lblValue.frame.size.width, suggestedSize.height);
cell.lblValue.text=(NSString*)[APP_DELGATE.emailDetailArray objectAtIndex:indexPath.row];
}
else if(indexPath.section==URL_DETAIL_SECTION) {
cell.lblName.text=(NSString*)[APP_DELGATE.urlLabelDetailArray objectAtIndex:indexPath.row];
cell.lblValue.numberOfLines = 0;
CGSize suggestedSize = [[APP_DELGATE.urlDetailArray objectAtIndex:indexPath.row] sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:CGSizeMake(CONSTRAINT_WIDTH, LONG_LONG_MAX) lineBreakMode:UILineBreakModeWordWrap];
NSLog(@" 222222 suggested size height url => %f",suggestedSize.height);
cell.lblValue.frame = CGRectMake(cell.lblValue.frame.origin.x, cell.lblValue.frame.origin.y, cell.lblValue.frame.size.width, suggestedSize.height);
cell.lblValue.text=(NSString*)[APP_DELGATE.urlDetailArray objectAtIndex:indexPath.row];
}
else if (indexPath.section==ADDRESS_DETAIL_SECTION) {
cell.lblName.text=(NSString*)[APP_DELGATE.addressDetailLabelArray objectAtIndex:indexPath.row];
cell.lblValue.numberOfLines = 0;
CGSize suggestedSize = [[APP_DELGATE.addressDetailArray objectAtIndex:indexPath.row] sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:CGSizeMake(CONSTRAINT_WIDTH, LONG_LONG_MAX) lineBreakMode:UILineBreakModeWordWrap];
cell.lblValue.frame = CGRectMake(cell.lblValue.frame.origin.x, cell.lblValue.frame.origin.y, cell.lblValue.frame.size.width, suggestedSize.height);
cell.lblValue.text = (NSString*)[APP_DELGATE.addressDetailArray objectAtIndex:indexPath.row];
}
else if(indexPath.section==BIRTHDAY_DETAIL_SECTION) {
NSString *birthdateStringLocal=[ContactDetails getBirthDate:record];
cell.lblName.text=BIRTHDAY_LABEL;
cell.lblValue.text=birthdateStringLocal;
}
else if(indexPath.section==NOTE_DETAIL_SECTION) {
cell.lblName.text=NOTES_LABEL;
cell.lblValue.numberOfLines = 0;
CGSize suggestedSize = [noteString sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:CGSizeMake(CONSTRAINT_WIDTH, LONG_LONG_MAX) lineBreakMode:UILineBreakModeWordWrap];
cell.lblValue.frame = CGRectMake(cell.lblValue.frame.origin.x, cell.lblValue.frame.origin.y, cell.lblValue.frame.size.width, suggestedSize.height);
cell.lblValue.text=noteString;
}
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section==PHONE_DETAIL_SECTION){
CGSize suggestedSize = [[APP_DELGATE.phonenumberDetailArray objectAtIndex:indexPath.row] sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:CGSizeMake(CONSTRAINT_WIDTH, LONG_LONG_MAX) lineBreakMode:UILineBreakModeWordWrap];
NSLog(@"1111111suggested size height Phone no=> %f",suggestedSize.height);
return suggestedSize.height + MARGIN_SIZE;
}
else if (indexPath.section==ADDRESS_DETAIL_SECTION){
CGSize suggestedSize = [[APP_DELGATE.addressDetailArray objectAtIndex:indexPath.row] sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:CGSizeMake(CONSTRAINT_WIDTH, LONG_LONG_MAX) lineBreakMode:UILineBreakModeWordWrap];
NSLog(@"1111111suggested size height Address=> %f",suggestedSize.height);
return suggestedSize.height + MARGIN_SIZE;
}
else if (indexPath.section==EMAIL_DETAIL_SECION){
NSLog(@"String 1111: %@", [APP_DELGATE.emailDetailArray objectAtIndex:indexPath.row]);
CGSize suggestedSize = [[APP_DELGATE.emailDetailArray objectAtIndex:indexPath.row] sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:CGSizeMake(CONSTRAINT_WIDTH, LONG_LONG_MAX) lineBreakMode:UILineBreakModeWordWrap];
NSLog(@"1111111suggested size height Email=> %f",suggestedSize.height);
return suggestedSize.height + MARGIN_SIZE;
}
else if (indexPath.section == NOTE_DETAIL_SECTION){
CGSize suggestedSize = [noteString sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:CGSizeMake(CONSTRAINT_WIDTH, LONG_LONG_MAX) lineBreakMode:UILineBreakModeWordWrap];
NSLog(@"1111111suggested size height NOTES=> %f",suggestedSize.height);
return suggestedSize.height + MARGIN_SIZE;
}
else if (indexPath.section == URL_DETAIL_SECTION){
CGSize suggestedSize = [[APP_DELGATE.urlDetailArray objectAtIndex:indexPath.row] sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:CGSizeMake(CONSTRAINT_WIDTH, LONG_LONG_MAX) lineBreakMode:UILineBreakModeWordWrap];
NSLog(@"1111111suggested size height URL=> %f",suggestedSize.height);
return suggestedSize.height + MARGIN_SIZE;
}
else
return 50;
}
我的地址&動態表格單元格高度注意,但不是電子郵件,電話沒有,URL。如果電話號碼或網址足夠長,則不顯示任何內容。 請看下面的截圖
幫助任何knid表示讚賞。謝謝。
非常感謝......只需更改CONSTRAINT_WIDTH即可完美工作。 – iOSAppDev 2012-03-22 11:27:10
你能告訴我爲什麼我需要設置cell.lblValue.lineBreakMode = UILineBreakModeWordWrap; ?因爲我已經設置cell.lblValue.numberOfLines = 0. – iOSAppDev 2012-03-22 11:29:14
因爲默認的'UILineBreakMode'設置爲'UILineBreakModeTailTruncation',所以如果你想通過單詞鏈接中斷,那麼你必須使用這個。 – hchouhan02 2012-03-22 11:36:38