I have created a five column (text boxes) cell (row) in table view controller with an option of add button. When a user clicks on add button, a new row (cell) with five column (text boxe) is added in a table view controller with null values. I want that when user fills the text boxes the data should get saved in database or if he changes any data in previous text boxes also it get saved.
this is my save btn coding..
- (IBAction)btn_save:(id)sender
{
NSInteger noOfRow=[(NSSet *)[projectObject valueForKey:#"rs_project_Input"] count];
NSLog(#"Save Btn: No of rows for saving %d",noOfRow);
for (row1=0; row1<noOfRow; row1++)
{
NSLog(#"Row is %d",row1);
path = [NSIndexPath indexPathForRow:row1 inSection:0];
Input_Details *inputDetailsObject1=[[self sortInputs] objectAtIndex:path.row];
/*
Update the input details from the values in the text fields.
*/
EditingTableViewCell *cell;
cell = (EditingTableViewCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row1 inSection:0]];
inputDetailsObject1.mh_Up= cell.cell_MH_up.text;
inputDetailsObject1.mh_down=cell.textField.text;
NSLog(#"Saving the MH_up value %# is saved in save at index path %d",inputDetailsObject1.mh_Up,row1);
[masterController saveContext];
}
[self.tableView reloadData];
}
My problem is that the code is not helping in saving the data. Plz help.
Thanks
You try to use the table view cells as a data source, which will not work (apart from being a bad design). The problem is that
[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row1 inSection:0]]
returns nil if the cell at that row is currently not visible. (A table view allocates only cells for the visible rows and reuses these cells as you scroll up or down.)
You have to track changes to the text fields immediately, e.g. by implementing a textFieldDidEndEditing: delegate function, and store the changed text in your data source.
Related
Here is my program. I want to create a simple list of items that display a number. When the rows are tapped the number will increment by one.
EDIT: Is it proper to change the UI of a row in the didSelectRowAtIndexPath function?
I created a UIViewController in Xcode 5 through a storyboard and it does everything right except I can't seem to stop the [tableView reloadData] from deselecting my row after being tapped. Specifically, I want the row to turn gray and then fade out normally.
I have tried selecting the row and then deselecting the row programatically after calling [tableView reloadData], but it doesn't work.
I know that if I was using UITableViewController that I could just call [self setClearsSelectionOnViewWillAppear:NO], but I'm not.
Is there a similar property I can set for UIViewController?
Here is the code:
[tableView beginUpdates];
[counts replaceObjectAtIndex: row withObject: [NSNumber numberWithInt:newCount]];
[tableView reloadData];
[tableView endUpdates];
I feel I may not be describing what is going on. I have a row that uses UITableViewCellStyle2, which displays a label to the left and right. On the right aligned text is a number that increments each time the row is tapped. Simply updating the data structure does not solve the problem. I need to update it visually. I don't need or want to replace the row, unless I have too. I just want to update the right-aligned text field AND keep the row from being deselected immediately without animation. I can do one or the other, but not both.
Is there a way to just update the right-aligned text field while still staying true to the MVC model?
Remove the [tableView reloadData]; from the code. It should not be called in the methods that insert or delete rows, especially within an animation block implemented with calls to beginUpdates and endUpdates .
Call reloadData method to reload all the data that is used to construct the table, including cells, section headers and footers, index arrays, and so on. For efficiency, the table view redisplays only those rows that are visible. It adjusts offsets if the table shrinks as a result of the reload. The table view's delegate or data source calls this method when it wants the table view to completely reload its data.
[tableView beginUpdates];
[counts replaceObjectAtIndex: row withObject: [NSNumber numberWithInt:newCount]];
[tableView endUpdates];
See the developer.apple section - reloadData
If you want to keep the selection after reload, the easy way is
NSIndexPath *selectedRowIndexPath = [tableView indexPathForSelectedRow];
[tableView reloadData];
[tableView selectRowAtIndexPath:selectedRowIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
I am trying to trigger some code based on if the cell I selected becomes the first or last cell on the users screen. I'm not trying to capture the index value of the data array. Just the index value of the cell that is visible on the screen. I'm sure this to create an array of visible cells.
NSArray *indexPathsForVisibleRows = [myTableView
indexPathsForVisibleRows];
But I keep hitting a dead end trying to then capture an index value based on that array.
I tried to use a CGPoint and convert that, but I keep getting an error. Any insight would be most helpful!
As per the documentation for the return value of that method:
An array of NSIndexPath objects each representing a row
index and section index that together identify a visible row in the
table view. Returns nil if no rows are visible.
The array returned from that method contains NSIndexPaths.
NSArray *indexPathsForVisibleRows = [myTableView indexPathsForVisibleRows];
for(NSIndexPath *eachIndexPath in indexPathsForVisibleRows)
{
NSInteger row = eachIndexPath.row;
NSInteger section = eachIndexPath.section;
UITableViewCell *cell = [myTableView cellForRowAtIndexPath:eachIndexPath];
if(cell.isSelected)
{
// this is our selected cell
}
}
I'm writing an iPhone application with a tableview which displays a list of items,each one with a checkmark. I can selected/deselect each item just with one click. When I press a button [DONE] I would like to iterate along all the cells and check which one has the checkmark enabled. Something like:
for (int i = 0; i < [fullDataset count]; i++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
NSLog(#"THIS CELL IS SELECTED!");
}
}
Now,the problem is that using this procedure I get only the cell which are selected and which are CURRENTLY DISPLAYED. I want to be sure that the procedure runs across ALL the cell,even the ones not displayed. I might use another data structure for keeping track of the selection but it looks to be a bit redundant.
Any idea?
Thanks a lot!
Claus
You can't count on the cells to tell u if selected or not, cells get reused and always change there state.
You need to manage a separate NSSet of the selected indexPaths, when user tap on a cell, u add this indexPath to the set, when he deselect the cell u delete the indexPath from the set.
Something weird is going on with my tableview when changing data. I have something like this:
// fadeout existing data
... // change data (new values for the cells are blank)
for{ // loop all rows
[TableView reloadRowsAtIndexPaths: withRowAnimation:]; // smooth fade out
}
// add one new row to the table
... // change data (just add one new row but leave the cells empty)
[TableView reloadData] // reload all of the data (new row should be added)
... // change data (just add values to the existing cells)
for{ // loop all rows
[TableView reloadRowsAtIndexPaths: withRowAnimation:]; // smooth fade in
}
In theory, at least what I think, this should work. But I had some glitches so I added NSLogs in the for loops and in the: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath so I noticed that my [tableView reloadData] executes after the first row of fade in loop!##
I was thinking about making some kind of a delay between reloadData and fade in loop, but I don't want to force it to work, I want it to work properly.
Is there a better way to accomplish what I'm trying to do?
Can I dynamically add one row at the bottom of the table without calling the 'reloadData' method?
Thanks.
Yes, just use the tableView insertRowsAtIndexPaths method, which will then call your dataSource to load those new rows.
To load a single row at the end of your table, you'd first update your data source to include the new row, then generate the indexpath using:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[yourdata count]-1 inSection:0];
NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
[tableView insertRowsAtIndexpaths:indexPaths withRowAnimation:whateveryouwant];
Remember, that will immediately call your tableView:cellForRowAtIndexPath method, so update your dataSource to include the extra row before inserting the new row into your table, and be careful that the row index in the indexPath you specify matches the newly added item in your data source.
Ok, I'm stuck. This is an extension of a previous post of mine. Here is what I am trying to do.
I have an Edit button on a navigation bar that when pressed adds a cell at the beginning of my one section table view. The purpose of this cell if to allow the use to add new data to the table; thus it's editing style is Insert. The remaining cells in the table are configured with an editing style of Delete.
Here is my setediting method:
- (IBAction) setEditing:(BOOL)isEditing animated:(BOOL)isAnimated
{
[super setEditing:isEditing animated:isAnimated];
// We have to pass this to tableView to put it into editing mode.
[self.tableView setEditing:isEditing animated:isAnimated];
// When editing is begun, we are adding and "add..." cell at row 0.
// When editing is complete, we need to remove the "add..." cell at row 0.
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
NSArray* path = [NSArray arrayWithObject:indexPath];
// fill paths of insertion rows here
[self.tableView beginUpdates];
if( isEditing )
[self.tableView insertRowsAtIndexPaths:path withRowAnimation:UITableViewRowAnimationBottom];
else
[self.tableView deleteRowsAtIndexPaths:path withRowAnimation:UITableViewRowAnimationBottom];
[self.tableView endUpdates];
// We nee to reload the table so that the existing table items will be properly
// indexed with the addition/removal of the the "add..." cell
[self.tableView reloadData];
}
I am accounting for this extra cell in my code, except I now have two index paths = [0,0] - the new cell and the old original first cell in the table. If I add a call to reload the table view cell in setEditing, the cells are re-indexed, but now my table view is no longer animated.
I want my cake and eat it too. Is there another way to accomplish what I am trying to do and maintain animation?
--John
You can do what you want but you need to keep your data source consistent with the table. In other words, When the table is reloaded, tableView:cellForRowAtIndexPath and the other UITableViewDataSource and UITableViewDelegate methods responsible for building the table should return the same cells depending on editing state that you are adding/removing in setEditing:antimated:.
So, when you insert/delete a cell in setEditing:animated: you need to also make sure your data source reflects the same change. This can be tricky if you are adding a special cell to the beginning of a section but the rest of the data is from an array. One way to do this is while reloading the table, if editing, make row 0 the add cell and use row-1 for your array index for subsequent cells. If you do that you'd also need to add one to tableView:numberOfRowsInSection: to account for the extra cell.
Another way would be to have a section for the add cell and it would have 0 rows when not editing, 1 row otherwise and you return the appropriate cell. This will also require you to configure your table and cell(s) appropriate depending on how you want things to look.