How to make UITextView scroll up when entering it? - iphone

I have a UITextView included in a UITableViewCell. The layout is correct when the views are initially displayed, but once I click in the UITextView it DOES NOT automatically scrolls up and the whole UITextView becomes invisible.
This image is when the UITextView is not active:
http://img13.imageshack.us/img13/3894/unloaded.png
And this one is when I clicked in the UITextView to make it active:
img337.imageshack.us/img337/2583/loaded.png (Put "http://" before the link, I can't post more than one Hyperlink)
I want the cell with the UITextView to scroll up. How can I achieve this?
Any suggestions are appreciated.
Julio Cezar

The simplest way would be to make an IBOutlet for the items you want to move up, and then in the code do something like this when your text field is selected:
myTextView.center.y = 200 or whatever y value you see fit.

I've already done it. My solution is not the best but it works very well: everything happens on tableView:cellForRowAtIndexPath:
1) For every cell that has a UITextView I have a NSDictionary: I save all my UITextView's on the cells on NSDictionary with a particular key.
2) And I also have another NSDictionary with the indexPath objects that are created in tableView:cellForRowAtIndexPath. If a cell A has UITextView A, they will have the same key in the NSDictionaries.
3) I implement the UITextViewDelegate and call textViewDidBeginEditing: here is my code:
(void)textFieldDidBeginEditing: (UITextField *)textField {
NSArray *keys = [self.answersDic allKeysForObject:textField];
id *key = (id *)[keys objectAtIndex:0];
NSIndexPath *tempIndex=[self.indexPathForViews objectForKey:key];
[tableViewLocal scrollToRowAtIndexPath:tempIndex atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
OBSERVATIONS:
a) answersDic is the NSDictionary where my UITextViews are saved.
b) indexPathForViews is the NSDictionary where my IndexPaths are saved.
c) the most important part of this code: the method, that brings the view to scroll up:
[tableViewLocal scrollToRowAtIndexPath:tempIndex atScrollPosition:UITableViewScrollPositionTop animated:YES];
So, thats it. Thanks for your help!

Related

Get the values in checked cells in a UITableView

I have a UITableView with each cell is having a UITableViewCellAccessoryCheckmark. The user can check and uncheck the cells depending on his preference. The user can check/select multiple cells in the tableview. A selected/checked cell can be unchecked and rechecked again on user preference.
I have a done button in the UITableViewController. On its click, I need to return to the previous view, before that I have to have a collection of the text in the checked cells(only checked cells).
How can I do this.
I was planning on developing a logic, by keeping an NSMutableArray and update it on - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath, when a cell gets checked/selected. But when every time a cell is unchecked I have to remove the item from the array and if the cell is checked again, then I have to add it again. I reckon thats not the right way to do this. What would be the right way to do this.
I couldn't find a question of similar kind here in Stackoverflow, which is very unusual. Would be helpful if someone could post a link, if the question was asked before.
Maintain two arrays, like this:
#property (nonatomic, retain) NSMutableArray *features, *selectedFeature;
synthesize them in .m file. initialize your both arrays something like this in viewDidLoad:
self.features = [NSArray arrayWithMyOwnResourceLikeDownloadedFromServerOrWhatever];
self.selectedFeature = [NSMutableArray array];
Then do something like this in didSelectRowAtIndex:
NSString * stirng = [features objectAtIndex:indexPath.row];
if ([self.selectedFeature containsObject:stirng]) {
[self.selectedFeature removeObject:stirng];
}
else{
[self.selectedFeature addObject:stirng];
}
and in your cellForRowAtIndexPath:
NSString * stirng = [features objectAtIndex:indexPath.row];
[cell.textLabel setText:stirng];
if ([self.selectedFeature containsObject:stirng]) {
//it is selected feature
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}
else{
//it is un-selected feature
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
Create a boolean array of the same length as the number of checkable cells and update the value on click events. If the table content is dynamic, you can use methods provided by NSMutabeArray to keep the boolean array in accordance with the table content. Upon return use the array to grab the text you need.

How to find out programmatically added textfields in a scrollview

Inside my scrollView i am adding uitextfields programmatically by clicking an "ADD" Button. the number of textfields depending on how much i press in add button. it can be 'N' number of textfields.
After this fields editing how can i get values inside the all textfields into an array.
if once i edited textfield there is a possibility to delete entry inside that. So what is the way to save values in the all text fields while clicking in Save button.
i am attaching my screenshot with this.
Thanks in Advance
use fast enumeration.
NSMutableArray *arr = [NSMutablearray....];
for (UIView *subV in self.view.subviews){
if([subV isKindOfClass:[UITextField class]]){
//store it in a NSDictionary, so later can still know which
//textField your text belongs,
NSDictionary *tempDic = [NSDictionary dictionaryWithObjectAndKey:subV.txt
,subV.tag,/*or subVw.placeholder*/,nil];
[arr addObject:tempDic];
}
}
You can get all the textfields by iterating through the subview array of the scrollview
for (UIView *subView in scrollview) {
//check if the subView is a textfield by for example matching a certain tag
//save text of textfield
}
When you are creating textField then assign tagValue to every textField and then access textField using tag value....
UITextField *txtField = (UITextField *) [scrollView viewWithTag:tag];

how to set hidden property to NO for particular cell in tableView

I am new to iphone.I have a small doubt that is,I have a table view with 66 rows initially i placed a progress view to all rows but in viewdidload i set it to hidden for all those like below in tableViewClass(ShowProgressViewCont)
cell.progressView.hidden = YES;
here (cell) is the the reference of the CustomCell class.In this class i declare a progress view and setter and getter properties also be set here in this class
here in tableview there is a download button in each cell.If we click on that download button(for example in 66th cell).we have to remove the hidden property to the progress view for that particular 66th cell only.The remaining 65 cells should have the progress view in hidden only like that for all cells.
If any body know this please help me....
Are you familiar with the concept of table cells being reused?
viewDidLoad is not an appropriate place to manipulate the content of a single cell. It may work fine if the table is so small that all of its cells fit on the screen (in both orientations).
When there are more cells in the table than beeing displayed on the screen, then those cell that became invisible recently is being reused and displayed again.
So if there are 6 cells on the screen at a time then table cell no. 7 (sometimes 8) will be identical with cell no. 1.
cellForRowAtIndexPath would be a better place to hide/unhide certain views of a cell.
If it is a custom cell already then the cell's layoutSubViews could be appropriate, too. However, only in the tableViewController's cellForRowAtIndexPath you will have easy access to both, the table's data and the associated cell.
cellForRowAtIndexPath is called every time when a cell is about to become visible. Use the default reusing-mechanism to ensure that a proper cell object will be reused. It is pretty much straight forward.
Get the cell at index 65 and then cast it to your custom cell
UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:64 inSection:0]];
YourCustomCell *customCell = (YourCustomCell *)cell;
customCell.progressView.hidden = NO;
First set the row number in your download button in CellForRowAtInedexPath
downloadButton.tag = indexPath.row
[downloadButton addTarget:self action:#selector(actionDownload:) forControlEvents:UIControlEventTouchUpInside];
in actionDownload, make the IndexPath from button.tag and get the cell from "cellForRowAtIndexPath:",
finally update via reloadRowsAtIndexPaths: withRowAnimation:
Hide your progress view in your custom cell means make the default property of every progress view is hidden ,and then your button click method .
CutomeCell *cell = (CutomeCell *)[[button superview] superview];
[cell.progressView setHidden:NO];
NSIndexPath *rowToReload = [[self tableView] indexPathForCell:cell];
NSArray* rowsToReload = [NSArray arrayWithObjects:rowToReload, nil];
[UITableView reloadRowsAtIndexPaths:rowsToReload withRowAnimation:UITableViewRowAnimationNone];
may this help you ...
your model should be tracking the progress for every button clicked. for purposes of this answer, let's say the model is held in a property called modelInformationArray (which would contain an array of objects that relate to each cell in the table)
when the download button is clicked for a cell, modelInformationArray is modified in such a way that its object knows a download is being processed for it.
that object reports the downloading processing in a member called downloadingProcessingStarted. there are many other ways to do that part.
to get to the answer to your question … to then unhide the progress view, you would do something as follows in your UITableViewDataSource implementation of tableView:cellForRowAtIndexPath: .
- (UITableViewCell*)tableView:tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
YourCustomCell* cell = ...
if ([[self.modelInformationArray objectAtIndex:indexPath.row] downloadingProcessingStarted])
cell.progressView.hidden = NO;
}

