How to pass values between two Custom UITableViewCells in iphone - 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;
}

Related

Get the values in checked cells in a UITableView

I have a UITableView with each cell is having a UITableViewCellAccessoryCheckmark. The user can check and uncheck the cells depending on his preference. The user can check/select multiple cells in the tableview. A selected/checked cell can be unchecked and rechecked again on user preference.
I have a done button in the UITableViewController. On its click, I need to return to the previous view, before that I have to have a collection of the text in the checked cells(only checked cells).
How can I do this.
I was planning on developing a logic, by keeping an NSMutableArray and update it on - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath, when a cell gets checked/selected. But when every time a cell is unchecked I have to remove the item from the array and if the cell is checked again, then I have to add it again. I reckon thats not the right way to do this. What would be the right way to do this.
I couldn't find a question of similar kind here in Stackoverflow, which is very unusual. Would be helpful if someone could post a link, if the question was asked before.
Maintain two arrays, like this:
#property (nonatomic, retain) NSMutableArray *features, *selectedFeature;
synthesize them in .m file. initialize your both arrays something like this in viewDidLoad:
self.features = [NSArray arrayWithMyOwnResourceLikeDownloadedFromServerOrWhatever];
self.selectedFeature = [NSMutableArray array];
Then do something like this in didSelectRowAtIndex:
NSString * stirng = [features objectAtIndex:indexPath.row];
if ([self.selectedFeature containsObject:stirng]) {
[self.selectedFeature removeObject:stirng];
}
else{
[self.selectedFeature addObject:stirng];
}
and in your cellForRowAtIndexPath:
NSString * stirng = [features objectAtIndex:indexPath.row];
[cell.textLabel setText:stirng];
if ([self.selectedFeature containsObject:stirng]) {
//it is selected feature
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}
else{
//it is un-selected feature
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
Create a boolean array of the same length as the number of checkable cells and update the value on click events. If the table content is dynamic, you can use methods provided by NSMutabeArray to keep the boolean array in accordance with the table content. Upon return use the array to grab the text you need.

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

Selecting multiple rows of a UITableView

This page
http://networkpx.blogspot.com/2009/07/multiple-row-selection-with-uitableview.html
mentions a way to implement table views that can allow multiple selections of rows.
At the time of this article, it seems that it was not a blessed way of doing this.
Now, apparently, Apple is allowing this kind of tableViews.
The article mentions this
NSArray* selectedRows = [tableView indexPathsForSelectedRows];
as a way of getting a list of all rows selected but this is not a legal functionality of the SDK.
The big question is: how do I get a list of all rows selected, so I can perform an action with them?
thanks
EDIT
To answer some questions... this is the code I am using to discover if a row is selected, but this is giving me zero entries.
NSMutableArray *selectedRows = [[NSMutableArray alloc] init];
for (int i=0; i<[list count]; i++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
UITableViewCell *aCell = (UITableViewCell*) [myTable cellForRowAtIndexPath:indexPath];
if (aCell.accessoryType == UITableViewCellAccessoryCheckmark) {
[selectedRows addObject:indexPath];
}
}
// selectedRows has always 0 entries... all cells give me their type as UITableViewCellAccessoryNone even those with checkmark
Apple will reject your app for for iOS 4 and below.
We found the following non-public API/s in your app:
indexPathsForSelectedRows
Create a NSMutableArray.
Whenever a table cell is selected insert the current indexpath in the array. whenever he deselects the cell remove it from the array. Final array will have everything you selected.
MultipleCheck in Table – UITableView Example with Demo
http://sugartin.info/2011/08/19/multiplecheck-in-table-uitableview/
Are you looking to have the user select multiple rows at the same time? If not you could create an array that holds each row that is selected as the user selects them.
Also for the check mark you could just subclass UITableViewCell.
How to subClass UITableViewCell and use it to not clear UILabel background color on UITabeViewCell selected?

UIButton inside UITableViewController

I have a UIButton that is created inside of each table cell. I want to hook up a touch event like so:
[imageButton addTarget:self
action:#selector(startVote:)
forControlEvents:UIControlEventTouchUpInside];
I want to pass data about the current row (the id of the object for that row) to the startVote method. Is there a method that I am missing to do this or am I breaking some best practice. This seems like a very normal thing to do?
I assume you have some sort of NSArray with the data that gets passed on to the buttons in cellForRowAtIndexPath.
Try this in startVote:
- (void)startVote:(id)sender {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSDictionary *myData = [myArray objectAtIndex:indexPath.row];
}
EDIT:
If for some reason the row is not selected, you can assign a unique tag to every button upon creation and then:
- (void)startVote:(id)sender {
int myTag = [(UIButton *)sender tag];
NSDictionary *myData = [myArray objectAtIndex:myTag];
}
Maybe you would do some sort of operation with the tag so it can be used as an index (I add a certain amount to every tag so it will not conflict with "automatic" tagging used by the OS.
The UITableViewCell doesn't know, out of the box, what row it's displaying in the table. Remember, the intent is that the same cell instances are re-used all over the table to display its data. That said, your UITableViewController is responsible for setting up the cells and passing them to the system (and has the index path, of course). You could, at that point, do something like:
Assuming it's a custom cell class, set a property on the cell instance to identify what row it's displaying, and which your button can later use.
If you're putting these buttons in the cells as their accessory views, take a look at the table delegate's tableView:accessoryButtonTappedForRowWithIndexPath: method.
If it's a one-section table, you could do something really cheesy like store the row index in the button's tag property. Your startVote: method is passed the button, and could then extract its tag.