0

我为我的工作流程的一部分制作了一个测试应用程序。我想要实现的是一种向用户展示他们正在为 Word 风格的游戏输入的内容的奇特方式。

目前这是方法,但可能有更简单/更好的路线。我有一个UITextField未向用户显示的,键盘显示在viewDidLoad. 我想要发生的是每次在键盘上按下一个字母时,都会将显示大写字母的新拼贴添加到上方的屏幕区域,即“W”,然后另一个字母将意味着添加了另一个拼贴,即“I”旁边以前的...

我已经设置了一个UICollectionView带有标签的自定义单元格,仅此而已。VC 是UICollectionView. UITextField也有其设置delegateself(VC)。

我无法弄清楚如何让瓷砖(单元格)创建每个字母。

#pragma mark - 
#pragma mark - Key board delegate methods
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSLog(@"%s",__PRETTY_FUNCTION__);
NSString *lastLetterTyped = [textField.text substringFromIndex:[textField.text length] - 1];

[self.wordArray addObject:lastLetterTyped];
[self.tileCollectionView reloadData];

    return YES;
}

#pragma mark - 
#pragma mark - Collection View Data Source Methods
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 3;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    // we're going to use a custom UICollectionViewCell, which will hold an image and its label
    //
    WordCVCell *cell = [cv dequeueReusableCellWithReuseIdentifier:kCellID forIndexPath:indexPath];

    // make the cell's title the actual NSIndexPath value
    NSString *lastLetter = [self.typedWord substringFromIndex:[self.typedWord length] - 1];
    cell.label.text = lastLetter;

    return cell;
}
4

2 回答 2

0

您需要有一个 NSMutableArray,每次用户键入一个字符时您都将添加它。为此,您需要将控制器连接到 UITextfieldDelegate。之后,每次添加到此数组时,您都需要调用 [collectionView reloadData] 并且您的项目数将为 [myMutableCharacterArray count];

所以基本上每次用户键入一个字母时,将它添加到一个可变数组并调用 [collectionView reload data] 来刷新collectionview。

于 2013-08-13T00:16:44.240 回答
0

您的

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 3;
}

应该读作

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    [self.wordArray count];
}

这将触发以下与数组中的对象相同的次数。

-(UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    // we're going to use a custom UICollectionViewCell, which will hold an image and its label
    //
    WordCVCell *cell = [cv dequeueReusableCellWithReuseIdentifier:kCellID forIndexPath:indexPath];

    // make the cell's title the actual NSIndexPath value
    NSString *lastLetter = [self.wordArray objectAtIndex:indexPath.row];
    cell.label.text = lastLetter;

    return cell;
}
于 2013-08-13T12:50:20.773 回答