[英]UICollectionView datasource method not called after reload data
[英]uicollectionview cells not showed after updates as per requirement after reload data
我有一个在收藏夹视图中显示的产品目录,用户可以更改新产品的类别或子类别 ,并且它们可以完美地填充在收藏夹视图中 。
现在,用户可以按1、2、3或更多的数量选择任何特定产品的数量进行购买。 为此,我在集合视图单元格中设置了UIButton动作,以使用户在单元格内的UITextfield中输入。
一切运行正常,实际上,根据类别,我有不同数量的产品,大多数时候,我不得不滚动以在收藏夹视图中查看产品。
每当确定任何产品的数量或一种以上产品的数量时,它们都是我想要的完美的每个单元 。
**更新的问题:**
如果我现在更改类别或子类别以在集合视图中查看其他产品 ,则此条件永远不会同时满足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];
}
注意:代码,indexPaths,updatedCodes和updatedQty是NSMutableArrays
关于这个问题的任何建议/帮助将不胜感激。
等待迅速的帮助….. :(
问题在这里
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];
}
}
}
回答您自己的问题可能不太好,但是我想出了自己的问题并自己解决了
所以在这里发布这个问题给其他遭受同样问题困扰的人。
这就是我在代码中所做的全部更改
在视图控制器实现中
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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.