Answer the question
In order to leave comments, you need to log in
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
cell.scrollView.parent = cell;
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if ([scrollView isKindOfClass:[MyScrollView class]])
{
if (scrollView.parent)
{
MyTableViewCell *cell = scrollView.parent;
cell.myLabel.text = ...
}
}
}
Answer the question
In order to leave comments, you need to log in
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 = ...
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question