Problems with dynamic actions in an UICollectionView

Mobile Testing, Android App Testing.
JToelstede
Posts: 55
Joined: Fri May 24, 2013 12:57 pm

Problems with dynamic actions in an UICollectionView

Post by JToelstede » Mon Jan 27, 2014 1:28 pm

Hi,

in our iOS-app we have an UICollectionView inserted. Each cell of the UICollectionView contains an image and a delete button. The action for the delete button is set dynamically with the method call "addTarget:action:forControlEvents:".
Without the Ranorex-Agent the app works fine and by tapping on any button the dynamic action is called.
When we build in the Agent and tap on any delete button the action isn't called.

Could it be that this functionality is actually not supported by Ranorex?

I use Ranorex 4.1.3 with the libRxAutomationUni_413.a.
The test device is an iPad with iOS 7.0.4.

Thanks a lot.
Best Regards,
Joerg

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Problems with dynamic actions in an UICollectionView

Post by Support Team » Wed Jan 29, 2014 9:50 am

Hi,

I just tried a to reconstruct the scenario to described. Unfortunately I wasn't able to reproduce the problem. Based on your description I added the following code to one of our test apps that contain a UICollectionView into the corresponding CollectionViewController.

First I register a custom cell NIB (that contains a UIButton):

Code: Select all

- (void)viewDidLoad
{
    [super viewDidLoad];
	 // Do any additional setup after loading the view.
    
    UINib* cellNib = [UINib nibWithNibName:@"RanorexCollectionViewCell" bundle:nil];
    [self.collectionView registerNib:cellNib forCellWithReuseIdentifier:@"Rx"];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView reloadData];
}

When creating a cell, a dynamic action will be added ot the button via 'addTarget:action:forControlEvents:':

Code: Select all

-(UICollectionViewCell*)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    RanorexCollectionViewCell* cell = [cv dequeueReusableCellWithReuseIdentifier:@"Rx" forIndexPath:indexPath];
    
    //Add dynamic action    
    [cell.button addTarget:self action:@selector(testDynamicButtonHandler:) forControlEvents:UIControlEventTouchUpInside];
    
    return cell;
}
And the dynamic action itself is implemented as:

Code: Select all

-(void)testDynamicButtonHandler:(id)sender
{
    NSLog(@"Dynamic action invoked");
}
When I click the button's in the UICollectionViewCells in the debug output pane the message 'Dynamic action invoked' will be printed for every click.

So I need further information to track down the problem you described:
  • Can you provide a small sample application that can reproduce this problem? (this would probably be the most efficient way; You can also send it to [email protected] if you don't wan't to attach it in the forum)
  • Can you see any significant differences between the code posted in this reply and your own code? If so, could describe them in more detail.
  • Can you post your relevant code parts for the cell initialisation etc. or mail them to [email protected]
Best Regards,
Philipp,
Ranorex Team

JToelstede
Posts: 55
Joined: Fri May 24, 2013 12:57 pm

Re: Problems with dynamic actions in an UICollectionView

Post by JToelstede » Wed Jan 29, 2014 9:01 pm

Hi Philipp,

first of all many thanks for your work.

I compared your code with ours and found the following diffence which is blockig the action call when the Ranorex-Agent is installed.
We only add the dynamic action if the button has no targets.

Code: Select all

//add dynamic action
    if (cell.button.allTargets.count == 0)
        [cell.button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
When I change this statement (remove the if-statement) in this way you add the dynamic action it is working with and without the agent.

Do you know why?

I added the xCode project of our sample-app to this post. You only have to add the ranorex-agent-lib.

Many thanks and best regards,

Joerg
You do not have the required permissions to view the files attached to this post.

JToelstede
Posts: 55
Joined: Fri May 24, 2013 12:57 pm

Re: Problems with dynamic actions in an UICollectionView

Post by JToelstede » Thu Jan 30, 2014 12:24 pm

Hi Philipp,

today I debugged the project and now I could answer my last question by myself.
JToelstede wrote: Do you know why?
The agent adds a target to each button so that the agent get a notification when the button is pressed or an action is called and because of our if -statement we could not add our action. Without the agent there is no target before and that is why we could add our action.
We still removed the if-statement in our code and it is working fine.

Many thanks for your help.

Best regards,
Joerg