XCode edit UITableView textFields for different rows - iphone

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.

Related

Getting Data from a tableview and store to sqlite database

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).

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

Check all items in iPhone UITableView

I have a UITableView where every cell either gets/loses a checkbox or goes to another table view when clicked. For every table, I want to add a check all button that checks off every cell in that table and all the ones inside it. How do I allow one UITableViewCell to toggle the checkbox of every one in it's TableView?
EDIT: Removing the last index in the index path, and replacing it by the new row number, I'm able to go through the entire table and check it off, like this:
for(int i = 0; i < [tableView numberOfRowsInSection:0]; i++){
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:[[indexPath indexPathByRemovingLastIndex]
indexPathByAddingIndex: i]];
newCell.accessoryType = UITableViewCellAccessoryNone;
}
Oddly, it skips over every tenth cell or so - and it's not even consistent. For a list of years, sometimes it doesn't check off 2003 and 1993, sometimes 2003 and 1991, and and so on. It's always around every tenth cell or so, but I can't find a pattern.
When you click the cell didSelectRowAtIndexPath will be called, at that point all you gotta do is use tableView cellForRowAtIndexPath:indexPath method in order to get back all your cells one by one and set their accessory type to checkmark...hope this helps
EDIT: Another way to do it is, if you have maybe some sort of array (hard to tell you what to do exactly without looking at your code), but if you have some array that holds values as to which cells need have check marks on them, then you can use in cellForRowAtIndexPath to assign the checkmark accessory, so when your cell is selected, you can set all the values in the array to what you need them to be and just call UITableViews reloadData...hope that made sence :), heres a little snipet of code to try and make it more clear
lets assume you have 1 section and x amount of rows
NSMutableArray shouldHaveCheckmark; //this array will be of size x
//(one for each row) and hold NSNUmbers 0 means not checked 1 means checked you initialize
//this somewhere (viewDidLoad maybe) and should be declared in your .h file
//this is cellForRowAtIndex path
-(UITableViewCell)cellForRowAtIndexPath:(NSIndexPath*)path
{
UITableViewCell *cell=...
if([[shouldHaveCheckmark objectAtIndex:path.row] boolValue] )
//assign checkmark accesory
//other code
return cell;
}
So you can see in didSelectRowAtIndexPath you can just flip the numbers in the array and call reloadData on the tableView...that approach will work as well

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

Referencing UISwitches within UITableCells

I have a table view which I'm using for some settings in my app. The tables cells are all default (no customisation at all), and simply contain some text for their label and a UISwitch for the accessory view.
My problem is that I need a reference to the switch so that I know when it has been switched on and off.
Currently I am setting the 'tag' property of the switch to be that of the cell's index within the table (grabbed from [indexPath row] in tableView:cellForRowAtIndexpath:).
This is fine when you only have one Section in your table, but I am now adding a new section. The problem is that they are both 0 based indexed so the switches in each section will end up reusing the tags - which isn't good.
Any suggestions on a better way to achieve this?
Thanks.
If you know roughly how many sections and rows you will have, like oh, say, not more than 1 million rows per section, just hash the section and row like this:
const int oneMillion = 1000000;
int tag = (section * oneMillion) + row;
slider.tag = tag;
Then, to figure out the section and row, reverse the logic:
int tag = slider.tag;
int row = tag % oneMillion;
int section = tag / oneMillion;
NSIndexPath *indexPath = [NSIndexPath indexPathForRow: row inSection: section];
Now get the slider that is in the cell in that section,row of the table
UITableViewCell *sliderCell = [tableView cellForRowAtIndexPath: indexPath];
UISlider *slider = [[sliderCell.contentView subviews] objectAtIndex: 0];
This assumes the slider is always the only view in the contents of the cell.
This method is a bit longer than some of the other suggestions above, but it keeps you from having to cache references off to the side.
For each cell, set a delegate link back to the table view controller, and also some kind of row reference ID - then wire the switch to a cell IBAction method, that calls back to the delegate with the reference ID for that cell.
What you can do is either have an Array of arrays or a dictionary, key it by the section number (or in case of the array they will be in order of the section numbers), now to retreive a switch all you do assuming you know the section and the row number
UISwitch *switch=[[switchArray objectAtIndex:section] objectAtIndex:row];
or if you have a dictionary
UISwitch *switch=[[switchDictionary objectForKey:section] objectAtIndex:row];