【问题标题】:UITableCell displaying improper contenUITableCell 显示不正确的内容
【发布时间】: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呢?

标签: ios objective-c xcode uitableview uiview


【解决方案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
    猜你喜欢
    • 2014-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-29
    • 1970-01-01
    • 2011-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多