Ios 从原型uicollectionviewcell内的文本字段获取数据
我有一个UICollectionViewCell,它是原型单元格,其中有一个文本字段,默认情况下,它们都存在。(这就是我想要的)。我希望能够在用户在这些文本字段中输入值时检索数据和数据放入的单元格(可能是行数)。我看到了一些其他相关的问题,但似乎没有一个适合我。有什么想法吗Ios 从原型uicollectionviewcell内的文本字段获取数据,ios,objective-c,uicollectionviewcell,Ios,Objective C,Uicollectionviewcell,我有一个UICollectionViewCell,它是原型单元格,其中有一个文本字段,默认情况下,它们都存在。(这就是我想要的)。我希望能够在用户在这些文本字段中输入值时检索数据和数据放入的单元格(可能是行数)。我看到了一些其他相关的问题,但似乎没有一个适合我。有什么想法吗 @interface ViewController () { NSArray *collections, *numbers; NSMutableArray *selectedItemsArray; UICollectionV
@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;
}
实现委托方法
(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
实现委托方法
(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
好的,我已经想出了一个方法,可以解决任何有同样问题的人。需要做的是
collectionView
放在专用的UICollectionViewController
中
textField shouldendediting:textField
方法中添加以下行
nsindepath*indepath=[self.collectionView indexPathForCell:(UICollectionViewCell*)[[textField superview]superview]]代码>
nsindepath
引用,它存储textField
的indepath
。indexPath
将通过indexPath.row
给出一个行号
然后你可以随意使用这些数据:)好的,我已经想出了一种方法,可以解决任何有同样问题的人。需要做的是
collectionView
放在专用的UICollectionViewController
中
textField shouldendediting:textField
方法中添加以下行
nsindepath*indepath=[self.collectionView indexPathForCell:(UICollectionViewCell*)[[textField superview]superview]]代码>
nsindepath
引用,它存储textField
的indepath
。indexPath
将通过indexPath.row
给出一个行号
然后,您可以随意使用这些数据:)您是否试图避免将UICollectionViewCell子类化?仍然存在问题,或者您的问题是否得到了回答?您是否试图避免将UICollectionViewCell子类化?仍然存在问题,或者您的问题是否得到了回答?我以前使用标记处理此类事情,但最近我改变了要删除XPath。我检查控件的indexath,得到一个单元格,然后是文本字段。“基本上是一样的,但更一般。”马卡尔请详细说明。你是从哪里弄来的?UITextViews没有indexPath属性。在这里,阅读此问题/答案:我询问了自定义单元格中的uicontrol,这些答案值得一读,并且对我帮助很大:鬼鬼祟祟!这些方法看起来非常脆弱。。。我发现使用superview是一种不好的做法。对于此类问题,我一直使用的解决方案是将nsindepath作为关联对象附加到控件。然而,我认为这种方法比OP所要求的要高级一点。我曾经使用标记来处理这类事情,但最近我改为使用indexPath。我检查控件的indexath,得到一个单元格,然后是文本字段。“基本上是一样的,但更一般。”马卡尔请详细说明。你是从哪里弄来的?UITextViews没有indexPath属性。在这里,阅读此问题/答案:我询问了自定义单元格中的uicontrol,这些答案值得一读,并且对我帮助很大:鬼鬼祟祟!这些方法看起来非常脆弱。。。我发现使用superview是一种不好的做法。我一直在使用的解决方案是将nsindxpath作为关联对象附加到控件。然而,我认为这种方法比OP所要求的要先进一点。虽然在技术上是有效的,但我建议不要使用这种方法。对任何东西调用superview.superview都是危险的。如果苹果决定在你的UITextView和手机之间添加一个额外的视图会怎么样?@CrimsonChris,这只是暂时的修复,因为我需要一个解决方案。如果我找到一个更好更稳定的解决方案,我会发布它。虽然技术上有效,但我建议不要使用这种方法。对任何东西调用superview.superview都是危险的。如果苹果决定在你的UITextView和手机之间添加一个额外的视图会怎么样?@CrimsonChris,这只是暂时的修复,因为我需要一个解决方案。如果我找到一个更好更稳定的解决方案,我会发布它。