How to handle references to UITextFields in dynamic UITableView (iPhone) - iphone

I have a UITableView that is broken up into a user defined number of sections. Within each of these sections there are always 2 rows. Each of these two rows contains a UITextField which the user is able to edit.
What I need is a way of being able to access the data in these UITextFields at a later point. I was hoping it would be a simple problem however it's causing me a great deal of grief.
So far I have tried two approaches:
Attempt 1
I created two NSMutableArrays in which I added the UITextField objects to at index i (corresponding to the section it came from). I then tried to access the values by iterating through the array. This didn't work since the UITextFields kept getting wiped clean. Every-time I scroll down the table and the UITextField is out of view, when I go back it's contents have been wiped clean.
Attempt 2
I tried to get hold of the number of sections in the UITableView (this was fine). I then wanted to iterate through each section of the UITableView, recording the values in the rows of each. This is where I became unstuck since I'm not sure how to do this or if it's even possible.
I apologise if this is a naive question to ask, however I'm really struggling and would appreciate any advice.

Keep in mind that the text fields get reused as you scroll, so you don't really want to store references to them.
What you do instead, is to capture the information as it is entered. The easiest way to do this is to implement the textFieldDidEndEditing protocol method in the delegate.
The tricky part is figuring out which row the text field is in. The best way is to create a UITableViewCell subclass which has a NSIndexPath property. You can then set that when you configure the cell with tableview:willDisplayCell:forRowAtIndexPath:.
Then, in textFieldDidEndEditing, access the tableViewCell indexPath property through its superview. i.e.:
NSIndexPath indexPathOfParentCell = [(MyUITableViewCellSubclass *)self.superview indexPath];
Doing it this way allows you to know both the section and row of the cell.

Create your TextField in the cellForRow of the Table like so and give it a tag
UITextField * userField = [[[UITextField alloc] initWithFrame:CGRectMake(0, 12, self.view.frame.size.width -20, 20)] autorelease];
userField.tag = 1001;
userField.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:14];
userField.textAlignment = UITextAlignmentCenter;
userField.delegate = self;
userField.autocorrectionType = UITextAutocorrectionTypeNo;
userField.autocapitalizationType = UITextAutocapitalizationTypeNone;
userField.clearButtonMode = UITextFieldViewModeWhileEditing;
if (indexPath.row == 0)
[cell.contentView addSubview:userField];
then access the TextField like so:
UITextField *userField = (UITextField *)[[(UITableViewCell *)[(UITableView *)tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]] contentView] viewWithTag:1001];

Your "Attempt 1" should work OK if you keep the text field's text in your arrays rather than the text field itself. (Anything that tries to make a view or control into a data object has a good chance of going wrong.)
Whatever acts as a data source for your table view should be able to re-populate the scrolled cells according to section and row if the content is stored separately.

Related

Paging in uitableview

