0

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?

Marco Almeida
  • 1,285
  • 3
  • 17
  • 39

3 Answers3

2

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;
    }
}
Mike S
  • 41,895
  • 11
  • 89
  • 84
  • Yes, I did declare cell globally. I fixed this and declared locally and also modified the scrollView methods, but still didn't work. All textLabels are still visible all the time. – Marco Almeida Oct 04 '14 at 06:33
  • Hey I managed to get it work just modifying form both scrollView methods [tableView visibleCells] to [self.tableView visibleCells] and added cell.textLabel.hidden = NO; to cellForRowAtIndexPath and all worked fine!!!! Thanks!!! – Marco Almeida Oct 04 '14 at 06:39
  • Opps, that was an error on my part not putting `self.` in there; I fixed the code in the answer. Glad you found it and fixed it :) – Mike S Oct 04 '14 at 06:40
0

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

Steve Rosenberg
  • 19,348
  • 7
  • 46
  • 53
  • If I do that all textLabels will be hidden without scrolling. I need them showing while static, but when the user scrolls the table only then the labels hide and when is finished scrolling the labels will be back again. – Marco Almeida Oct 04 '14 at 05:10
0

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.

n00bProgrammer
  • 4,261
  • 3
  • 32
  • 60