Retrieve UITextField Text for a specific UITextField Tag

Hey
Just wondering how do you..
Iv created some code that automatically creates a certain amount of UITextField input by the user.
Each UITextfield has a set tag automatically created.
The user then inputs his value into each of the UITextFields.
I want to retrieve the input text from the UITextFields which correspond to the tag.
I thought I nearly had it with:
NSString * tmpV = (NSString*)[choiceTextField.text viewWithTag:result];
where result is a increment, choiceTextField is the UITextField. With this I get a problem not defining instance, which I can't do as the UITextFields are generated in code not on the xib.
So to sum up, basically want the retrieve the text from a UITextField with specific tag.
Thanks
Dan
UITextField *yourTextField = (UITextField *)[self.view viewWithTag:result];
NSString *getText = myTextField.text;
Hope this helps
Your textfields must be a subview of a parent view. You need to write the following in your view controller of the parent view:
NSString *text = ((UITextField*)[self.view viewWithTag:result]).text;
Just go with Hetal Vora's solution: looks much cleaner!
-viewWithTag can be called on any view and searches all its subviews for one that matches the tag. Thus, you will find your UITextField by calling it on any view that is above it in the hierarchy. For example, you could call it on the view controller's view that contains your text field, as follows:
NSString *tmpV = [[myViewController.view viewWithTag:result] text];
If you are already in the view controller's class you could use:
NSString *tmpV = [[self.view viewWithTag:result] text];
On second thought, the following may be more correct & will avoid any compiler errors:
UITextField *myTextField = (UITextField *)[self.view viewWithTag:result];
NSString *tmpV = myTextField.text;

How to pass values between two Custom UITableViewCells in iphone

I have two customized cells in an iphone UITableViewController and would like to capture the data in one cell into another.
I am currently unable to post images and please assume the following points.
There are two custom UITableViewcells
Notes Textfield in one customcell
Submit button in another text field.
Now I want to pass data typed in the notes textfield to be available when I click the Submit button. Is this possible? Please help :(
In general you should store your data separate from views. So if you have some table controller there must data objects like array or dictionary or array of dictionaries. And when you click submit you should get associated data object and work with it.
In your case when textfield lose focus you must get textfield value and store it in your data store.
UPD: It not very clean but can help at first
- (void) textFieldDidEndEditing: (UITextField *) textField {
// At first need to get cell. findSuperviewWithClass is custom method to find proper superview object
MyCellClass *cell = (MyCellClass*)[textField findSuperviewWithClass:[MyCellClass class]];
// and indexPath for it
NSIndexPath *index = [tableView indexPathForCell:cell];
// tableData is NSArray of my objects
MyDataObjClass *dataObj = [tableData objectAtIndex:index.row];
dataObj.text = textField.text;
}