I am using a UITableview.In that there are nearly 100 records.I want to show 10 records at a time after that i will click on a button or UIPageControl which will show the next 10 records.So,please suggest me how can i do it?
If you just wanna create a table like that you could follow the below outlines.
In your button click Append or change the data in your data source array of your tableview. (Depending upon your requirement)
[self.myDataSourceArray addObjects:#"obj", #"obj2", nil];
Reload the table
[self.tableView reloadData];
UPDATE FOR THE COMMENT:
OK let me add some code,
//Consider this method is called when you click the next button
- (void)nextButtonAction
{
//Remove the older objects
[self.myDataSourceArray removeAllObjects];
//Add your new objects to the array
[self.myDataSourceArray addObjects:#"obj", #"obj2", nil];
//Reload the table
[self.tableView reloadData];
}
You could have a forward and backward button in the navigation bar with the page number
Define a rect would you like to scroll and then call method scrollRectToVisible:newRect for UITableView
CGRect newRect = CGRectMake(0, 0, tableView.frame.size.width, tableViewCell.height*10);
[tableView scrollRectToVisible:newRect animated:YES];
I am not sure if it is a good thing to introduce a pagination mechanism in your case. Because a UITableView should be fine for handling around 100 records. Just think of your Addressbook which can easily contain more than 100 records.
If you have a big number of records in a UITableView filtering and the index on the right with the letters of the Alphabet can help you to quickly navigate to the desired entry.
However, if you do decide to add more records to one table a common practize is to add a "Load more entries" cell at the very bottom of the table view.
For Example: Check out the Top50 Apps in the AppStore-App.
This really goes against Apple's User Interface Guidelines. TableViews are designed with built in highly efficient pagination (reusable cells). I have TableViews with 4000 records and when the data model behind the TV is done right, it can be very responsive.
If you are dead set on this, you could use a button on the NavBar, Toolbar, etc and use that to call scrollToRowAtIndexPath.
you can keep a count of the number of times the button is pressed
int i=0;
-(void)buttonPressed{
i=i+10;
[table reloadData];
}
then in
cellForRowAtIndexPath
{
cell.textLabel.text = [NSString stringWithFormat:#"%#",[Arr objectAtIndex:i+indexPath.row]];
}

Getting access to a UITableViewCell row that is currently not showing

Is there a way to get access to a UITableViewCell that is not currently on screen? I'm trying to update rows that are currently not in view. I am using this collapsable/expandable rows for a UITableView in one of Apple's example codes. I use this:
for (NSInteger row = 0; row < totalRows; row++) {
NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:section];
OrderTableViewCell *cell = (OrderTableViewCell *)[_tableView cellForRowAtIndexPath:path];
cell.CheckmarkButton.selected = YES;
cell.Symbol.isSelected = YES;
}
My totalRows is correct in that I get the number of rows from the TableView. So I thought I could use that, loop through all the rows, and set some values. However, if I check the state of the rows, the ones that are currently on screen have values and I can change them, but the ones off screen are null and cannot be set. Is there a way I can get around this? Thanks.
Depending on what you want to update you could just update the ith entry in a backing collection.
For example you could have a collection that stores bools corresponding to CheckmarkButton selected. In your loop you could set myArray[row] = YES. In cellForRowAtIndexPath you can do cell.CheckmarkButton.selected = myArray[indexPath.row].
If you want to maintain multiple items for each cell then myArray in the above example could contain instances of an object that holds values relevant to the ith cell.
Well why do you have to update a cell like that... If the cell is out of view, to save memory the system unloads it, and since its not in the view there is no need for you to update the cell directly, rather you should have the state be reflected in such way that when cellForRowIndexPath is called by the table view to load the cell the desired look is shown... The only reason youd want to do what you are doing is if the cell is showing and you need to update the cell, for the user... Even then you can still set your state and call the reloadRowsAtIndexPaths instead of manipulating the cell directly... Hope that helps

Delete Row from UITableView Outside TableView Delegate

I am trying to delete a row from UITableview outside the delegate method. I am calling a method when I click a button inside a table cell and trying to delete the row inside that method.
Here is the function I am using
UIButton *btn = (UIButton*)sender;
int tag = btn.tag;
UITableViewCell *buttonCell = (UITableViewCell*)[[btn superview] superview];
NSIndexPath *indexPath = [self.msgTbl indexPathForCell:buttonCell];
[deleg.rmessages removeObjectAtIndex:buttonRow];
[self.msgTbl deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];NSInteger buttonRow = indexPath.row;
[self.msgTbl reloadData];
Using this one or two rows get deleted but after that its crashing giving exception
Number of rows before and after deletion must be same
How can I do this in ios?
Thanks
Your problem is that the data that is being taken to populate your table isn't consistent with the table after deleting the cell. Make sure your dataSource methods provide the correct data after doing this (for example, if it is an array of objects you are using to populate the table, you must remove the object from the array as well)
The root issue is that this method:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
Must return the correct number of rows, and it isn't. When you remove the item from deleg.rmessages, is this the same object that is being used to supply the return value of the above method? (Something like [deleg.rmessages count]?)
Also, in my experience that exception often gives you more details, in particular:
How many items it had before
How many were added/deleted
How many it expects to have vs. how many it does have after the reload
Do you see anything like this being mentioned? If so, it would be worth including in your question.
Sidenote:
It's a bad idea to rely on:
UITableViewCell *buttonCell = (UITableViewCell*)[[btn superview] superview];
To return the UITableViewCell. You appear to assign the tag of the button to a local variable, but never use it. (Maybe this would be a good place to store the index of the UITableViewCell, and then subclass the cell to maintain an ivar to the button?) This is only part of the problem.

UITableView change first upper/lower unused cell

Is it possible to modify the first "cell" under/over the "real" content?
Imagine this as the content or structure:
#0:[]
#1:[]
#2:[]
#3:[]
#4:[]
In this case, I would want to modify the cells with the number -1 and 5. But this should not affect the behaviour of the scrollbar. (e.g. not extending the scrollable content)
Thanks in advance
EDIT: Take a look at the facebook-refresh-cell. (Pull down the UITableView)
If I understand it correctly, you want to change the content of cells off-screen.
You actually don't want to change the cells themselves, but rather, you want to change the underlying data model that provides the cells with their data. You can, of course, do this at any time.
You shouldn't be trying to get at the UITableCell objects themselves.
Hi
Instead of trying to modify the cells, just create your own Footer and Header cells and put everything you need there, it will always stay there without modifying the actual data cells
Look into
(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(10, 10, 100, 30)] autorelease];
view.backgroundColor = [UIColor redColor];
return view;
}
you can do the same for Footer view just change the viewForHeaderInSection to viewForFooterInSection

