How to save the new data to existing cell? - iphone

How can I save the time to existing cell?
Page 1 have name, phone number, company, job number..
Because the time is in the page 2, I don't know how do save it easily.
Anybody can help me solve this problem? thanks soooo much!!!!
NSLocalizedDescription=The operation couldn’t be completed. (Cocoa error 1570.), NSValidationErrorObject=<VisitorMO: 0x60000009c570> (entity: Visitor; id: 0x600000221800 <x-coredata:///Visitor/t47C35BF8-213C-4C00-9B86-DDF5B423B9092> ; data: {
company = nil;
jobnumber = nil;
jobtype = nil;
name = nil;
phonenumber = nil;
signintime = "May 30, 2017, 2:10:23 PM";
})}
)

Looks like you want to update existing cell's data..
You can do few things..
First yourTableview.reloadData() call this method.
This will reload your tableview.
Second if you want to update existing cell..
yourTableview.beginUpdates()
yourTableview.reloadRows(at: [indexPath], with: .automatic)
yourTableview.endUpdates()
Here indexPath will be your index which you want to update.
Third update specific cell
let indexPathYouWantToUpdate = IndexPath(row:Any row which you want to update,section:0)
let cell = yourTableview.cellForRowAtIndex(at:indexPathYouWantToUpdate) as? YourCustomCell
cell?.value1 = 100
cell?.value2 = 200
update all the cell values the way you want.
I hope this solves your question here.

Related

Compare a sort list item with a string for a UItableview title

I sorted a list in a table view, but i don't want to show the item of the list as the title of the row. I need to compare the first item of the sorted list with a string, and if it is the same, I want to show an other string as the title.
Just as an example, to explain it better:
if (first item of the sorted list == some string) {
cell.textlabel.text = #"name";
}
Is it possible to do something like this?
Is your sorted array stored in an NSArray? If so, you can use:
if ([[sortedArray objectAtIndex:0] isEqualToString:#"Some String"]) {
cell.textlabel.text = #"name";
}
If I have interpreted the question correctly, then yes it is. If the first item was a cell, which it would be presumably, then you could do;
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
UITableViewCell *firstCell = [yourTableView cellForRowAtIndePath:indexPath];
NSString *titleOfFirstCell = firstCell.textLabel.text;
Then you could compare it to check what it should be changed to - I'm going to call this string 'updatedTitle'.
titleOfFirstCell.textLabel.text = updatedTitle;
[firstCell setNeedsDisplay];
Hope this helps,
Jonathan

UITableViewCell is nil if it's not visible

In my app I'm using ELCTextfieldCell. The idea is to use the data entered by the user for some calculations. But there is the problem. I have about 14 cells and, of course, they can't all fit on a screen. So when I click OK the app is checking if all fiels are filled in:
BOOL complete = YES;
for (int i = 0; i < [cellTextArray count] - [self.numberOfBools intValue]; i++) {
NSIndexPath *iPath = [NSIndexPath indexPathForRow:i inSection:0];
ELCTextfieldCell *theCell = (ELCTextfieldCell *)[self.tableView cellForRowAtIndexPath:iPath];
if (!theCell.rightTextField.text)
complete = NO;
}
This code works perfectly if all the cells are visible, but if some are out, then the complete becomes NO. The output of theCell in gdb is:
(gdb) po theCell
Can't print the description of a NIL object.
Can somebody push me in a right direction please? :)
All help will be appreciated, thank you.
EDIT
self.numberOfBools is just an NSNumber with total of bools in these rows. They are using UISwitches, not UITextField as the other cells, so I excluded them from the check.
Complete needs to be calculated on the data that is backing the cells rather than on the cells themselves. Let me explain.
The cell is a visual representation of your data and when it is not in view the run-time will release it and that is why it is nil.
However cellForRowAtIndexPath: creates the cell from data right? (or it is normal to do so) so when the user updates the cell.rightTextField you should update data.rightTextField and then complete should be looking something akin to ... and this is pseudocode not compilable
complete = YES;
for (Data* data in myDataSet) {
if (!data.rightTextField)
complete = NO;
}
So, in summary cells represent data and they are not guaranteed to persist. You yourself can ensure the data is persisted; therefore test for completeness on the data and not on the cells.
This is by design. Cells in UITableView are reusable, and they can be released when invisible to save resources. You have to store your data somewhere else than in cells.
I agree with Damo and anticyclope here. You should have updated the
theCell.rightTextField.text
into your datasource of your table in the first place.
Then you could make your calculation based on the datasource.
Let textFieldArray be the datasource of your table.
NSMutableArray *textFieldArray;
myTableView.dataSource = textFieldArray;
Then need to update the datasource when the textfield is updated.
Finally you can process the datasource.

Decrease indexPath by one

