How to get the index value of a TextField defined in table - iphone

I have a table view in which I'm showing some text fields. My problem is I want to read the data from those text fields. I am able to get the value but I want to know which text box the value is from, ie I want the index of the text box.

you can use UITabeViewCell's index... it would be same...
you can get it
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger row = [indexPath row];
//row is your index

Textfields within a tableviewcell do not have index values because they are not part of the table itself but are merely ordinary subviews of the tableviewcell's content view.
Therefore, you access them just like you would any subview. The tableviewcell's content view is the textfields superview.
If you use one of the standard tableviewcell styles, then you can easily access them by name e.g. myTableViewCell.displayLabel.text. If you have custom cell class you can make references the textfields properties of the class and access them by property name.

Related

Special TableView in Iphone SDK

As you see above, I have a Table View on LeftSide which contains a Some Text.
But when i selected any annotation in map according to that the cell will be Highlighted & it has more text rather than other Cell and also background color is changed.
How can i achieve this?
You can approach in following way.
First both table data and annotation pins in Map are filled from an array!!
What you can do, you can define "tag" as array index to particular item.
When a user tap on a annotation, that annotation has a "tag" (or array index) and this "tag" (or array index) also has an item for table data.
On click of annotation tap, you have to reload your table and make that particular tableview cell highlighted.
You need to customize the UITableViewCell for this
Add a table view and map view in your view as shown in figure.
Load the custom table view cell to tableview
Initially give the needed color for all table cells
When user selects a cell change it's color
Using didSelectRowAtIndexPath: delegate method show the corresponding value on map.
Check this link for tutorials:
Appcoda
The basic sample for slide menu in iOS
https://github.com/nverinaud/NVSlideMenuController
Hope this sample code will helps you
I think you are creating custom annotation instead of the default one so if it is possible for you to set tag for each annotation view which will be same as tableview cell index. While selecting an annotation create indexpath with that tag and set selectedrow for that indexpath. Hope it may help you.
Create your customized class of UITableViewCell.
Understand you have two conditions A. Normal Cell B. Cell after the click.
You'll have to use 3 delegate methods of UITableView to achieve this.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Here you'll have to check for the condition you'll have to check which indexPath.row is selected. and according to that you'll have to change the height of your row.
return hight;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Again the same thing. You'll have to check the condition and load the appropriate controls with appropriate frames.
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// This is the important function for you.
// Here you'll have to set one counter. Which you'll use in the
// After that reload the table.
}

UITableView renames every eighth cell

I have a UITableView in my MainViewController. When a user taps a cell,
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
selectedRow = indexPath;
....
[self performSegueWithIdentifier:#"OtherViewControllerSegue" sender:self];
}
and they are taken to another UIViewController (let's call it OtherViewController). In OtherViewController, the name for the selected cell is set. When OtherViewController is dismissed, it updates the cell in MainViewController with the new name:
[[[mainvc.myTableView cellForRowAtIndexPath:mainvc.selectedRow] textLabel] setText:namecell.textField.text];
[self.navigationController popViewControllerAnimated:YES];
This all works fine until I have more cells than will fit on the screen. If there are more cells than will fit on the screen (8 for iPhone or 16 for iPad), then this will also set the name for every eighth or sixteenth cell respectively. Any ideas on what I am doing wrong?
Update:
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [pointsTableView dequeueReusableCellWithIdentifier:#"myTableCell"];
return cell;
}
This is due to cell-reuse and you are mixing up your model with your view (in the MVC context).
A table-cell is a transient thing, once it goes off the screen it is reused (instead of creating new cells) when another cell is needed. This is what the dequeueReusableCellWithIdentifier: method does.
This means you can't store data in there and expect it to still be valid later on. In this example you are trying to store the name in the table cell. The reason to set a property (like the label text) on any view object is purely for display, not for storage. So to solve this problem you should maintain a list of objects in your model (this could be in separate classes or in an array in your mainvc object for example). Then in cellForRowAtIndexPath: you should set the label text every time - even when there should be no label you need to set it to nil or an empty string because the cells are re-used it might contain something from the last time it was used.
Update:
Instead of calling cellForRowAtIndexPath: yourself and setting its text, you should set the text in your model using a method or property in your controller and then tell the table view to reload that cell. The code might look something like this:
// This code is in where you want to set the text from
[mainvc setText:someText forIndexPath:indexPath];
.. and in your main view controller:
- (void)setText(NSString*)newText forIndexPath:(NSIndexPath*)indexPath
{
// Store the text in your model here...
...
// If the view is loaded, the table view should reload the cell.
if(self.isViewLoaded)
{
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
The table view will then call cellForRowAtIndexPath: where the text will be set correctly. This may seem a little convoluted at first, but when you get used to using the Model-View-Controller design pattern you will find that keeping the jobs of each MVC component separate like this will mean your code is tidier, easier to understand, has less bugs, is easier to update/extend, etc.
You're trying to store data (the new name) in a view (the cell's label). What's probably happening is that when you re-use cells in the data source's cellForRowAtIndexPath method, some of them are ones that have had this text set for them and it's still there.
The better idea is to make your changes in whatever array you use as cell information and then reload the table view to make the changes visible.
As I suppose, you shouldn't call cellForRowAtIndexPath by yourself. It can be called to create cell, not to change it.
You can update your table by passing needed string to the first view via delegate, for example. And on the event (user sets the name) you can update all table and set needed names to cells.
Hard to say exactly what the problem is, but one possible solution might be this:
Make sure that in your cellForRowAtIndexPath you are initializing the cells like this:
// Create the Cell
static NSString *recordCell = #"pickerTableCell";
cell = [tableView dequeueReusableCellWithIdentifier:recordCell];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:recordCell];
}
I know this is primarily a memory solution, but might gelp here too.
Also, look through your code and check how you are determining which cell is renamed. You could be accidentally calling the rename on more than one cell without realizing it