Preload cells in tableView for use with mapView

I have a mapView and tableView, with each annotation in the mapView corresponding with a cell in the table. What I want to do is select the appropriate cell anytime an annotation is selected on the map.
As of right now, im creating an NSDictionary when the cells are created, which maps the row number to the annotationID. This works, but the problem is that the dictionary isnt completely populated until all the cells have been created, and all the cells arent created until youve scrolled all the way through the table. Thus, when the app starts for the first time, only the 4 annotations originally visible can be selected from the mapView.
So what im looking for is either a method to automatically populate my dictionary, or a better way of accomplishing what i need to do. Thanks a lot!
Well, first thoughts are that instead of populating the NSDictionary during scrolling, just populate it during viewDidLoad ..I do something similar where I populate all of my data into an NSDictionary and then use that to initialize/update the UI on the cells during scrolling for re-usable cells.
Also, by putting your data into an array of some kind, you can also map it to the cells. Just remember that arrays start at 0, so position in table = indexOfYourArray + 1
Simple example of loading an array stored in a plist into an NSArray in viewDidLoad
// Load the data
NSString *pathToFile = [[NSBundle mainBundle] pathForResource:someArrayNameString ofType:#"plist"];
self.someArrayYouCreated = [NSArray arrayWithContentsOfFile:pathToFile];
Now that you just dumped a whole bunch of data into that array, you can populate it during scrolling in cellForRowAtIndexPath
NSDictionary *dataItem = [someArrayYouCreated objectAtIndex:indexPath.row];
UILabel *label;
label = (UILabel *)[cell viewWithTag:1];
label.textColor = [UIColor colorWithRed:(58/255.f) green:(58/255.f) blue:(58/255.f) alpha:1.0];
label.text = [dataItem objectForKey:#"PersonName"];
With this example, you are just populating the cells from an array that you created in viewDidLoad and therefore all of your data is ready to use almost right away. If you have a ton of data, you could also throw up a progress circle to delay for a second until the array is finished loading.
Again, I don't quite understand how you are storing your data (since you have not said anything about that) so I can only speculate that this will work for you. Until you provide more detail, this is the best I can do.