0
votes

I am playing with some UITableView coding and tried to hide the default textLabel property. I used the UIScrollViewDelegate protocol and used both -scrollViewDidScroll and scrollViewDidEndDecelerating methods to hide and show the label.

The code works fine just with the first row of the table and not all of them which is what I want. Here's my code:

*Edited to show the solution code.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   cell = [self.tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

   cell.textLabel.hidden = NO;
   cell.textLabel.text = @"TEST";

   return cell;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    for (cell in [self.tableView visibleCells]) {
    cell.textLabel.hidden = YES;
  }
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    for (cell in [self.tableView visibleCells]) {
    cell.textLabel.hidden = NO;
  }
}

So anyone could help me to hide and show all the rows?

3

3 Answers

2
votes

It looks like you've tried declaring your cell as a variable in your table view controller or as a global somewhere. That's not going to work because it will always be set to whatever the last cell dequeued was. You should definitely declare that as locally in tableView:cellForRowAtIndexPath:.

Hiding the textLabel in scollViewDidScroll and showing it again in scrollViewDidEndDecelerating should work fine, you just have to make sure you're hiding/showing all the currently visible cells in the table. Luckily, there's a method of tableView that'll help with that: visibleCells. That returns an NSArray of UITableViewCells which you can loop over and hide the textLabel.

So, it should look something like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    NSDate *object = self.objects[indexPath.row];
    cell.textLabel.text = [object description];

    cell.backgroundColor = [UIColor redColor];
    return cell;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    for (UITableViewCell *cell in [self.tableView visibleCells]) {
        cell.textLabel.hidden = YES;
    }
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    for (UITableViewCell *cell in [self.tableView visibleCells]) {
        cell.textLabel.hidden = NO;
    }
}
0
votes

Try putting this cell.textLabel.hidden = YES;in your cellForRowAtIndexPath method. that is where each cell is created.

0
votes

NOTE: This is just one possible solution, There might be other, more optimized solutions out there.

  • Get the visible cells from your table each time the table begins scrolling with scrollViewDidScroll:. Then iterate over each cell and set it's textLabel hidden.

  • Get the visible cells in scrollViewDidEndDecelerating: and set the textLabel visible.

Get the visible cells with yourTableView.visibleCells.