How to make UITableView footer for section not sticky [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
TableView Footer is scrolling with the table
I want to have a footer for each section that will not be sticky and would scroll with the table
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
This method defines a view for the footer that is "sticky".
Thank you in advance
Assuming you have a means by which to know when you're at the end of the data set for a particular section, why don't you just tag on a custom UITableViewCell that appears as the section footer you want? It's very simple to write the logic within tableView:cellForRowAtIndexPath: that would check if the cell being requested is within the data set for that particular section. As soon as the indexPath.row being requested is 1 greater than the available data (generally contained in an array, so if indexPath.row is equal to [array count]), return your "footer" cell. This will scroll along with the table as it scrolls, since it's just another cell. You can make it look as differently from the regular cells as you'd like.
Additionally, you would need to tell your table view that each section will have one extra row, so if you're saying something like
return [array count];
You would need to say, instead,
return [array count] + 1;
in your numberOfRowsInSection method.

Add blank cell at end of UITable

I'm trying to add an additional cell for custom content as the last cell in a tableView, without altering the dictionary that creates the other content of the table. I think the place to add it is in cellForRowAtIndexPath rather than adding one to the numberOfRowsInSection, which just crashes if I do.
I get a cellCount from the dictionary that creates the data for the table, then in cellForRowAtIndexPath, I have:
if (indexPath.row == cellCount) {
... stuff goes here
return cell;
}
of course, this never gets called. If I do if (indexPath.row == cellCount -1) it overwrites the last cell with content.
I can get this to work if I add a blank entry into the xml from which I am populating the dictionary, but that's ugly.
Example code would be neat!
The problem here is that tableviews are designed to easily and accurately display the contents of you data-model and you've decided you don't want to do that. You're fighting the API.
The straight forward way to do this would be to put a check in numberOfRowsInSection such that it adds one to the row count when you want to display the input row. Then in cellForRowAtIndexPath: you will need to check if the table view is asking for the input row and return the appropriate type of cell.
From a UI design point, you might want to consider whether this setup is the best. This isn't a standard setup. Is the user going to understand that they can only edit the last row of the table? Will they understand they can't edit the other rows? Does anything in the UI tell them how all this works? Does the user have to scroll to the end of the table every time they want to add data? How long can this table grow? How will displaying a keyboard for the last row of the table affect how table scrolls?
I think it would be a better design to use a footer view to display the text field such that is is visually distinct from the rest of the table. It would be programmatically simpler as well. You wouldn't have to check if the table was asking for the last row every single time it ask for a cell.
Edit:
In thinking about it, perhaps a sectioned table would be simpler. Just put the special row in its own section (with or without a header.) That would simplify you handling of the rows that source from the dictionary because the row count in that section would always be the count of the dictionary. Likewise, you could just check the section attribute of the indexpath to know what cell to return for what row.
First you need to modify the numberOfRowsInSection to return +1. Then in cellForRowAtIndexPath you need to add that extra blank cell.
You need to provide for the extra cell in both cellForRowAtIndexPath and numberOfRowsInSection.
Assuming that cellCount is the actual number of cells in your array then: (a) in cellForRowAtIndexPath return the extra custom cell when indexPath.row == cellCount, and (b) in numberOfRowsInSection you need to return cellCount+1.
Assuming a single section, an example would go something like this:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [journalArray count] + 1; // add 1 for summary
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row < [journalArray count]) {
return [journalArray objectAtIndex:indexPath.row];
} else {
return summaryCell;
}
}

How do I set a custom cell for non databound TableCells

I am happily able to set the styling of my tableviewcells using the cellForRowAtIndexPath delegate, but I want to set the background of all the nonpopulated cells which are still on the screen
hey, basically any cell which is part of your tableview belongs to a particular index and particular row
so incase you are not populating a particular section via data source - you can still get a reference to a cell in that indexPath by manually calling the cellForRowAtIndexPath delegate, and passing the section as well as row index
it returns you a cell. assign it to your private variable and edit it the way you want to.
Use the visibleCells method on your table view. Then do what you wish to the cells.
Alternately, you could call indexPathsForVisibleRows to get just the index paths. Then call cellForRowAtIndexPath: to get the cell corresponding to each index path.