1

Initialise two uibutton and one uitextfield in uicollectionviewcell class

then on button tap inside the cell got the index path, now stuck on this point how to update text in textfield on button tap inside collectionviewcell

buttons are (+) Plus and (-) Minus , Actually i have to tele input from user as quantity how much they need against any product that are shown in my collection view.

this what i am doing but whit no success

custom collection view class

@interface MyCell  : UICollectionViewCell
@property (retain, nonatomic) UIButton *btn1;
@property (retain, nonatomic) UIButton *btn2;
@property (retain, nonatomic) UITextField *txt1;
@end

Implementation

@implementation MyCell
@synthesize btn1, btn2, txt1;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{        
    CGRect btn1Rect = CGRectMake(190, 230 , 35 , 35);
    btn1 = [[UIButton alloc] initWithFrame:btn1Rect];
    btn1.tag=11;
    [btn1 setBackgroundImage:[UIImage imageNamed:@"BtnPlus.png"] forState:UIControlStateNormal];
    //btn1.layer.borderWidth = 1.0f;

    CGRect btn2Rect = CGRectMake(105, 230 , 35 , 35);
    btn2 = [[UIButton alloc] initWithFrame:btn2Rect];
    btn2.tag=12;
    [btn2 setBackgroundImage:[UIImage imageNamed:@"BtnMinus.png"] forState:UIControlStateNormal];
    //btn2.layer.borderWidth = 1.0f;

    CGRect txt1Rect = CGRectMake(143, 230 , 45 , 30);
    txt1 = [[UITextField alloc] initWithFrame:txt1Rect];
    txt1.tag=13;
    txt1.font=[UIFont fontWithName:@"Superclarendon" size:18];
    txt1.textAlignment = NSTextAlignmentCenter;
    txt1.textColor = [UIColor blueColor];
    //txt1.layer.borderWidth = 1.0f;
   }
 return self;
 }
 @end

in main class

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CellID" forIndexPath:indexPath];

[[cell btn1] addTarget:self action:@selector(btnPlus:event:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:cell.btn1];

[[cell btn2] addTarget:self action:@selector(btnMinus:event:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:cell.btn2];

[cell.contentView addSubview:cell.txt1];
return cell;
}

the handler Methods code is

-(void)btnPlus:(id)sender event:(id)event
{
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:myCollection];
NSIndexPath *indexPath = [myCollection indexPathForItemAtPoint: currentTouchPosition];
NSLog(@"%@", indexPath);

 }

thats all i have done ,,, now please help me to resolve or figure out my problem.

just want to take input in textfield in specific cell and then get this text to save….

M Zubair Shamshad
  • 2,741
  • 3
  • 23
  • 45

2 Answers2

1

You should create an XIB for UICollectionviewCell. That will be an easier approach. Take IBOutlets for buttons and textfield. You have already set them as properties. In your method you have got the indexpath. So you are very near to the solution. You can get the UICollectionViewCell using the method UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];

Then you can access properties of these cells.

Yogi
  • 3,578
  • 3
  • 35
  • 56
  • in a XIB file a have set a collection view, how can i set the each and every cell IBOutlets separately…..???/ – M Zubair Shamshad May 13 '14 at 10:55
  • You can create a class inheriting `UICollectionViewCell`. Take an XIB. In that xib remove view object and add UICollectionViewCell object. Change the class of this UICollectionViewCell to your custom cell class. The process is very similar to that of creating xib for UITableViewCell. Refer http://stackoverflow.com/questions/14466959/create-uicollectionviewcell-subclass-with-xib for more information. BTW you can get cell using indexpath in your current implementation too as quoted in my answer. – Yogi May 13 '14 at 11:09
  • You can add tag values to each TextField you can achieve this. – PREMKUMAR May 13 '14 at 11:12
  • Set tags for both UIbutton and UItextfield – PREMKUMAR May 13 '14 at 11:14
  • Set tags for both UIbutton and UItextfield – PREMKUMAR May 13 '14 at 11:15
  • I think Zaibi has already done all work. Now there is no need to set tags again. – Yogi May 13 '14 at 11:26
  • the approach @Yogi suggested is too much confusing… i didn't get the point why separate xib is necessary , i am working like that and getting images and labels inside collection view cell and all that woks perfectly – M Zubair Shamshad May 13 '14 at 11:34
  • Hello Zaibi, As I already said you can do it without creating XIB. As you have already given reference names to your controls line btn1, btn2, txt1 etc. so you just need to access cell using the method which I have mentioned in my answer and then you can access the textfield on that cell as we normally access any property using `cell.txt1`. I hope its not confusing now. – Yogi May 13 '14 at 11:42
  • i already tried it but the error is : proprety txt1 not found on object of type uicollectionviewcell – M Zubair Shamshad May 13 '14 at 12:07
  • @Yogi what should i do now? – M Zubair Shamshad May 13 '14 at 12:07
  • 1
    You should use `MyCell *cell = (MyCell*)[collectionView cellForItemAtIndexPath:indexPath];` as you have a customized cell class. Then your error should go. – Yogi May 13 '14 at 12:17
  • but with the problem i just updated one or two cells but there are random cells are updated with that text now… ??? what is this problem now??? – M Zubair Shamshad May 13 '14 at 12:27
  • This is because UICollectionView reuses the cells. Check your code in itemforRowAtIndexPath. You should set the text of cell everytime the function is called. – Yogi May 13 '14 at 12:48
  • The datasource function `collectionView: cellForItemAtIndexPath:`of collection view. You should check the code there and set the textfield text there also. – Yogi May 14 '14 at 04:34
0

When you click the button you will store the indexpath value to global string. when you hit the button you reload collectionView then you display global indexpath string values to TextField.

PREMKUMAR
  • 8,283
  • 8
  • 28
  • 51
  • using this way each cell's text field will be updated with the text – M Zubair Shamshad May 13 '14 at 11:08
  • Use Tag Value to EachText field like this "[cell.deleteButton setTag:indexPath.row]; [cell.deleteButton addTarget:self action:@selector(deletebuttonClicked:) forControlEvents:UIControlEventTouchDown]; cell.deleteButton.hidden=NO; " – PREMKUMAR May 13 '14 at 11:14