Reputation: 73
I have a CollectiveView with a variety of elements inside. I have managed to add a button and have attached an action to it. On pressing the button, I want that call to perform a certain activity, like change background colour for instance...
At the moment, this activity is being applied to all the cells. Any suggestions appreciated.
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
cell.label.text = [NSString stringWithFormat:@"%ld",(long)indexPath.item];
cell.trackLbl.text = [NSString stringWithFormat:@"Track %ld", (long)indexPath.row + 1];
cell.trackTitle.image = [UIImage imageNamed:[[self.util trackTitleImages] objectAtIndex:indexPath.row]];
[cell.record addTarget:self
action:@selector(recording:)
forControlEvents:UIControlEventTouchUpInside];
if (isRecording) {
[cell.spinnerWidget beginCompletion:[UIColor redColor]];
}
else {
[cell.spinnerWidget beginCompletion:[UIColor colorWithRed:0.45f green:0.77f blue:0.87f alpha:1.0f]];
}
[self.view setNeedsDisplay];
return cell;
}
So as you can see, at the moment I have the 'recording' target assigned to my cell uibutton. From this, I can also get the index of the cell. My work around was to have a bool (isRecording) and reloadData. But obviously this gets applied to all cells.
Upvotes: 0
Views: 200
Reputation: 4100
If the button is in the cell use superview. For example in the case of background color
@IBAction func buttonPressed(sender: AnyObject) {
yourButton.superview.backgroundColor = UIColor(your values here)
}
Upvotes: 1