无法使UITableView表现出色

时间:2011-11-28 18:56:19

标签: iphone ios uitableview

我正在创建一个简单的消息传递应用程序,用户可以在其中发送图像或文本消息。我正在使用UITableView来显示消息,我正在使用HeightForRowAtIndexPath来确定给定单元格的大小,具体取决于它是否包含图像。

然而,在发送图像之后,下一条文本消息(另一个UITableViewCell)位于图像的顶部(比UITableViewCell的默认高度高得多),我无法弄清楚原因!

以下是我对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 :(得分:1)

看起来heightForRowAtIndexPath中的if语句是错误的。它应该读得像它一样松散:

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

这与你在cellForRowAtIndexPath中的情况相同,这听起来就像你想要的那样。据推测,在显示图像的单元格中,message字符串为"",因此if语句为true,使单元格高44像素。