【问题标题】:update and get text from uitextfield inside uicollectionciewcell从 uicollectionciewcell 内的 uitextfield 更新和获取文本
【发布时间】:2014-06-30 23:39:28
【问题描述】:

在uicollectionviewcell类中初始化两个uibutton和一个uitextfield

然后点击单元格内的按钮获得索引路径,现在停留在这一点上,如何在collectionviewcell内的按钮点击时更新文本字段中的文本

按钮是 (+) 加号和 (-) 减号,实际上我必须从用户那里远程输入他们对我的收藏视图中显示的任何产品的需求量。

这是我正在做的,但没有成功

自定义集合视图类

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

实施

@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

在主类中

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

处理程序方法代码是

-(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);

 }

这就是我所做的一切,,,现在请帮助我解决或找出我的问题。

只想在特定单元格的文本字段中输入,然后保存此文本……。

【问题讨论】:

    标签: ios iphone objective-c ipad uicollectionview


    【解决方案1】:

    您应该为 UICollectionviewCell 创建一个 XIB。这将是一种更简单的方法。 将 IBOutlets 用于按钮和文本字段。您已经将它们设置为属性。 在您的方法中,您已经获得了索引路径。所以你非常接近解决方案。可以使用UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];方法获取UICollectionViewCell

    然后您可以访问这些单元格的属性。

    【讨论】:

    • 在 XIB 文件中设置了一个集合视图,我如何分别设置每个单元格 IBOutlets.....???/
    • 你可以创建一个继承UICollectionViewCell的类。拿一个XIB。在那个 xib 中删除视图对象并添加 UICollectionViewCell 对象。将此 UICollectionViewCell 的类更改为您的自定义单元格类。该过程与为 UITableViewCell 创建 xib 的过程非常相似。请参阅stackoverflow.com/questions/14466959/… 了解更多信息。顺便说一句,您也可以在我的回答中引用当前实现中的 indexpath 来获取单元格。
    • 您可以在每个 TextField 中添加标签值来实现这一点。
    • 为 UIbutton 和 UItextfield 设置标签
    • 您应该使用MyCell *cell = (MyCell*)[collectionView cellForItemAtIndexPath:indexPath];,因为您有一个自定义的单元格类。那么你的错误应该消失了。
    【解决方案2】:

    当您单击按钮时,您会将 indexpath 值存储到全局字符串。当您点击重新加载 collectionView 的按钮时,您会将全局 indexpath 字符串值显示到 TextField。

    【讨论】:

    • 使用这种方式,每个单元格的文本字段都将更新为文本
    • 对 EachText 字段使用标记值,如下所示“[cell.deleteButton setTag:indexPath.row]; [cell.deleteButton addTarget:self action:@selector(deletebuttonClicked:) forControlEvents:UIControlEventTouchDown]; 单元格。 deleteButton.hidden=NO; "
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多