【问题标题】:display images in tableview cell在 tableview 单元格中显示图像
【发布时间】:2017-05-13 06:19:26
【问题描述】:

我必须在点击图像上的前进按钮时在 tableview 单元格中显示图像,但滚动后 tableview 图像正在改变...... 我已经为单元格中的每个按钮提供了标记值,并将所有单元格引用存储在数组中,然后单击前进按钮,我正在识别数组中的引用并更改该特定引用的图像..

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

  //  static NSString *cellidentifier = @"cell";
   cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[LotsandLandTableCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
   // cell.smallimage.image=[UIImage imageNamed:[_smallimagesarr objectAtIndex:indexPath.row]];
    //if ( [str_scroll isEqualToString: @"scrolling"]) {

  //  }else{
    cell.bigimage.image=[UIImage imageNamed:[_bigimagesarr objectAtIndex:indexPath.row]];
    //}

     cell.forwardbutton.tag =indexPath.row;
    NSLog(@" %p",cell);
    [cell.forwardbutton addTarget:self action:@selector(buttonInfo:) forControlEvents:UIControlEventTouchUpInside];
 CellModel *cellmodel=[[CellModel alloc]init];
    cellmodel.tagValue=cell.forwardbutton.tag;
    cellmodel.cellobj=cell;
    [arrofcells addObject:cellmodel];
    NSLog(@"%p",cell);
    return cell;

}


-(void)buttonInfo:(UIButton *)sender{
    UIButton *btn = (UIButton *)sender;
    NSInteger tag = btn.tag;

   CellModel *cmodel=[[CellModel alloc]init];

    cmodel=[arrofcells objectAtIndex:tag];
    democell=(LotsandLandTableCell *)cmodel.cellobj;


   previoustag=cmodel.tagValue;
 if(cmodel.tagValue==previoustag)
   {

       if(j<_bigimagesarr1.count){
       democell.bigimage.image=[UIImage imageNamed:[_bigimagesarr1 objectAtIndex:j]];
      NSLog(@"j value is  is  %d",j);
           j++;

      }else{
          j=0;
         [democell.forwardbutton setEnabled:NO];

       }

  }
}

cellModel 包含单元格引用和属性标记

@interface CellModel : NSObject
@property (nonatomic, assign) NSInteger tagValue;
@property(nonatomic,strong) id cellobj;
@end

【问题讨论】:

  • 不要使用标签。为什么不简单地将图像数组提供给您的单元格并处理单元格类中的按钮?此外,每次重用单元格时,您都会添加按钮处理程序。
  • @Paulw11 没听明白,你能说得更具体些吗
  • 您正在尝试处理表格视图控制器中的按钮点击,这意味着您需要确定按钮点击发生在哪个单元格中,您正在使用标签。标签很脆弱,因为单元格可以重复使用并且可以重新排序。如果您只是将图像数组设置为自定义单元格类的属性,那么每个单元格都可以处理自己的按钮点击,并且只需在其图像数组中移动。

标签: ios objective-c uitableview uibutton


【解决方案1】:

您当前的代码没有使用适当的关注点分离;您的表格视图控制器不关心单元格中当前显示的是哪个图像,因此尝试处理表格视图控制器中的按钮点击是不必要的复杂。

您尚未显示所有源代码,因此我所拥有的可能与您的代码 100% 不匹配 - 我正在做出有根据的猜测。此外,您似乎有两个不同的图像数组,我认为这只是您的测试方法。实际上,每个单元格应该有一个图像数组,每个数组都包含在其他一些数据结构中,其中的一个数组是表格视图的数据源。

LotsandLandTableCell.h

-(void)setImages:(NSArray *)images;

LotsandLandTableCell.m

 @property (weak, nonatomic) NSArray *bigImages;
 @property NSInteger imageIndex;

-(void) prepareForReuse {
    self.bigImages = nil;
    self.bigimage.image = nil;
    self.imageIndex = 0;
}

-(void) setImages:(NSArray *)images {
    if (images != null) {
        self.bigImages = images;
        self.imageIndex = 0;
        [self updateBigImage];
    }
}

@IBAction forwardTapped:(UIButton *)sender {
    if (self.images.count == 0) {
        return;
    }
    self.imageIndex = (self.imageIndex + 1) % self.images.count;
    [self updateBigImage];
}

@IBAction backwardTapped:(UIButton *)sender {
   if (self.images.count == 0) {
       return;
   }
   self.imageIndex -= 1;
   if (self.imageIndex < 0) {
       self.imageIndex = self.bigImages.count;
   }
   [self updateBigImage];
}

- (void) updateBigImage {
    self.bigimage.image = [UIImage imageNamed:self.images[self.imageIndex]];
}

视图控制器

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LotsandLandTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[LotsandLandTableCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }

     NSArray *yourImagesArray = yourDataArray[indexPath.row].bigImages; // Change this

     [cell setImages:yourImageArray];

     return cell;
}

【讨论】:

    【解决方案2】:

    在lotsandLandTableCell 类中添加这个

    -(void)prepareForReuse {
        _bigimage.image = nil;
     }
    

    当你滚动 tableView 时,图片会发生变化,因为它可能会使用以前的图像,所以当 tableView 重用它的单元格时,最好清除以前的单元格数据。

    【讨论】:

    • 静止图像正在变化
    【解决方案3】:

    显示行和图像的方式存在问题。

    您需要遵循以下步骤以获得最佳效果:

    1. 创建用于在行中填充数据的项目模型(在您的情况下 CellModel 也可以工作) 以下可能是模型的属性:
      • 物品ID
      • 价格
      • 说明
      • 地区
      • imageNames(Array) 等

    imageNames 数组将包含您希望在向前单击特定行时显示的所有图像名称。

    将此项目对象分配给您的LotsandLandTableCell 类,并在单元格本身中处理前进按钮回调,在前进单击时从imagenNames 数组中选择另一个名称并将其设置为imageView。

    因此,通过使用上述方法,您可以在有任何问题的情况下实现此白化。

    您可以创建所有属性的数组(一个项目模型将代表一个属性)。

    以下示例可作为参考:

    - (NSInteger)numberOfRowsInSection:(NSInteger)section
    {
      return items.count;
    }
    
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
      //  static NSString *cellidentifier = @"cell";
       cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (cell == nil) {
            cell = [[LotsandLandTableCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        }
    
    cell.item = [items objectAtIndex:indexpath.row];
        return cell;
    
    }
    

    现在在您的自定义单元格类中,您可以实现其余逻辑来填充您的所有单元格数据:

    【讨论】:

      猜你喜欢
      • 2019-11-29
      • 1970-01-01
      • 1970-01-01
      • 2016-04-21
      • 2021-07-07
      • 2015-11-14
      • 2018-03-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多