Перезапись UITableView - PullRequest
       3

Перезапись UITableView

0 голосов
/ 30 декабря 2018

У меня есть приложение UITableView, которое состоит из текста и изображений.Ячейки иногда перезаписывают себя, и если я прокручиваю, проблема ухудшается.См. http://web -cars.com / images / temp_img / Simulator-Screen-Shot.png

У меня была такая же проблема в предыдущем приложении, и друг показал мне, как ее решитьпутем подкласса UITableViewCell.Это было только текстовое приложение, и я не могу получить то же решение с изображениями.

Вот мой подкласс UITableViewCell:

-(void)prepareForReuse  {
    self.permLabel101.text = @"";
    self.permUIImageView201.image = nil;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier    {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    UILabel *label101 = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, 310, 100)];
    label101.tag = 101;
    [label101 setLineBreakMode:NSLineBreakByWordWrapping];
    [label101 setFont:[UIFont systemFontOfSize:FONT_SIZE]];
    label101.textColor = [UIColor blackColor];
    label101.backgroundColor = [UIColor clearColor];
    label101.numberOfLines = 0;

    self.permLabel101 = label101;

    [self.contentView addSubview:self.permLabel101];

    UIImageView *image201 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 100)];
    image201.tag = 201;

    return self;
}

Вот мой cellForRowAtIndexPath.Я делю текст и изображения с photo_text.

- (MyTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSManagedObject *object = (NSManagedObject *)[entityArray objectAtIndex:indexPath.row];

    NSString *CellIdentifier = @"Cell";

    int photo_text = [[object valueForKey:@"photo_text"]intValue];

    MyTableViewCell *cell = (MyTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)   {
        cell = [[MyTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView setSeparatorColor:[UIColor clearColor]];    // Eliminates border between cells

    if (photo_text == 1)    {   //  Image will be here

        NSNumber *imgHeight = [[entityArray objectAtIndex:indexPath.row]valueForKey:@"imgHeight"];
        float imgHeightFloat = [imgHeight floatValue];
        NSLog(@"imgHeightFloat: %f", imgHeightFloat);

        UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(0, 2, 320, imgHeightFloat)];    //  imv is OK
        UIImageView *image201View = [[UIImageView alloc]initWithFrame:CGRectMake(0, 2, 320, imgHeightFloat)];

        imv.image = [UIImage imageNamed:[object valueForKey:@"photo"]];
        image201View.image = [UIImage imageNamed:[object valueForKey:@"photo"]];

        NSLog(@"Image cell: %@", cell);

        NSString *title = [object valueForKey:@"title"];
        self.title = title;
        [cell.contentView addSubview:image201View];

        return cell;

    }   else if (photo_text == 2)       {   //  Text will be here

        UILabel *label101;

        cell.selectionStyle = UITableViewCellSeparatorStyleNone;
        [self.tableView setSeparatorColor:[UIColor clearColor]];    //  Eleminates border between cells

        NSString *text = [object valueForKey:@"text"];

        CGSize constraint = CGSizeMake(296.0f, 20000.0f);
        CGSize size = [text sizeWithFont: [UIFont systemFontOfSize:12.0f] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];

        NSLog(@"Text cell: %@", cell);

        label101 = (UILabel *)[cell viewWithTag:101];

        label101.frame = CGRectMake(5, 5, 320, size.height);
        label101.text = [NSString stringWithFormat:@"%@", text];

        NSString *title = [object valueForKey:@"title"];
        self.title = title;

        return cell;
    }
        // Configure the cell...
//    return cell;
}

Предложения приветствуются !!

1 Ответ

0 голосов
/ 12 января 2019

Друг смог указать мне правильное направление.

Моя главная проблема состояла в том, что у меня возник конфликт CellIdentifier.

Перед оператором if было следующее, разделяющее изображение итекстовые ячейки:

NSString *CellIdentifier = @"cell";

И мне нужно было использовать разные CellIdentifiers для изображения и текстовых ячеек:

NSString *CellIdentifier = @"cell";

и NSString * CellIdentifier = @ "cell2";

Также оказывается, что мне не нужно было помещать мои UILabel и UIImageView в подкласс UITableViewCell.

Наконец, чтобы избавиться от предупреждения на cellForRowAtIndexPath: мне нужно было

`        return cell;
}   else    {
    return nil;
}`
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...