Setting up Hansel Index for dynamic views
Dynamic Views
A UI element is said to be dynamic when it can change its index within its parent ViewGroup.
For example, due to personalization, recommendation1 can be placed at index 2 for a User A whereas the same recommendation1 can be placed at index 5 for another User B. Then the UI element containing recommendation1 data is dynamic in nature.
Prerequisite: Support for Dynamic views via below method is available from iOS SmartechNudge SDK v8.5.26 onwards
To ensure that a nudge doesn't lose its context in such a scenario and is always placed on the correct view, provide a custom identifier to the UI elements using the following method:
Method:
- (BOOL)setHanselIndexForView:(UIView _ Nonnull)view withIndex:(NSString _Nonnull)index;
Where,
view: UI element that is dynamic in nature
index: an unique identifier for the dynamic UI element
UIView *view = [[UIView alloc] init]; //view which is dynamic in nature.
NSString *hanselIndexKey = @"uniqueKeyName"; //Unique value which is set for a view.
[Hansel setHanselIndexForView:view withIndex:hanselIndexKey];
let view = UIView(frame: CGRect())
let hanselIndexKey = "uniqueKeyName"
Hansel.setHanselIndexFor(view, withIndex: hanselIndexKey)
Where,
view: UI element that is dynamic in nature
hanselIndex: an identifier for the dynamic UI element
If your app has dynamic content populated in the UIScrollView, UITableView, UICollectionView and you want to set up nudges on such views (e.g. UITableViewCell, UICollectionViewCell, UIView, UIButton, etc.), then follow the below steps to set the Hansel Index.
Important
- Hansel indices are supported only for views that are immediate children of UITableView, UICollectionView, and UIScrollView.
- Hansel index should be set only after the view is attached to its parent container
While Using UIScrollView:
Set Hansel index key (unique) to all child views of UIScrollView once they are attached.
Let’s take an example.
UIScrollView has 6 different button views as children whose index can be changed depending on any condition.
override func viewDidLoad() {
super.viewDidLoad()
self.showNames = ["Button 1","Button 2","Button 3","Button 4","Button 5","Button 6"]
var yAxis = 20;
for name in self.showNames {
let button = UIButton(frame: CGRect(x: 0, y: yAxis, width: 100, height: 40))
button.setTitle(name, for: .normal)
button.backgroundColor = .white
button.setTitleColor(.black, for: .normal)
button.addTarget(self, action: #selector(self.buttonTapped), for: .touchUpInside)
self.scrollView.addSubview(button)
Hansel.setHanselIndexFor(button, withIndex: "\(name)")
yAxis = yAxis + 200;
}
}
While using UITableView / UICollectionView:
Set Hansel index key (unique) to child views of UITableView / UICollectionView as given below.
Let’s take an example:
UITableView / UICollectionView has x number of UITableViewCell / UICollectionViewCell which position can be changed depending on any condition.
self.showNames = ["House M.D","Friends","Stranger Things","Seinfeld","Dexter","Westworld","Sacred Games","Black Mirror","Rick and Morty","Better call saul"]
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "dynamicCellIdentifier", for: indexPath) as!DynamicViewTableViewCell
Hansel.setHanselIndexFor(cell, withIndex: "\(self.showNames[indexPath.row])")
cell.title.text = "\(self.showNames[index])"
return cell
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionVw.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! DynamicViewCollectionViewCell
Hansel.setHanselIndexFor(cell, withIndex: "\(self.showNames[indexPath.row])")
cell.title.text = "\(self.showNames[indexPath.row])"
return cell
}
Updated 6 months ago