Monday, 12 August 2013

Using a CollectionView to add a tile effect when the user types

Using a CollectionView to add a tile effect when the user types

I have a test application I have made for a part of my workflow. What I am
trying to achieve is a fancy way of showing the user what they are typing
for a Word styled game.
At the moment this is the approach but there could be an easier/better
route. I have a UITextField which is not shown to the user and the
keyboard is shown on viewDidLoad. What I am trying to have happen is each
time a letter is pressed on the keyboard a new Tile showing the letter
capitalised is added to the screen area above i.e. 'W', then another
letter would mean another tile added i.e. 'I' next to the previous...
I have setup a UICollectionView and custom cell with a label in, that is
all. The VC is the dataSource of the UICollectionView. The UITextField
also has its delegate set to the self (the VC).
I cannot work out how to have the tiles (cells) created each letter.
#pragma mark -
#pragma mark - Key board delegate methods
-(BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString
*)string {
NSLog(@"%s",__PRETTY_FUNCTION__);
self.typedWord = textField.text;
// NSInteger idx = self.tileCollectionView.visibleCells.count + 1;
// [self.tileCollectionView insertItemsAtIndexPaths:idx];
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;
}

No comments:

Post a Comment