2011-11-28 61 views
0

我創建一個簡單的消息傳遞應用程序,其中用戶可以發送任何圖像或文本消息。我使用UITableView來顯示消息,並且我使用HeightForRowAtIndexPath來確定給定單元格的大小,具體取決於它是否包含圖像。但是,在發送圖像之後,下一個文本消息(另一個UITableViewCell)位於圖像的頂部(比UITableViewCell的默認高度高得多),並且我找不到原因!有麻煩一個UITableView表現

下面是我對HeightForRowAtIndexPath代碼:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
Message *messageInUse = [messageArray objectAtIndex:indexPath.row]; 

if(messageInUse.message != nil || [[messageInUse message] isEqualToString:@""]) 
{ 
    CGSize size = [[messageInUse message] sizeWithFont:[UIFont systemFontOfSize:16] constrainedToSize:CGSizeMake([messageTable frame].size.width, [messageTable frame].size.height) lineBreakMode:UILineBreakModeWordWrap]; 
    return size.height + 44; 
} 
else if(messageInUse.image != nil) 
{ 
    return 170; 
} 
else 
    return 0; 
} 

而且的cellForRowAtIndexPath:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
for(int i = 0; i < [[cell subviews] count]; i++) 
{ 
    if([[[cell subviews] objectAtIndex:i] isKindOfClass:[UIImageView class]]) 
    { 
     cell = nil; 
     break; 
    } 
} 

if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
} 


Message *messageInUse = [[messageArray objectAtIndex:indexPath.row] retain]; 

if([messageInUse message] != nil && ![[messageInUse message] isEqualToString:@""]) 
{ 
    NSLog(@"Message sent"); 
    [cell.textLabel setText:[messageInUse message]]; 
    [[cell textLabel] setNumberOfLines:0]; 
    [[cell textLabel] setLineBreakMode:UILineBreakModeWordWrap]; 
    [cell.detailTextLabel setText:[messageInUse timeStamp]]; 

} 
else if([messageInUse image] != nil) 
{ 
    NSLog(@"Image sent"); 

    [cell setFrame:CGRectMake([cell frame].origin.x, [cell frame].origin.y, [cell frame].size.width, 180)]; 
    //Need to make sure that the date label is under the image 
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 2, 96, 126)]; 
    [imageView setContentMode:UIViewContentModeScaleToFill]; 

    [imageView setImage:[messageInUse image]]; 
    [cell addSubview:imageView]; 
    [imageView release]; 
    //[cell.detailTextLabel setText:[messageInUse timeStamp]]; 

    UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 135, 320,20)]; 
    [timeLabel setText:[messageInUse timeStamp]]; 
    [timeLabel setFont:[UIFont systemFontOfSize:14]]; 
    [timeLabel setTextColor:[UIColor grayColor]]; 
    [cell addSubview:timeLabel]; 
    [timeLabel release]; 
} 
else 
{ 
    //Something is wrong with the message 
    [cell.textLabel setText:@"Error occurred. Please resend"]; 
    [cell.detailTextLabel setText:[self getFormattedDate]]; 
} 


[cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 
[messageInUse release]; 
return cell; 
} 

任何幫助,非常感謝!提前致謝!

+1

發送圖像時會發生什麼?你可以發佈一些代碼嗎? –

+0

當我發佈圖片時,顯示正常。然而,當我嘗試在此之後發送文本時,它會將其放置在其上方的單元格的高度爲默認值,而不是我在'heightForRowAtIndexPath'中指定的自定義高度(170)。那有意義嗎? 例如,以下是一系列事件: 1.發送圖像 2.圖像完美顯示 3.發送文字 4.文字單元顯示在圖像上方,而不是下方。它看起來好像認爲它上面的單元格是默認高度。 有什麼想法? – Mason

+0

沒關係,下面的答案完美無缺。謝謝! – Mason

回答

1

它看起來像你的if語句中heightForRowAtIndexPath是錯誤的。它洛斯像它應該閱讀:

if(messageInUse.message != nil && ![[messageInUse message] isEqualToString:@""]) 

這是一樣的,你在cellForRowAtIndexPath有話,這聽起來像它你想要什麼。假設在顯示圖像的單元格中,message字符串爲"",因此if語句爲true,並且它使單元格高44像素。

+0

非常感謝你!我沒有意識到這是一個簡單的調試問題。否則,我會本人尋找它:) – Mason