I'm filling a TableView with CoreData.
Until now I was doing something like this:
NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
...to retrieve the object to fill the row.
Everything was working fine until I realized I have to manage the first row of the table as an exception because the first line will contain other content, not provided by CoreData.
Now my issue is how can I manipulate the indexPath to shift everything by one. I would like to do something like this:
// I know this is not going to work, just to give you an idea...
NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath-1];
but I cannot find the right syntax to manipulate the indexPath. Can anyone help me?
Thx for your time!
In case we are talking about iOS UITableView index path there's much easier way:
NSIndexPath* newIndexPath = [NSIndexPath indexPathForRow:oldIndexPath.row+1 inSection:oldIndexPath.section];
Cheers... :)
If you're talking about a standard iOS UITableView, then your index path is an instance of NSIndexPath, and will have two entries: a section index and a row index. If I understand you right, you want to decrement the row index by 1 every time you go to fill a table view cell. (I'm assuming you only have one section, or don't care about the section index - if this is wrong, please edit your question.)
Basically what you need to do is construct a new NSIndexPath instance with your adjusted indices, then use that to query your fetched results controller. Something like this would work (untested):
NSUInteger indexes[2];
[indexPath getIndexes:indexes];
indexes[1]--;
NSIndexPath * adjustedIndexPath = [[NSIndexPath alloc] initWithIndexes:indexes
length:2];
NSManagedObject * managedObject = [self.fetchedResultsController
objectAtIndexPath:adjustedIndexPath];
This basically does the following:
Pulls the existing indexes into a C array of NSUIntegers
Decrements the last index (at position 1 - the row index) by 1
Creates a new NSIndexPath with the adjusted indexes from the C array
Fetches the managed object using the new index path
Note again that this doesn't touch the section index at all, and so will adjust every cell in your table, regardless of whether it's in your first section. If that's not what you want, either wrap the adjustment in a conditional (e.g. if([indexPath indexAtPosition:0] == 0)) or add your own custom logic.

Getting row number in scrollViewDidEndDecelerating

when i use
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
NSArray *visibleCells = [my_table visibleCells];
i want to know the number of the row (in my_table) that is visible (with visibleCells).
For example, if i do
[visibleCells count];
i know that there are 5 rows visible, but i need to know the real number (for example: number 3, 4, 5, 6, 7 in the table view).
Is it possible?
You can call indexPathForCell: on the table view with the first and last objects of the array to get the two index paths.
solved!
using this
UITableViewCell *currentCell = [visibleCells objectAtIndex:i];
NSIndexPath *indexPath = [tabella_gallery indexPathForCell:currentCell];
works fine!
Here is a method I use
func loadImagesForVisibleRows() {
// Get the visible cells indexPaths
let indexes: Array = tableView.indexPathsForVisibleRows()!
// Loop through them to determine whether visible or not
for index in indexes {
let row = index.row
// Do what you need to do
}
}

getting user specific data from a webservice to populate a UITableView

I am trying to populate a UITableView with data for a specific user (his projects) from a webservice, after the user as logged into the app.
for example:
tableData = [[NSMutableDictionary alloc] initWithCapacity:DICTIONARY_INITIAL_CAPACITY];
//CURRENT PROJECT
//get the information from the server
NSArray *currentProject = [[IHMObjectFinderServices sharedIHMObjectFinderServices] getCurrentProjectAsArray:connectedUserId];
if(![currentProject isEqual:[NSNull null]]) {
//put the info in a dictionnary
NSMutableDictionary *currentProjectDictionary = [[NSMutableDictionary alloc] initWithCapacity:PROJECT_DICTIONARY_CAPACITY];
[currentProjectDictionary setObject:[currentProject objectAtIndex:INDEX_PROJECT_NAME] forKey:KEY_PROJECT_NAME];
[currentProjectDictionary setObject:[currentProject objectAtIndex:INDEX_PROJECT_MANAGER] forKey:KEY_PROJECT_MANAGER];
[currentProjectDictionary setObject:[currentProject objectAtIndex:INDEX_PROJECT_TAG] forKey:KEY_PROJECT_TAG];
NSArray *activitiesForProject = [NSArray arrayWithArray:[[IHMObjectFinderServices sharedIHMObjectFinderServices] getAvailableActivities:[currentProjectDictionary valueForKey:KEY_PROJECT_TAG]]];
[currentProjectDictionary setObject:activitiesForProject forKey:KEY_PROJECT_ACTIVITIES];
[tableData setObject:currentProjectDictionary forKey:KEY_CURRENT_PROJECT];
At the moment, my table loads in viewDidLoad method, so the problem is that the user is not yet connected when the table is constructed.
I have read about the reloadData method but I am not sure how to proceed to load the table only once, once the user has logged in. Could someone explain to me what is the correct procedure for this ?
Thanks for your help,
Michael
There should be some kind of call back method when the data for the tableView is available. Just put your reloadData in it.
- (void)dataIsAvailable
{
// ... your code to set the tableData ...
[self.tableView reloadData];
}