문제

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….

도움이 되었습니까?

해결책

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.

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top