简体   繁体   English

重新加载数据后,按要求更新后未显示uicollectionview单元格

[英]uicollectionview cells not showed after updates as per requirement after reload data

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. 现在,用户可以按1、2、3或更多的数量选择任何特定产品的数量进行购买。 for this i set UIButton action in collection view cell to take users input in UITextfield inside Cell. 为此,我在集合视图单元格中设置了UIButton动作,以使用户在单元格内的UITextfield中输入。

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. 如果我现在更改类别或子类别以在集合视图中查看其他产品 ,则此条件永远不会同时满足uibutton操作方法和cellForItemAtIndexPath方法。 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 自定义CollectionView类

@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 在ViewController实现中

- (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 UIButton操作方法是

-(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 注意:代码,indexPaths,updatedCodes和updatedQty是NSMutableArrays

Any suggestion / Help about this problem will be greatly appreciated. 关于这个问题的任何建议/帮助将不胜感激。

waiting for swift help….. :( 等待迅速的帮助….. :(

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". 当该数组具有记录时,它将进入该IF语句,并跳过您实际设置cell.txt1.text = @“ 0”的else语句。 Easiest fix would be to put next line of code just before the for loop 最简单的解决方法是将下一行代码放在for循环之前

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];
            }
        }
}

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM