【问题标题】:iOS - UiTableviewCell - Background Image only showing on one cell at a timeiOS - UiTableviewCell - 背景图像一次只显示在一个单元格上
【发布时间】:2012-04-15 01:10:53
【问题描述】:

我已使用 UIAppearance 设置应用程序中表格单元格的背景图像。

[[UITableViewCell appearance] setBackgroundView:[ [UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"list_bg.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]];

但是,当我查看列表时,仅为屏幕上的一个单元格设置背景图像。如果我滚动屏幕,则会在出现在视图中的单元格上设置背景图像,但不会在任何其他可见单元格上设置此背景图像。

有人知道发生了什么吗?我以为通过设置 UITableviewCell 的 UIAppearance ,这种类型的所有实例都会自动获取背景图片。

谢谢

布赖恩

这是我的 cellForRowAtIndexPath 方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"plantCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    [[NSBundle mainBundle] loadNibNamed:@"PlantListCell" owner:self options:nil];
    cell = _plantCell;
    self.plantCell = nil;
}


Plant *plant = [self.fetchedResultsController objectAtIndexPath:indexPath];

UIImageView *thumbnail = (UIImageView *)[cell viewWithTag:1];
thumbnail.image = [UIImage imageNamed:[plant.name stringByAppendingString:@"_S"]];

UILabel *label = (UILabel *)[cell viewWithTag:2];
label.text = plant.name;

UIImageView *set = (UIImageView *)[cell viewWithTag:3];
set.image = [UIImage imageNamed:@"set"];

UIImageView *favourite = (UIImageView *)[cell viewWithTag:4];
favourite.image = [UIImage imageNamed:@"fav"];

return cell;
}

【问题讨论】:

    标签: iphone ios uitableview background-image


    【解决方案1】:

    是的。这个问题是你告诉 UITableViewCell 类为每个表格视图单元格的背景视图设置一个 UIImageView 。一旦它被设置为一个,它就会从之前的 superview 中移除,并添加到新的 UITableViewCell 中。您没有在每个 tableViewCell 上设置重复的 UIImageViews;您将在每个表格视图单元格上设置一个视图。

    然后从每个 tableViewCell 设置和取消设置,直到它设置的最后一个。

    使用此方法的外观代理将其设置在 UITableViewCell 的每个对象上。

    我不会使用外观代理来设置背景视图方法,因为您传递给它的任何视图都是一个视图,一次只能应用于一个单元格。

    我的建议是为每个单元格独立创建一个视图,或者创建一个在初始化时设置它的子类。

    【讨论】:

    • 干杯。有道理就好。我通过直接设置单元格 BG 图像而不是使用 UIAppearance 来解决它。一两张桌子都可以,但如果你有很多桌子就不行了。一旦获得许可,我将在 8 小时内将我的代码附加到答案中。布赖恩
    【解决方案2】:

    感谢条形码项目。这有点道理,但我不明白为什么苹果会费心让我们设置 UITableviewCell 背景图像,如果它真的不起作用。 最后我采用了一个简单的解决方案,只需在 cellForRowAtIndexPath 方法中设置图像。

    cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"list_bg.png"]];
    

    它可以正常工作,但如果您有很多表并希望以通用方式设置它,则效果不佳。

    干杯

    布赖恩

    【讨论】:

      【解决方案3】:

      我通过创建一个名为 CustomizableUITableCell 的基类并为背景图像设置一个 ui 外观值来解决这个问题。然后,每当我创建一个需要自定义背景的单元格时,我都会简单地继承 CustomizableUITableCell。

      //标题

      #import <UIKit/UIKit.h>
      
      @interface CustomizableUITableCell : UITableViewCell <UIAppearance>
      
      @property (nonatomic, weak) UIColor *backgroundCellColor UI_APPEARANCE_SELECTOR;
      
      @end
      

      //实现

      #import "CustomizableUITableCell.h"
      
      @implementation CustomizableUITableCell
      
      @synthesize backgroundCellColor;
      
      #pragma mark - Public Methods.
      
      -(void)setBackgroundCellColor:(UIColor *)backgroundColor
      {
          [super setBackgroundColor:backgroundColor];
      }
      
      @end
      

      然后您可以使用外观代理进行设置。

      [[CustomizableUITableCell appearance] setBackgroundCellColor:[UIColor colorWithPatternImage:backgroundImage]];
      

      【讨论】:

      • 不幸的是,这似乎只在您不必滚动表格时才有效。出于某种原因,一旦 [tableView dequeueReusableCellWithIdentifier:cellIdentifier] 重用已分配的单元格,它就会将所有内容重置为我的 Storyboard 中设置的值。
      【解决方案4】:

      cell.backgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"1.jpg"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];

      它对我有用..

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-15
        • 1970-01-01
        • 2018-04-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多