UITableCell 显示不正确的内容

Posted

技术标签:

【中文标题】UITableCell 显示不正确的内容【英文标题】:UITableCell displaying improper conten 【发布时间】:2015-02-13 02:49:12 【问题描述】:

我正在显示一个包含图像、按钮、UIView 的 TabelCell。单击按钮时,它必须显示 UIView。

UIView 正在显示,但问题是,如果我单击第一个单元格中的按钮而不是在第一个单元格中显示 UIView,它会显示第二个单元格或其他一些单元格。我不知道为什么会这样。

代码如下

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

  static NSString *simpleTableIdentifier = @"HomeTablecell";
  cell = (HomeTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
  cell.vEdit.hidden=TRUE; //UIView hidden initially
  [cell.editButton addTarget:self action:@selector(EditButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; //When this button is clicked cell.vEdit become visible


-(void)EditButtonClicked:(UIButton*)sender

  cell.vEdit.hidden=FALSE;

【问题讨论】:

所以每次回收一个单元格时,你都会添加另一个目标? 是的,每个单元格都会有不同的条件 但是你不认为每个单元格应该只添加一个目标吗?而不是每次回收都添加一个? 申请要求不是这样的......每个单元格都会有共同的项目,如姓名,个人资料图像,发布时间,主图像,编辑按钮,删除按钮和其他用于报告垃圾邮件的按钮。根据条件,功能将发生变化。我现在正在尝试不同的方法,不知道它是否有效。不过现在就去试试 那为什么每次回收cell都要加一个target呢? 【参考方案1】:

我的问题的答案是 FPPopover,当我使用该控件时,视图会准确显示在我想要的位置

https://github.com/50pixels/FPPopover/

【讨论】:

【参考方案2】:

1:确保正确引用了单元格(例如我在单击的按钮上添加了标签)

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

  static NSString *simpleTableIdentifier = @"HomeTablecell";
  cell = (HomeTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
  cell.vEdit.hidden=YES; //UIView hidden initially
   cell.editBUtton.tag = indexPath.row //suppose has 1 section, you can customise the tag rule based on section/row
  [cell.editButton addTarget:self action:@selector(EditButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; //When this button is clicked cell.vEdit become visible


-(void)EditButtonClicked:(UIButton*)sender

HomeTableCell* theCell = self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:sender.tag inSection:0];
  theCell.vEdit.hidden =NO;

2:确保 UIView 隐藏在您自定义的单元格类中,以便重用单元格

//Your cell class
- (void)prepareForReuse 
    [super prepareForReuse];
    self.vEdit.hidden=YES;

【讨论】:

那么,为什么每次cell回收的时候都要添加一个新的target呢? 你有什么解决办法吗? @HotLicks

以上是关于UITableCell 显示不正确的内容的主要内容,如果未能解决你的问题,请参考以下文章

UITableCell 高度未正确调整

UITableCell 中的 UILabel 大小不正确,直到 UITableView 滚动

如何检测 UITableCell 上的右滑动并显示自定义按钮而不是删除按钮

将 UITableCell 拖到 UITableView 上

iOS-iOS uitablecell 怎么设置文字显示区域的宽度

实现可变大小的多行 UITableCell 的最佳方法是啥?