Вопрос

i have a products catalogue that is shown in collection view, User may change the category or sub category for new products and they are populating in collection view perfectly.

now user may choose the quantity of any specific product in numbers as 1, 2, 3 or more to buy. for this i set UIButton action in collection view cell to take users input in UITextfield inside Cell.

all works perfectly, actually i have different number of products as per Category wise, and most of the time i have to scroll to see products in collection view.

when ever the quantity of any product or more than one products are set they are just perfect as i want in each Cell.

** Updated Problem:**

if i change the category or sub category to see other products in collection view now this condition never satisfies in both uibutton action method and in cellForItemAtIndexPath method. here is problem:

if ([code objectAtIndex:indexPath.row] == [updatedCodes objectAtIndex:i])
{} // this condition is creating problem on category or sub category change. 

that how i am working:

Custom CollectionView class

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

implementation

@implementation MyCell
@synthesize btn1, 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 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;
}

in ViewController implementation

- (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.txt1.text = @"0";
for (i = 0; i < [updatedCodes count]; i++)
{
    if ([code objectAtIndex:indexPath.row] == [updatedCodes objectAtIndex:i])
    {
        cell.txt1.text = [updatedQty objectAtIndex:i];
     }
}
[cell.contentView addSubview:cell.txt1];
return cell;
}

the UIButton Action Method is

-(void)btnPlus:(id)sender event:(id)event
{
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:myCollection];
btnIndex = [myCollection indexPathForItemAtPoint: currentTouchPosition];
MyCell *cell = (MyCell*)[myCollection cellForItemAtIndexPath:btnIndex];
NSString *newCode = [code objectAtIndex:btnIndex.row];
NSString *newQty = cell.txt1.text;
if ([updatedCodes containsObject:newCode])
{
    for (i = 0; i < [updatedCodes count]; i ++)
    {
        if ([updatedCodes objectAtIndex:i] == newCode)
        {
            qnty = [newQty integerValue];
            qnty = qnty + 1;
            cell.txt1.text = [NSString stringWithFormat:@"%d", qnty];
            newQty = cell.txt1.text;
            [updatedQty replaceObjectAtIndex:i withObject:newQty];
        }
    }
    if (![indexPaths containsObject:btnIndex])
    {
        [indexPaths addObject:btnIndex];
    }
}
else
{
    [updatedCodes addObject:newCode];
    qnty = [newQty integerValue];
    qnty = qnty + 1;
    cell.txt1.text = [NSString stringWithFormat:@"%d", qnty];
    newQty = cell.txt1.text;
    [updatedQty addObject:newQty];
    if (![indexPaths containsObject:btnIndex])
    {
        [indexPaths addObject:btnIndex];
    }
}
[myCollection reloadItemsAtIndexPaths:indexPaths];
}

NOTE: Code, indexPaths ,updatedCodes and updatedQty are NSMutableArrays

Any suggestion / Help about this problem will be greatly appreciated.

waiting for swift help….. :(

Это было полезно?

Решение 2

answering your own question may be not good but i figured out my problem and resolve it myself

so posting here the issue for some one else who is suffering in same problem….

this is all i change in my code

in view controller implemention

if ([code objectAtIndex:indexPath.row] == [updatedCodes objectAtIndex:i])
{
    cell.txt1.text = [updatedQty objectAtIndex:i];
}

with this

if ([[code objectAtIndex:indexPath.row] isEqualToString:[updatedCodes objectAtIndex:i]])
    {
        cell.txt1.text = [updatedQty objectAtIndex:i];
    }

and same change in Button Action method as

if ([newCode isEqualToString:[updatedCodes objectAtIndex:i]])
{
    // do something
}

Другие советы

The problem is here

if ([indexPaths count] > 0)
{
    for (i = 0; i < [updatedCodes count]; i++)
    {
        if ([code objectAtIndex:indexPath.row] == [updatedCodes objectAtIndex:i])
        {
            cell.txt1.text = [updatedQty objectAtIndex:i];
        }
    }
}

As when this array has records it will go in within this IF statement and will skip the else statement where you are actually setting your cell.txt1.text = @"0". Easiest fix would be to put next line of code just before the for loop

cell.txt1.text = @"0";

Like this

if ([indexPaths count] > 0)
    { 
        cell.txt1.text = @"0";
        for (i = 0; i < [updatedCodes count]; i++)
        {
            if ([code objectAtIndex:indexPath.row] == [updatedCodes objectAtIndex:i])
            {
                cell.txt1.text = [updatedQty objectAtIndex:i];
            }
        }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top