Z
Z
zzasdee2015-08-10 16:11:23
Objective-C
zzasdee, 2015-08-10 16:11:23

How to change UILabel when scrolling UIScrollView inside UITableViewCell?

Essence of a question: is UITableView. Inside it are custom cells of the MyTabbleViewCell class. Inside the cell lies a UIScrollView (which scrolls horizontally) and a UILabel.
MyTabbleViewCell
|- ​​ContentView
|--UIScrollView
|--UILabel
I need to change the values ​​in the label when scrolling the scroll view.
I made my MyScrollView. In it: During cell creation
@property (strong, nonatomic) id parent;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

I expose
cell.scrollView.parent = cell;
Now I can do this:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if ([scrollView isKindOfClass:[MyScrollView class]])
    {
        if (scrollView.parent)
        {
            MyTableViewCell *cell = scrollView.parent;
            cell.myLabel.text = ...
        }
    }
}

You can also save cells in MyScrollView indexPath. It's not the point.
Question: Is there any other way to do this more elegantly?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
An, 2015-08-11
@Flanker_4

Strange situation, my answer disappeared somewhere. Or I foolishly clicked Preview instead of Publish.
Your approach is damn bad.
1) A circular link is here cell.scrollView.parent = cell;
2) ScrollView climbs up the hierarchy and changes something in the child element of the parent. Very bad practice.
To comb everything, you need to do something like the following.
1) Most likely, kill the UIScrollView subclass if it does nothing but a link to the parent and the delegate handler.
2) ScrollView declare as UIScrollView and remove it from public properties. Make it private, and initialize somewhere in awakeFromNib or init After
confirming the UIScrollViewDelegate interface for the cell class
3) Declare the scrollViewDidScroll method inside the cell class, throwing out those tons of checks.

- (void)scrollViewDidScroll:(DSScrollView *)scrollView
{
      //хотя может и добавить проверку на if (scrollView==self.scrollView)
      self.myLabel.text = ...
}

Z
zzasdee, 2015-08-11
@zzasdee

Something I got excited with a strong link. Week will not increase retain count and there will be no cycle.
But in fact, how does the installation of the delegate differ from my version?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question