UITableViewCell проблемы с загрузкой - PullRequest
0 голосов
/ 20 февраля 2011

У меня есть UITableViewCell, загружающий 10 изображений, он загружает первые 6, затем его поговорку (ячейка! = Ноль), поэтому он не загружает остальные изображения, но если я удаляю «if (cell == nil)», он загружает всеизображения я что-то упускаю?

спасибо

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

AsyncImageView *asyncImageView = nil;

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

int section = indexPath.section;
NSMutableArray *sectionItems = [sections objectAtIndex:section];

if (cell == nil) {
    NSLog(@"CELL == NIL %@", cell);
    int n = ([sectionItems count]) ;
    cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    int i=0,f=0; 
    while (i < n)
    {
        int yy = 4 +f*74;
        for(int j=0; j<3;j++){
            Item *item = [sectionItems objectAtIndex:i];                
                int buttonX = 10;
                if (i == 0) {
                    buttonX = 10;
                }else {
                    buttonX = 203
                }

                CGRect frame;
                frame.origin.x = buttonX;
                frame.origin.y = yy;
                frame.size.width = 107;
                frame.size.height = 107;
                asyncImageView = [[[AsyncImageView alloc] initWithFrame:frame] autorelease];
                asyncImageView.tag = ASYNC_IMAGE_TAG;
                NSString *urlString = item.image;
                NSURL *url = [NSURL URLWithString:urlString];
                [asyncImageView loadImageFromURL:url];
                [cell.contentView addSubview:asyncImageView];

            i++;
        }
        f = f+1;
    }
}
else {
    NSLog(@"cell != nill");
    asyncImageView = (AsyncImageView *) [cell.contentView viewWithTag:ASYNC_IMAGE_TAG];                     
}

return cell;

}

Ответы [ 2 ]

0 голосов
/ 21 февраля 2011

- (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath {static NSString * CellIdentifier = @ "Cell";UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];if (cell == nil) {NSLog (@ "CELL == NIL% @", cell);cell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier] autorelease];cell.accessoryType = UITableViewCellAccessoryNone;cell.selectionStyle = UITableViewCellSelectionStyleNone;CGRect frame = CGRectMake (0,20,30,40);UIImageView * img = [[[UIImageView alloc] initWithFrame: frame];img.tag = IMAGE_TAG;[cell.contentView addSubview: img];[IMG релиз];} int row = indexPath.row;UIImageView * myImage = = (UIImageView *) [cell.contentView viewWithTag: IMAGE_TAG];UIImage * image = [UIImage imageNamed: [NSString stringWithFormat: @ "% d", строка]];[myImage setImage: image];возвращаемая ячейка;}

UITableViewCells кэшируются для экономии памяти и затрат на выделение ресурсов (следовательно, производительности).Вы создаете ячейку только один раз, и после этого вы устанавливаете ее содержимое в соответствии с вашим индексным путем.Я просто создал представление изображения в ячейке, а затем установил изображение в зависимости от индекса в этой ячейке (1.png, 2.png и т. Д.)

Прочтите документацию здесь .

0 голосов
/ 20 февраля 2011

UITableViewCells обычно используются для экономии времени и памяти, обычно, и вы используете эту парадигму, которую я вижу. Вам необходимо реализовать регистр cell! = Nil и настроить ячейку для отображения содержимого на основе indexPath. Ячейка ранее использовалась для отображения другого содержимого, поэтому вам необходимо настроить все зависимые от indexPath представления в ячейке, отличной от nil.

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