ios - 从原型(prototype) uicollectionviewcell 中的文本字段获取数据
<p><p>我有一个 UICollectionViewCell,它是原型(prototype)单元格,其中有一个文本字段,默认情况下存在于所有单元格中。 (这就是我想要的)。我希望能够当用户在这些文本字段中输入一个值以便能够检索数据和数据所在的单元格(可能是行号)。我已经看到了一些其他相关的问题,但似乎没有一个对我有用。有什么想法吗?</p>
<pre><code>@interface ViewController ()
{
NSArray *collections, *numbers;
NSMutableArray *selectedItemsArray;
UICollectionViewCell *cell;
UITextView *text1Field;
NSIndexPath *indexPath1;
}
- (void)viewDidLoad
{
;
// Do any additional setup after loading the view, typically from a nib.
collections = ;
selectedItemsArray = ;
numbers = ;
}
- (void)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 = ;
UIImageView *imageview = (UIImageView *);
text1Field = (UITextView *);
imageview.image = ];
collectionView.allowsMultipleSelection = YES;
collectionView.backgroundColor = ];
return cell;
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>实现委托(delegate)方法<code>(void)textViewDidChange:(UITextView *)textView
</code>。您可以使用 textView 的 tag 属性来区分哪个被更改。</p>
<pre><code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"MySpecialCell";
MySpecialCell *cell = ;
if (!cell) {
cell = [ initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.mySpecialTextView.delegate = self;
}
cell.mySpecialTextView.tag = indexPath.row;
return cell;
}
- (void)textViewDidChange:(UITextView *)textView
int rowOfTextViewThatJustChanged = textView.tag;
}
</code></pre>
<p><em>更新!</em><br/>
这是一种使用关联对象而不是标签的方法。 </p>
<pre><code>static NSString *const kIndexPathKey = @"indexPathKey";
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"MultiSelectCell";
UITableViewCell *cell = ;
if (!cell) {
cell = [ initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UISwitch *switchView = [ init];
;
cell.accessoryView = switchView;
}
UISwitch *switchView = (id)cell.accessoryView;
;
switchView.on = ;
id item = ;
cell.textLabel.text = safePerformSelector(item, self.itemDescriptionSelector);
return cell;
}
- (void)switchValueChanged:(UISwitch *)sender {
NSIndexPath *indexPath = ;
;
atIndexPath:indexPath selected:sender.isOn sender:self];
}
- (void)setIndexPath:(NSIndexPath *)indexPath onSwitch:(UISwitch *)switchView {
;
}
- (NSIndexPath *)indexPathOfSwitch:(UISwitch *)switchView {
return ;
}
@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
</code></pre></p>
<p style="font-size: 20px;">关于ios - 从原型(prototype) uicollectionviewcell 中的文本字段获取数据,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/22963192/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/22963192/
</a>
</p>
页:
[1]