【问题标题】:Having trouble making a UITableView behave无法使 UITableView 正常运行
【发布时间】:2011-11-29 03:09:15
【问题描述】:

我正在创建一个简单的消息传递应用程序,用户可以在其中发送图像或文本消息。我正在使用 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;
}

非常感谢任何帮助!提前致谢!

【问题讨论】:

  • 发送图片时会发生什么?可以发一些代码吗?
  • 当我发布一张图片时,它显示得很好。但是,当我尝试在此之后发送文本时,它会将其放置为好像其上方单元格的高度是默认值,而不是我在 heightForRowAtIndexPath 中指定的自定义高度 (170)。那有意义吗?例如,下面是一系列事件: 1. 发送图像 2. 图像完美显示 3. 发送文本 4. 文本单元格显示在图像上方,而不是下方。看起来好像它认为它上面的单元格是默认高度。有什么想法吗?
  • 没关系,下面的答案很完美。谢谢!

标签: iphone ios uitableview


【解决方案1】:

您在 heightForRowAtIndexPath 中的 if 语句似乎是错误的。它看起来应该是:

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

这与您在 cellForRowAtIndexPath 中的情况相同,这听起来像是您想要的。大概在显示图像的单元格中,message 字符串为 "",因此 if 语句为真,它使单元格高 44 像素。

【讨论】:

  • 非常感谢!我没有意识到这是一个简单的调试问题。否则我会自己寻找它:)
猜你喜欢
  • 2021-04-22
  • 2015-12-08
  • 2019-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多