Getting Data from a tableview and store to sqlite database - iphone

I Have a form with Tableview and Custom Cells with many number of text fields,Label and textViews.If a user Fills All fields and He want to save the Form then How can i get all the field values on my Button click method ?
I would highly appreciate it if someone could help me out by showing me how to do this.

// get the path of cell from row and section
-(void)btnClick
{
NSIndexPath *path = [NSIndexPath indexPathForRow:0 inSection:0];
customTxtField *cellCustomTxtField = (customTxtField *)[mytableview cellForRowAtIndexPath:path];
NSString *str = cellCustomTxtField.text1.text;
}

Don't wait until the button click event to retrieve the cell contents. Because cells are reused (e.g., during scrolling), the values in each cell will not be preserved.
As cell values change, update the model that drives your table by making the tableViewController a delegate for the respective input fields.
For example, make the tableViewController the textField delegate and update the model as the user inputs data. To distinguish among textFields in the tableViewController, assign a tag to each textField when creating the cell (i.e., tableView:cellForRowAtIndexPath).

Related

XCode edit UITableView textFields for different rows

I have a program that displays a table with just 1 Cell row with the customer's information to be keyed in using 2 textFields. (Shown below)
The user is able to click on the Addition sign to add a new row to their interests.
When the user selects the Main Category, the Sub Category should be auto filled with information.
My problem is, when i have say 2 rows of cells, I'm unable to auto populate the second subCategory with the selection from the mainCategory because I have no idea how to identify them.
I've implemented both the UITableView/UIPickerView delegates in this view.
Use the convenient tag attribute of the text fields. You can set these in Interface Builder inside Xcode to distinguish between your two fields.
Now you can identify the particular row and its index path like this, say, in textField:didBeginEditing:
#define mainTag 2
#define subTag 3
UITableViewCell *cell = (UITableViewCell*)textField.superview.superview;
NSIndexPath *indexPath = [_tableView indexPathForCell:cell];
UITextField *mainCategory = (UITextField*)[cell viewWithTag:mainTag];
UITextField *subCategory = (UITextField*)[cell viewWithTag:subTag];
The trick is that the text field is a subview of a view called contentView which is part of every UITableViewCell, which again is a subview of the cell itself.

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;
}

Save the state of a table view/ save the state of the table view text

I have a UITableView and i can add and delete cells. I also have two buttons on each cell to add and subtract 1 from the cells text. But when i go to a different page and then back to the table view page, all the cells text is set back to 1. But i want the cell's text to stay at the value that the user had set it to! Could someone help me? Im not sure what to do whatsoever. Thanks!
You will have to maintain an NSMutableArray (probably of NSIntegers) that saves the values of the cells. Then, whenever the user changes the value, update the value in the array. Also, display the cell label values by reading from the array. Sample codes below-
-(void)plusButtonClicked:(id)sender
{
//figure out the cell index which was updated using sender
//update the array entry
//[self.array objectAtIndex:index]++; self.array is the array you will maintain
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.label.text = [NSString stringWithFormat: #"%d", [self.array objectAtIndex:indexPath.row]];
}
If you want the values to persist even after the app is terminated and restarted, consider using CoreData.

UITableView reload cells' view

How can I reload the whole tableview? In -tableView:cellForRowAtIndexPath: I'm comparing data online with data on user's device and if it's not the same then I am displaying a button to download content. After actions connected to this button I'd like to refresh the whole tableview to complare again the content online with local one.
reloadData does in fact update values but it doesn't remove these buttons. I'd like to change the way cells are displayed..
In other words I want this condition (in -tableView:cellForRowAtIndexPath:) to work and it doesnt:
if (![[NSFileManager defaultManager] fileExistsAtPath:someDir isDirectory:nil]) {
UIButton *bt = [UIButton buttonWithType:UIButtonTypeRoundedRect];
(...)
[cell addSubview:bt];
}
To update the table now I have to pop the viewcontroller and push it again. I'd like to do it with button on navBar.
Thanks in advance.
That is because you are caching cells with dequeueReusableCellWithIdentifier. Check the docs. You should cache 2 types of cells, with and without the button.
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier: #"your-id"];
if (cell)
{
//Check if it should contain a button. If it shouldn't remove it
}
else
//Create a fresh new cell

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;
}