Getting data from a textfield inside a prototype uicollectionviewcell

前端 未结 2 1538
北恋
北恋 2020-12-21 12:45

I have a UICollectionViewCell which is prototype cell, I have a textfield in it, which by default present in all of them. (which is what I want). I want to be able to when t

相关标签:
2条回答
  • 2020-12-21 13:03

    Implement the delegate method (void)textViewDidChange:(UITextView *)textView . You could use the tag property of the textView to distinguish which one was changed.

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

    Update!
    Here's an approach that uses associated objects instead of tags.

    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
    
    0 讨论(0)
  • 2020-12-21 13:04

    Ok, I have figured out a way for anyone with the same issue. What needs to be done is

    1. Have your collectionView in a dedicated UICollectionViewController.
    2. Add the delegate method <UITextFieldDelegate>
    3. In your textFieldShouldEndEditing:textField method add the following line

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

    What is happening here is basically you declaring a NSIndexPath reference which stores the indexPath of the textField. The indexPath will give out a row number by indexpath.row.

    Then you can use that data however you want :)

    0 讨论(0)
提交回复
热议问题