【问题标题】:Unable to update the Subview in a Custom UITableViewCell无法更新自定义 UITableViewCell 中的子视图
【发布时间】:2016-05-28 16:14:34
【问题描述】:

我有一个自定义 UITableViewCell ,其中我有两个 UIViews 。我想更改 UIViews 的 BackgroundColor 半径和更多属性。

但我做不到。

这是我的设置。

第 1 步:

Create A Custom Cell with XIB.

第 2 步:在 XIB 中为 CustomCell 输入 Cell Identifer 名称。

第 3 步:在 viewDidLoad 中实例化 NIB

 UINib *nib = [UINib nibWithNibName:@"CustomCell" bundle:nil];
 [[self mTableView] registerNib:nib forCellReuseIdentifier:@"CustomCell"];

第 4 步:索引方法中的行单元格:

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
    // Create an instance of ItemCell
   CustomCell *cell =  [tableView dequeueReusableCellWithIdentifier:@"CustomCell"];

   [cell updateCellView];

   return cell;
}

第 5 步:检查两次插座连接一切正常。

第 6 步:自定义单元格类:

    #import "TransportCell.h"

    @implementation TransportCell

    - (void)awakeFromNib {
         // Initialization code

      }

    - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
       [super setSelected:selected animated:animated];

        // Configure the view for the selected state
     }


    - (void)updateCellView
    {
     self.backgroundView.backgroundColor = [UIColor redColor];
    }

   @end

此代码对我的单元格视图没有影响。

我调试代码。当我登录backgroundView 时,当updateCellView 方法被调用时,我得到nil

这是我的 CustomCell 的 Xib:

我必须更改内部 UIView 的属性。(蓝色)

【问题讨论】:

  • 试试 self.backgrondColor = [UIColor redcolor]
  • 它会改变单元格背景颜色
  • 你为你的背景视图做了出口还是连接到你的视图?

标签: ios objective-c uitableview ios7 tableviewcell


【解决方案1】:

不要调用cellForRowAtIndexPath 中的方法,试试这个:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
    // Create an instance of ItemCell
   CustomCell *cell =  [tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
   return cell;
}

然后在你的自定义方法中

- (void)updateCellView
    {
     self.backgroundView.backgroundColor = [UIColor redColor];

     // reload the table view
     [self.tableView reloadData];
    }

【讨论】:

  • 兄弟你看到这个问题了吗,该方法存在于单元类中而不是当前视图控制器中
  • 如何在我的自定义单元格中获取表格视图的参考
  • 对不起,我没有检查...但是您也可以在视图控制器中创建自定义方法,然后您可以管理视图
【解决方案2】:

您需要添加 [ tableview reloadData ] 在方法更新单元格视图中

【讨论】:

  • updateCellView 方法在 customClass 中。如何传递对该自定义单元格的引用
【解决方案3】:

理想情况下,渲染单元格所需的所有信息都应该来自数据源或其他事件。例如,如果您要更改边框颜色,则应该意味着某个实体(例如用户)已经执行了一个事件,或者数据源中的某些内容发生了变化,例如某个值超过了阈值。

数据源或那些事件的变化应该触发刷新整个表:

-(void) reloadData 

或一些单元格:

- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

只有这样单元格才会更新。

【讨论】:

  • 我想在单元格出现给用户之前更改这些属性。当用户第一次获得我的单元格时,我希望它们显示为圆形
  • reload 函数用于动态刷新表格。您想根据事件动态更新单元格还是仅在第一次渲染时更新它们?
【解决方案4】:

在 TransportCell 中试试这个方法,它的 swift 代码与目标 c 相同

override func layoutSubviews() 
{
 self.backgroundView.backgroundColor = [UIColor redColor];
}

【讨论】:

    【解决方案5】:

    您正在更改self.backgroundview.backgroundColor

    UITableViewStylePlain 中的单元格默认为 nilUITableViewStyleGrouped 中的单元格默认为非 nil。

    “backgroundView”将被添加为所有其他视图后面的子视图。

    @property (nonatomic, strong, nullable) UIView *backgroundView;
    

    【讨论】:

      【解决方案6】:

      使用这个:

      self.contentView.backgroundColor = [UIColor redColor];
      

      希望这会有所帮助!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多