Allow user inteaction with UITextField in a CollectionView Cell - ios

I've written an IOS App which uses UITableViewCells and I am converting it to use UICollectionView Cells.
It's working OK at present, but I can't figure out how to allow user to update a UITextFied which is situated within the UICollectionView Cell.
I've managed to do this with UITableView Cells.
I've searched for answers to my problem, but can't find anything that uses a UITextField within a UICollectionView Cell.
Help in pointing me in the right direction would be much appreiciated.

Can you show us some of the code you are using. I have used UITextFields in UICollectionViews without a problem, so I am not sure what your question is.
Edit:
First add a tag in storyboard or programatically to your textfield for this example we will use a tag of 2
Then every time you want to modify the textfield simply call
UITextField *myText = (UITextField *)[cell viewWithTag:2];

You can declare and IBOutlet for the UITextField and then get it lkike that:
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:some_index_path];
UITextField *myTextField = cell.textFieldOutlet;

Try this, It's work for me,
First you have to set tag to UITextFied
Add below line of code :-
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
UITextField *addCaptionTxt=(UITextField *)[cell viewWithTag:101];
[cell addSubview:addCaptionTxt];
return cell;
}

Related

viewWithTag: returns nil after reloadData

I have tableView with 6 rows made with a custom cell.
The custom cell has a UILabel and a UISwitch.
In the method "cellForRowAtIndexPath:" I have:
static NSString *CellIdentifier = #"optionsCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
UISwitch *optionsSwitch = (UISwitch *)[cell viewWithTag:300];
UILabel *optionsLabel = (UILabel *) [cell viewWithTag:200];
NSLog(#"%#", optionsSwitch);
NSLog(#"%#", optionsLabel);
When the tableView is first instantiated (from the storyboard) I correctly get the two objects (I'm adding the result only for the first row of the table view):
but when I send the reloadData method to the tableView (the UISwitch status can be changed programmatically so I update the tableview before displaying it) I correctly get the UILabel while UISwitch is nil:
Why the UILabel works and the UISwitch not ?
UISizeClasses is not enabled, as suggested in another post regarding this problem.
Thanks, Corrado
You change the tag of the switch somewhere else in the code, probably so that when the switch is changed you can get the row number from it.
Really you should have a custom cell which has outlets to the views so you don't need to use tags at all, and the cell should handle the switch changes to update the model or call some callback to the view controller.

UILabel does not render in prototype cell

I have a UIViewController that contains a UITableView. In my table view I have a prototype cell in which I want to place custom labels. I cannot get any of my custom labels to render in the cell. The datasource and tableview delegates are connected and functioning properly. For my first attempt, I dragged a UILabel control onto my prototype cell. I set its tag to 1000. Here is my code to populate the cells:
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellID = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
UILabel *customLbl = (UILabel*)[cell viewWithTag:1000];
if(indexPath.section == 0){
[cell.textLabel setText:[rData.currentHeightReadings objectForKey:[heightKeys objectAtIndex:indexPath.row]]];
[customLbl setText:#"CFS"];
} else {
[cell.textLabel setText:[rData.currentFlowReadings objectForKey:[flowKeys objectAtIndex:indexPath.row]]];
[customLbl setText:#"Ft."];
}
return cell;
}
The default textLabel for each cell renders as it should. However, the custom label does not appear. I verified that the prototype cell was actually rendering by changing the background color of the cell's content view. In my story board I have set the prototype cell's reuse identifier to match my code. The style is set to 'Custom.' What else should I be doing? Thanks!
Verify that your autolayout constraints are correct such that the UILabel is really positioned where you expect it.
I had the same issue once and I've solved it by not calling any methods to cell.textLabel property. Just don't set its text in any way.
Try it out and report back.
Try this:
UILabel *customLbl = (UILabel*)[cell.contentView viewWithTag:1000];

UICollectionView showing wrong data on Scroll forward and back?

I am using a UICollectionView. Inside this View one of my cells has a size of 1000*600(showing one cell in screen).
In my UICollectionViewCell are 4 UILabels and two tableViews.
The data which I am passing to UILabels is not showing correctly and as soon as I scroll back the data does not retain. I have seen various example in which it was suggested to use block Operation.
Which approach I have to use? Please help.
Here is the code:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
ToursCollectionViewCell *collectionViewCell=[collectionView dequeueReusableCellWithReuseIdentifier:#"collectionViewCell" forIndexPath:indexPath ];
UILabel *artistOne=(UILabel*)[self.collectionView viewWithTag:1111];
artistOne.text=[artistNameOneArray objectAtIndex:indexPath.row];
UILabel *venueOne=(UILabel*)[self.collectionView viewWithTag:1212];
venueOne.text=[venueLocationOneArray objectAtIndex: indexPath.row];
UILabel *artistTwo=(UILabel*)[self.collectionView viewWithTag:2121];
artistTwo.text=[artistNameTwoArray objectAtIndex:indexPath.row];
UILabel *venueTwo=(UILabel*)[self.collectionView viewWithTag:2222];
venueTwo.text=[venueLocationTwoArray objectAtIndex:indexPath.row];
return collectionViewCell;
}
UILabel *artistOne=(UILabel*)[self.collectionView viewWithTag:1111];
should probably be
UILabel *artistOne=(UILabel*)[collectionViewCell viewWithTag:1111];
(and similarly for the other labels) because the labels are subviews of each
individual cell and not of the entire collection view.

Custom dynamic TableViewCell text positioning

I have a dynamic TableView that looks like this.
However, I'd like to position the text more neatly, like this.
Here's the code I'm currently using:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier =#"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
[cell.textLabel setFont:[UIFont fontWithName:#"HelveticaNeue-Light" size:17.0f]];
cell.textLabel.text=[[self.responseArray objectAtIndex:indexPath.row]objectForKey:#"name"];
return cell;
}
I've experimented with putting a label into the TableViewCell that's positioned where I want the dynamic text to be, but I've been unsuccessful in calling it in my code. How can I do this?
You should create a custom cell subclass. This subclass should have an #property in the .h file which makes your custom label available. In the .m (or XIB) you create (or connect) the label to the property (outlet).
Now, instead of using cell.textLabel you use cell.customTextLabel. Just remember to register the custom cell class against your CellIdentifier.
You can also configure the font and size on the label in the cell .m (or XIB) so you don't need to do it in code.
You will also want to change to:
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
so that the compiler knows that class the cell is (and thus what properties it has).
There is no need to create subclass of UITableViewCell. Coz, you want just change origin of your text.
I recommend to add a UILabel to your cell as subview.
cell.textLabel.text = #"";
UILabel *textLabel = [[UILabel alloc] initWithFrame: CGRectMake(10,10,300,20)];
textLabel.text = #"Text here";
[cell.contentView addSubview:textLabel];
2-3 lines only and you needn't create a subclass.

viewWithTag returns nil when initializing a UICollectionViewCell

Just getting started with UICollectionView. I've used IB to create a simple UICollectionView in a UIViewController. It scrolls horizontally with paging. I placed a simple UICollectionViewCell inside the UICollectionView. I set the reuse identifier for the cell.
I placed a UILabel with a tag of 100 inside the UICollectionViewCell.
In viewDidLoad, I call:
[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:#"Thing"];
Later I attempt to initialize the cell like so:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"Thing" forIndexPath:indexPath];
cell.backgroundColor = [UIColor greenColor];
UILabel *nameLabel = (UILabel *)[cell viewWithTag:100];
nameLabel.text = #"Bogus";
return cell;
}
When I run the app, the view loads correctly; I can scroll horizontally through the 2 cells. I see the ugly green color identifying each cell.
However, the viewWithTag method returns nil and therefore the nameLabel text is not set.
Why?
Note that I also tried defining a custom subclass of UICollectionViewCell and calling registerClass with it. In IB, I change the cell class to the custom subclass and I bind the UILabel to the UILabel outlet in the subclass.
But this also does not work. (I would prefer to avoid the custom subclass approach anyway because it does not seem necessary to define classes just to hold IBOutlets.)
I'm thinking that I'm missing something obvious here.
Similar problem described here:
Trouble accessing Image from instance of UICollectionViewCell
Remove the registerClass line. You are doing it on storyboard, so you don't need it.
try it
UILabel *nameLabel = (UILabel *)[cell.contentView viewWithTag:100];
Update:
You can look the cell's hierarchy and all its subview's tag :[self showAllSubView:cell] ;, can you find your label with tag 10 ?
- (void)showAllSubView:(UIView *)view
{
for (UIView * subView in [view subviews]) {
NSLog(#"%#, tag:%d", subView, subView.tag) ;
[self showAllSubView:subView] ;
}
}

Resources