【问题标题】:Make Change in TableViewCell在 TableViewCell 中进行更改
【发布时间】:2014-05-15 05:10:58
【问题描述】:

我正在处理TableView,我使用了TableViewCell 上的标签,然后单击按钮我想隐藏表格中所有单元格的标签, 在标签中我设置了标签:

label.tag = indexPath.row+1;

在按钮上单击我正在使用这样的代码:

for (int i = 0; i < Array.count; i++)
{
    [[self.view viewWithTag:i+1] setHidden:YES];      
}

但是从我的代码中,标签只隐藏在最后一个单元格中,而不是所有其他单元格中。

【问题讨论】:

  • 什么是数组...显示?
  • 使用委托/数据源方法并告诉tableview重新加载特定的单元格。

标签: ios uitableview tableview


【解决方案1】:

您可以通过其他方式简单地做到这一点。

  1. 首先你需要在你的类中声明一个 BOOL

    @property(assign,nonatomic) BOOL hideLabels;
    
  2. 接下来在您的按钮操作处理程序方法中,设置此 YES

  3. 在您的cellForRowAtIndexPath中,检查hideLabels是否为YES,如果是,则使用代码隐藏标签。

     cell.yourLabel.hidden = hideLabels;
    
  4. 现在将 hideLabels 设置为 YES

    后重新加载表格
    [self.tableView reloadData];
    

【讨论】:

    【解决方案2】:
     for(id object in tableView.subviews)
        {
    
          if([object isKindOfClass:[UILabel class]])
          {
            UILabel *label = (UILabel *) object;
            [[label setHidden:YES];      
    
          }
      }
    

    【讨论】:

      【解决方案3】:

      在你的 ViewController.h 中

      BOOL isLabelHidden;
      

      在 ViewController.m 中

      - (void)viewDidLoad
      {
         isLabelHidden = FALSE;
      }
      
      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
          static NSString *CellIdentifier = @"TableCell";
      
          UILabel *lbl;
          UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
          cell = nil;
          if (cell == nil)
          {
              cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
          }
      
           if(isLabelHidden)
             [lbl setHidden:YES];
           else
             [lbl setHidden:NO];
      }
      

      在你的按钮点击方法

      - (void)buttonClicked
      {
        isLabelHidden = TRUE;
        [tableView reloadData];
      }
      

      【讨论】:

        【解决方案4】:

        您应该首先获取对UITableViewCell 的引用,然后您可以删除其中的标签。

        首先获取 tableview 中所有单元格的引用,如下所示:

        NSMutableArray *cells = [[NSMutableArray alloc] init];
        for (NSInteger j = 0; j < [tableView numberOfSections]; ++j)
        {
        for (NSInteger i = 0; i < [tableView numberOfRowsInSection:j]; ++i)
        {
            [cells addObject:[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:j]]];
        }
        }
        

        现在遍历单元格数组中的那些单元格以隐藏标签视图,

         for (int i = 0; i < cells.count; i++)
            {
               [[[cells objectAtIndex:i] viewWithTag:i+1] setHidden:YES];      
            }
        

        来源:How can I loop through UITableView's cells?

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-09-03
          • 1970-01-01
          • 2019-01-11
          • 1970-01-01
          相关资源
          最近更新 更多