• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 从原型(prototype) uicollectionviewcell 中的文本字段获取数据

[复制链接]
菜鸟教程小白 发表于 2022-12-13 07:27:35 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

@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)collectionViewUICollectionView *)collectionView numberOfItemsInSectionNSInteger)section
{
    return collections.count;
}
- (UICollectionViewCell *)collectionViewUICollectionView *)collectionView cellForItemAtIndexPathNSIndexPath *)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;

}



Best Answer-推荐答案


实现委托(delegate)方法(void)textViewDidChangeUITextView *)textView 。您可以使用 textView 的 tag 属性来区分哪个被更改。

- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)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)textViewDidChangeUITextView *)textView
    int rowOfTextViewThatJustChanged = textView.tag;
}

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

static NSString *const kIndexPathKey = @"indexPathKey";

- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)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 actionselector(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

关于ios - 从原型(prototype) uicollectionviewcell 中的文本字段获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22963192/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap