2014-08-08 2 views
0

У меня возникли проблемы с добавлением некоторых изображений внутри пользовательских ячеек в моей таблице. В каждой ячейке создается UIView, которому затем присваивается уникальный тег. Я тестировал, пытаясь добавить изображение в одну конкретную ячейку, например, используя тег «2204», но он все равно добавит это изображение к каждой третьей ячейке (2201, 2204 и т. Д.), Поэтому я я не знаю, что может даже вызвать это. Я устанавливаю ярлык в каждой ячейке, чтобы отобразить текущий тег представления, и он показывает, что теги правильные, но зачем тогда размещать изображения в других ячейках?Пользовательские изображения ячеек будут повторяться каждую третью ячейку

У меня есть только одна секция в этой таблице, и она идет только по строкам. По умолчанию отображаются 5 строк, но можно добавить больше строк.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    workoutTableViewCell *cell = (workoutTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     cell = [[workoutTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    cell.exerciseViewArea.tag = 2200 + indexPath.row; 

    //using fifth cell as a test 
    testview = [self.view viewWithTag:2204]; 
    return cell; 
} 

- (void)changeExerciseImage 
{ 
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(9,10,20,20)]; 
    imageView.image = [UIImage imageNamed:@"testImage.png"]; 
    [testview addSubview:imageView]; 
    [testview bringSubviewToFront:imageView]; 

    NSLog(@"changed exercise image to %@", _exerciseTempText); 
} 
+0

Как ваши изображения когда-либо попасть в клетку? Я не вижу нигде в вашем опубликованном коде, где вы вызываете changeExerciseImage. – rdelmar

ответ

1

Клетка может получить повторно по UITableView так, а не держать ссылку на отдельную ячейку вы лучше обновить источник данных и затем вызвать reloadData или reloadItemsAtIndexPaths. Так, например, вы могли бы иметь NSMutableArray имен изображений использовать для каждой ячейки, а затем сделать что-то вроде этого:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    workoutTableViewCell *cell = (workoutTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     cell = [[workoutTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(9,10,20,20)]; 
    [imageView setTag:myImageViewTag]; 
    [cell.exerciseViewArea addSubview:imageView]; 
    [cell.exerciseViewArea bringSubviewToFront:imageView]; 

    return cell; 
} 

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UIImageView *imageView = (UIImageView *)[cell.exerciseViewArea viewWithTag:myImageViewTag]; 
    [imageView setImage:[UIImage imageNamed:[myArrayImages objectAtIndex:indexPath.row]]]; 
} 

- (void)changeExerciseImage 
{ 
    [myArrayImages replaceObjectAtIndex:4 withObject:@"testImage.png"]; 
    [myTableView reloadData]; //or just update that cell with reloadItemsAtIndexPaths 
} 
Смежные вопросы