在UITableViewCell中进行更改

时间:2014-05-15 05:10:58

标签: ios uitableview tableview

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

label.tag = indexPath.row+1;

点击按钮,我正在使用这样的代码:

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

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

4 个答案:

答案 0 :(得分:1)

您可以通过其他方式完成此操作。

  1. 首先,您需要在班级中声明一个BOOL

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

  3. cellForRowAtIndexPath 中,检查 hideLabels ,如果是,则使用代码隐藏标签。

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

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

答案 1 :(得分:0)

 for(id object in tableView.subviews)
    {

      if([object isKindOfClass:[UILabel class]])
      {
        UILabel *label = (UILabel *) object;
        [[label setHidden:YES];      

      }
  }

答案 2 :(得分:0)

在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];
}

答案 3 :(得分:0)

您应首先获得对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]]];
}
}

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

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

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