Reload table row right detail content of a specific cell - ios

I am new to iOS development. I want to reload a particular set of rows for an action taken in some other row. For example: I have 2 rows in my tableview. I select an option for row1 and row2 in order. Now I go back and change the value in row1. This should automatically clear the "right detail" text of the second row as it is dependent on the first row. I did some search online and went through the apple developer docs and I see I need to use "reloadRowsAtIndexPaths" but I cannot get this to work for some reason.
Here is what I have done so far when I edit row0 when row1 already has a value:
if ([row1 length] > 0) {
NSIndexPath* indexPath1 = [NSIndexPath indexPathForRow:1 inSection:0]; //get the index path for row1 as we need to reset its "right detail label"
NSArray* indexArray = [NSArray arrayWithObjects:indexPath1, nil];
[self.tableViewHdl reloadRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationFade];
}
I am under the assumption this will automatically reload the cell to NULL. I would really appreciate if someone could give me an example and help me understand this concept. Thank you in advance.

Related

Assign section and row indexpath to prepareForSeague

I am trying to get the indexpath.section and indexpath.row for my prepareForSeague. I can pass the indexpath.row no problem however I am struggling to integrate it with sections. It passes the correct data across but not when outside the first section as I do not know how to set the sections.
So basically I need it to know which section it is in too.
I looked at objectsAtIndexes?
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
dynamicDetailTableViewController *destViewController = segue.destinationViewController;
NSLog(#" indexPath row %d",indexPath.row);
NSLog(#" indexPath section %d",indexPath.section);
// Assume self.view is the table view
destViewController.titleString = [self.namesArray objectAtIndex: indexPath.row];
//Health
destViewController.healthArray = [self.healthArray objectAtIndex:indexPath.row];
EDIT
To make it clearer, it does work however I have different sections so after the first section it doesn't pass the right value. It passes only the first Sections values correct as it is only calculating the indexpath based on the row and not the section. I need it to calculate the section too, but not sure how.
I guess your data structure is wrong, if you have multiple sections and rows. Your data structure should look something like below
Section 1
----> Row 1
----> Row 2
----> Row 3
Section 2
----> Row 1
----> Row 2
Section 3
----> Row 1
----> Row 2
----> Row 3
----> Row 4
And to fetch the values your code should be something like
[[self.array objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
The above code says give me particular row for the particular section.
In your case it doesn't seems to be the case, you have got all the data in a single array, where as it should be array of array.
You are getting wrong values because for each indexPath.section your indexPath.row starts with 0, so for 1st section it works fine, but not for other sections..
Hope it helps.

Reload data on a UITableView with animation (the table hasn't got sections)

I'm developing an iOS 5.0+ app with latest SDK.
I have a NSArray with 20 elements. I want to show these 20 elements dynamically on a UITableView. I have disabled scroll on the tableview, and I want to remove the first NSArray element every 1 minute, and then reload UITableView to don't show the element that I have removed and show the remanning ones. And I want to do it with animation.
Searching over internet I have found some examples about how to do it using sections, but I have only one section on the UITableView.
In a nutshel,
Load 20 elements into a NSArray.
Show these 20 elements on an UITableView with scroll disabled.
For loop to remove first NSArray element every minute.
After remove, do UITableView reload with animation.
How can I do that?
You don't have to reload the entire table. Just remove the object from your array (once a minute or whatever). The object that you remove will be at index XXX in your array. And then call :
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:XXX inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];
Hope this helps. Cheers!
You need to use this :
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:intvalue(your indexPath) inSection:0];
NSArray *array = [NSArray arrayWithObject:indexPath];
[yourtableview deleteRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationFade];

core data saving in textboxes in table view controller

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.

Find selected cells in a TableView

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.

Table view cell indexing when using insertRowsAtIndexPaths

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.

Resources