【问题标题】:uicollectionview cells not showed after updates as per requirement after reload data重新加载数据后根据要求更新后未显示 uicollectionview 单元格
【发布时间】:2014-05-14 14:14:51
【问题描述】:

我有一个显示在集合视图中的产品目录,用户可以更改新产品的类别子类别,它们是完美地填充在集合视图中。

现在用户可以选择任何特定产品的数量 1、2、3 或更多来购买。 为此,我在集合视图单元格中设置了 UIButton 操作,以在单元格内的 UITextfield 中获取用户输入。

一切都完美无缺,实际上我有不同数量的产品,每个类别明智的,大部分时间我必须滚动才能在收藏视图中查看产品。

当设置任何产品或多个产品的数量时,它们在每个 Cell 中都是完美的。

** 更新问题:**

如果我更改类别或子类别以在 collection view 中查看其他 products 现在此条件在 uibutton 操作方法和在 cellForItemAtIndexPath 方法中。 这是问题:

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

我的工作方式:

自定义 CollectionView 类

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

实现

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

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

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

注意: Code、indexPaths、updatedCodes 和 updatedQty 是 NSMutableArrays

任何有关此问题的建议/帮助将不胜感激。

等待快速帮助...... :(

【问题讨论】:

    标签: ios iphone objective-c ipad uicollectionview


    【解决方案1】:

    问题来了

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

    当这个数组有记录时,它会进入这个 IF 语句,并会跳过你实际设置 cell.txt1.text = @"0" 的 else 语句。最简单的解决方法是将下一行代码放在 for 循环之前

    cell.txt1.text = @"0";
    

    这样

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

    【讨论】:

    • 问题是一样的……实际上这个文本值,例如一个产品的“4”和另一个产品的“3”也显示了其他一些单元格。:(
    • 你为什么在return cell;之前调用这个[cell.contentView addSubview:cell.txt1];,这个对象是在哪里创建的?
    • txt1 在单元格的自定义类中创建。我认为这里是在返回单元格之前在单元格内显示 uitextfiled 的地方;
    • 删除 else { cell.txt1.text = @"0"; } 并将 cell.txt1.text = @"0"; 放在 if ([indexPaths count] &gt; 0) 之前
    • 在类别或子类别上更改 CollectionView 已重新加载,但如果我再次回到同一类别,则所有文本字段再次为“0”
    【解决方案2】:

    回答你自己的问题可能不好,但我发现了我的问题并自己解决了

    所以在这里为其他遇到同样问题的人发布问题......。

    这就是我在代码中所做的全部更改

    在视图控制器实现中

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

    有了这个

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

    按钮操作方法的变化与

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

    【讨论】:

      猜你喜欢
      • 2014-11-07
      • 2013-05-19
      • 1970-01-01
      • 1970-01-01
      • 2015-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多