从原型uicollectionviewcell内的文本字段获取数据

时间:2014-04-09 12:53:40

标签: ios objective-c uicollectionviewcell

我有一个UICollectionViewCell,它是原型单元格,我有一个文本字段,默认情况下存在于所有文本字段中。 (这就是我想要的)。我希望能够在用户输入这些文本字段中的值时能够检索数据和放入数据的单元格(可能是行的编号)。我看过其他一些相关的问题,但似乎没有一个对我有用。有什么想法吗?

@interface ViewController ()
{
NSArray *collections, *numbers;
NSMutableArray *selectedItemsArray;
UICollectionViewCell *cell;
UITextView *text1Field;
NSIndexPath *indexPath1;
}
    - (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    collections = [NSArray arrayWithObjects:@"starbucks_coffee.jpg", @"thai_shrimp_cake.jpg", @"vegetable_curry.jpg", @"white_chocolate_donut.jpg", nil];
    selectedItemsArray = [NSMutableArray array];
    numbers = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return collections.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"Cell";

    cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

    UIImageView *imageview = (UIImageView *)[cell viewWithTag:100];
    text1Field = (UITextView *)[cell viewWithTag:50];

    imageview.image = [UIImage imageNamed:[collections objectAtIndex:indexPath.row]];

    collectionView.allowsMultipleSelection = YES;

    collectionView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg2.png"]];



    return cell;

}

2 个答案:

答案 0 :(得分:4)

实施委托方法(void)textViewDidChange:(UITextView *)textView 。您可以使用textView的tag属性来区分哪一个已更改。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"MySpecialCell";
    MySpecialCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[MySpecialCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.mySpecialTextView.delegate = self;
    }
    cell.mySpecialTextView.tag = indexPath.row;
    return cell;
}

- (void)textViewDidChange:(UITextView *)textView
    int rowOfTextViewThatJustChanged = textView.tag;
}

更新!
这是一种使用关联对象而不是标签的方法。

static NSString *const kIndexPathKey = @"indexPathKey";

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"MultiSelectCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        UISwitch *switchView = [[UISwitch alloc] init];
        [switchView addTarget:self action:@selector(switchValueChanged:) forControlEvents:UIControlEventValueChanged];
        cell.accessoryView = switchView;
    }

    UISwitch *switchView = (id)cell.accessoryView;
    [self setIndexPath:indexPath onSwitch:switchView];
    switchView.on = [self isIndexPathSelected:indexPath];

    id item = [self itemAtIndexPath:indexPath];
    cell.textLabel.text = safePerformSelector(item, self.itemDescriptionSelector);
    return cell;
}

- (void)switchValueChanged:(UISwitch *)sender {
    NSIndexPath *indexPath = [self indexPathOfSwitch:sender];
    [self setRowSelected:sender.isOn atIndexPath:indexPath];
    [self.delegate didSelectItem:[self itemAtIndexPath:indexPath] atIndexPath:indexPath selected:sender.isOn sender:self];
}

- (void)setIndexPath:(NSIndexPath *)indexPath onSwitch:(UISwitch *)switchView {
    [switchView setAssociatedObject:indexPath forKey:kIndexPathKey];
}

- (NSIndexPath *)indexPathOfSwitch:(UISwitch *)switchView {
    return [switchView associatedObjectForKey:kIndexPathKey];
}


@implementation NSObject (AssociatedObjects)

- (void)setAssociatedObject:(id)object forKey:(NSString *const)key {
    objc_setAssociatedObject(self, (__bridge const void *)(key), object, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (id)associatedObjectForKey:(NSString *const)key {
    return objc_getAssociatedObject(self, (__bridge const void *)(key));
}

@end

答案 1 :(得分:0)

好的,我已经找到了解决同一问题的人的方法。需要做的是

  1. 让您的collectionView专注于UICollectionViewController
  2. 添加委托方法<UITextFieldDelegate>
  3. textFieldShouldEndEditing:textField方法中添加以下行

    NSIndexPath *indexPath = [self.collectionView indexPathForCell:(UICollectionViewCell*)[[textField superview] superview]];

  4. 这里发生的事情基本上是您声明NSIndexPath引用,该引用存储indexPath的{​​{1}}。 textField将按indexPath列出行号。

    然后您可以根据需要使用该数据:)