Возникли проблемы с поведением UITableView - PullRequest
0 голосов
/ 28 ноября 2011

Я создаю простое приложение для обмена сообщениями, в котором пользователь может отправить изображение или текстовое сообщение. Я использую 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 Ответ

1 голос
/ 28 ноября 2011

Похоже, ваше утверждение if в heightForRowAtIndexPath неверно. Это выглядит так:

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

Это то же самое, что и в cellForRowAtIndexPath, что звучит так, как вы хотите. Предположительно, в ваших ячейках, которые отображают изображение, у вас есть строка message, равная "", поэтому утверждение if истинно и делает ячейку высотой 44 пикселя.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...