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?
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 UITableViewCell
s 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;
}
}