Issue when getting data from text field in dynamic cell - objective-c

I have created a dynamic table, but I'm facing an issue when I try to access the content of a text field in the first cell. The textfield I get is always empty, even though I enter some text in it before calling the action.
Here is my code. Could you help me to find what's wrong in it ?
CustomNameCell.h
#interface CustomNameCell : UITableViewCell
#property (retain, nonatomic) IBOutlet UITextField *nameTextField;
#end
CustomViewController.h
#interface CustomViewController : UITableViewController
#property (retain, nonatomic) IBOutlet UITableView *tableview;
- (IBAction)search:(id)sender;
#end
CustomViewController.m :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 1) {
static NSString *CellIdentifier = #"nameCell";
CustomNameCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil) {
cell = [[CustomNameCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
return cell;
}
...
and the action when clicking a button, where I try to get the content of the textfield :
- (IBAction)search:(id)sender {
static NSString *CellIdentifier = #"nameCell";
CustomNameCell *nameCell = [_tableview dequeueReusableCellWithIdentifier:CellIdentifier];
here nameCell.nameTextField.text is always equal to #""
Why is my text field always empty ?
Thank you for your help ;)

If you need a cell you should get it like this:
CustomNameCell * cell = (CustomNameCell *)[tableview cellForRowAtIndexPath:indexPath];
NSString * textInField = cell.nameTextField.text;
If you tell me what logic do you need I'll expand my question. For now - you can use this method.
Content of the first cell will be:
CustomNameCell * cell = (CustomNameCell *)[tableview cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
NSString * textInField = cell.nameTextField.text;

You shouldn't call [_tableview dequeueReusableCellWithIdentifier:CellIdentifier];within your - (IBAction)search:(id)sender as it is a mechanism to reuse a cell during the creation of your tableView. Instead of creating new cell when you are scrolling your tableview you are reusing some cells you have previously created.
Bottom line: use instead the cellForRowAtIndexPath to access a cell at a specific row in your section and if you want the first cell of the first section it would be:
CustomNameCell * cell = (CustomNameCell *)[tableview cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];

I just do in this Way. I dont know it is the current way to do that.
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return cellContentArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellId=#"cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
UITextField *firsttext=[cell viewWithTag:101];
UITextField *second=[cell viewWithTag:102];
[second setTag:(indexPath.row)+1000];
[firsttext setTag:(indexPath.row)+100];
return cell;
}
- (IBAction)buttonPressed:(id)sender
{
for (int i=0; i<cellContentArray.count; i++) {
UITableViewCell * cell = (UITableViewCell *)[baseTableveiew cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
UITextField *tempTextField=[cell viewWithTag:i+100];
UITextField *secondTextField=[cell viewWithTag:i+1000];
NSString * textInField = tempTextField.text;
NSLog(#"tempTExtTiedl tag %ld data %#",(long)tempTextField.tag, textInField);
NSLog(#"second tag %ld data %#",(long)secondTextField.tag, secondTextField.text);
}
}

Related

didSelectRowAtIndexPath to assign values

I'm having problems with a , I guess, simple thing.
I have a (It is NOT a SplitViewController) ViewController with a TableView object in it.
I want that when the users click a row in a table, my label on the right side change appropriately.
My label (with the text "Nome do Vinho" on the right) that i want to change
#property (weak, nonatomic) IBOutlet UILabel *rotuloDetalhesLabel;
I populate the TableView with this code.
I create the objects on viewDidLoad method and put in a NSArray called "vinhos".
// Create object
Vinho *vinho1 = [Vinho new];
vinho1.rotulo = #"Adega de Borda Premium";
Vinho *vinho2 = [Vinho new];
vinho2.rotulo = #"Adega de Borda Premium 2";
// Populate the NSArray with the objects that I create before
vinhos = [NSArray arrayWithObjects: vinho1, vinho2, nil];
Populating the TableView
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Configura a Cell
static NSString *CellIdentifier = #"TableCell";
TableCell *cell = (TableCell *)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[TableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Vinho *vinho = [vinhos objectAtIndex:indexPath.row];
// rotuloLabel is the Label of the row
cell.rotuloLabel.text = vinho.rotulo;
return cell;
}
I guess that it is made on didSelectRowAtIndexPath method but I dont know how.
If I forget to post something important just tell me.
Do you wan't to change the label on the right to the cell text?
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Vinho *vinho = [vinhos objectAtIndex:indexPath.row];
NSString *cellRotuloText = vinho.rotulo;
rotuloDetalhesLabel.text = cellRotuloText;
}
If you wan't the fade animation as you said in the comment, maybe this could get you started: xCode ios5: How to fade out a label text after an interval?

Displaying next data in Detailview of my NSMutableArray

I have a simple problem.
Im using a NSMutableArray for multiple Questions and Answers. I`ve put all the data in a tableView and put the data of the selected row in a Label. Now, how do i get the data from the next row using a button? in think i need something like this:
question.text = [questions objectAtIndex:indexPath.row+1];
But where to put it? If i use this line outside the cellForRowAtIndexPath Method it won't accept the indexPath.
I`m sure there is an easier way than this but with the words of Frank Sinatra: It is my way. :-)
Here is the complete methode:
- (NSInteger)tableView : (UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [questions count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"questionCell";
UITableViewCell *cell = [questionTableView
dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:simpleTableIdentifier];
}
if ([content.text isEqualToString:#"Unfallverhütung"]) {
question.text = [questions objectAtIndex:indexPath.row];
}
return cell;
}
question is the Name of my Label.
questionsis the Name of the Array.
I`m greatful for any advice.
THX
When you say
it won't accept the indexPath
What you mean is
I don't have the indexPath
So, you need to store it. You can just add a property, like:
#property (strong, nonatomic) NSIndexPath *selectedIndexPath;

How to get a custom UITableViewCell class created in IB to load into a UITableView?

I am new to this so please bear with me:
I have a .xib in IB containing a UIScrollView that has a UITableView embedded in it. I would like to load custom UITableViewCells into the UITableView. I have four/five custom cells created in IB but cannot get the first to load correctly.
Here are relevant screenshots from IB:
I have set the class for the custom UITableViewCell created in IB to the UITableView class I created called "itemCell". Additionally, I have connected the three text fields outlets and delegates to "itemCell".
Here is itemCell.h:
#import <UIKit/UIKit.h>
#interface itemCell : UITableViewCell
#property (weak, nonatomic) IBOutlet UITextField *quantityTextField;
#property (weak, nonatomic) IBOutlet UITextField *itemTextField;
#property (weak, nonatomic) IBOutlet UITextField *priceTextField;
#end
Here is itemCell.m:
#import "itemCell.h"
#implementation itemCell
#synthesize quantityTextField,itemTextField,priceTextField;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#end
In filecontroller.m, which is the rootcontroller .xib's implementation file I create a new cell based on the user pressing a button (add it to an array) and then reload the tableview. I am getting no errors but I am getting a normal cell loaded in rather than what I have created in IB. Here is rootcontroller.m. Any help is appreciated.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
scrollView =(UIScrollView *)self.view;
items = [[NSMutableArray alloc] init];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [items count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
return [items objectAtIndex:[indexPath row]];
}
- (UITableViewCell *)initiateNewCell{
itemCell *cell = [[itemCell alloc] init];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
return cell;
}
- (IBAction)addItem:(id)sender {
//creates the new cell to be added
[items addObject:[self initiateNewCell]];
[self.tableView reloadData];
As of iOS 5 you can now load a UITableViewCell from a nib for cell reuse with the following function.
- (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier
First make sure you create an IB file that contains only the UITableViewCell
Under Identify inspector: Set the class of the UITableViewCell to "itemCell"
Under attributes inspector: Set the identifier of the UITableViewCell to "itemCellIdentifier"
In viewDidLoad: place the following code, and replace itemCellNibName with the name of the nib that contains the UITableViewCell.
NSString *myIdentifier = #"itemCellIdentifier";
[self.tableView registerNib:[UINib nibWithNibName:#"itemCellNibName" bundle:nil] forCellReuseIdentifier:myIdentifier];
In cellForRowAtIndexPath: place the following code, you can now access your IBOutlet properties you set on the itemCell, and connected in the nib.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
static NSString *myIdentifier = #"itemCellIdentifier";
itemCell *cell = (itemCell *)[tableView dequeueReusableCellWithIdentifier:myIdentifier];
cell.textLabel.text = [NSString stringWithFormat:#"Test Row:%i!",indexPath.row];
return cell;
}
Also: You do not want to include the UITableView inside a UIScrollView, a UIScrollView is already built into the UITableView, which allows for proper cell queing.
Also: I suggest you just get the bare bones of this going first, before you start pulling data from an array.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 10;
}
Also: As someone else had mentioned, the array contains the data that will populate the UITableViewCells, not the actual cells themselves.
In your
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//This identifier string needs to be set in cell of the TableViewController in the storyboard
static NSString *CellIdentifier = #"Cell";
// Resuse the cell
SomeCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// If there are no cell to be reused we create a new one
if(cell == nil) {
cell = [SomeCustomCell new];
}
// Configure the cell...
NSString *someString = [yourArrayWithStringsOrWhatEver objectAtIndex:indexPath.row];
// Setting the some property of your custom cell
[cell.yourSpecificCellProperty someString];
[cell.yourOtherSpecificCellProperty setText:#"What Ever your want!"];
}
Your cell's properties could be whatever your have connected to your custom cell in interface builder.
Hope it helps!
Well there are a few things that aren't right:
The class name begins with upper case letter itemCell -> ItemCell
You should reuse the cell. The way you want to do this, you will create a separate cell for each index path.
the items array should only contain the data source of your table, not your cells
instead of calling reload data, you can add just one row to the table, and it can be animated too
Try something like:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
scrollView =(UIScrollView *)self.view;
items = [[NSMutableArray alloc] init];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [items count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[NSBundle mainBundle] loadNibNamed:#"itemCell" owner:self options:nil].lastObject;
cell.reuseIdentifier = CellIdentifier;
}
// Do any additional setup to the cell
return cell;
}
- (IBAction)addItem:(id)sender {
[items addObject:[NSNumber numberWithInt:items.count]]; // Here comes the data source
[self.tableView insertRowsAtIndexPaths:#[[NSIndexPath indexPathForRow:items.count - 1 inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];
}
PS: I wrote it without an IDE so there might be some errors

Can't identify if tableView parameter is seachResultsTableView

I'm developing an iOS 6 application.
I want to add a search functionallity to a UITableView following this Table Search example from Apple. But, my implementation doesn't work.
On - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath I don't know how to if tableView parameter is searchDisplayController.searchResultsTableView.
Take a look at this screenshot:
As you can see, tableView is a UISearchResultsTableView, but this line:
if ([tableView isEqual:self.searchDisplayController.searchResultsTableView])
It is always false.
Maybe, I need to add an outlet to access seachResultsTableView, but I haven't found that outlet on TableSearch example.
This ViewController.h:
#interface ViewController: UIViewController <UITableViewDelegate, UITableViewDataSource, UISearchDisplayDelegate, UISearchBarDelegate> {
What do I need to do to make it work?
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if(searching)
{
cell.textLabel.text = [searchedListOfItems objectAtIndex:indexPath.row];
}
else {
NSDictionary *dictionary = [self.tableDataSource objectAtIndex:indexPath.row];
cell.textLabel.text = [dictionary objectForKey:#"Title"];
}
return cell;
if all you're trying to achieve is a normal search then use this.

Uilabel and a Tablecell

A small issue i created a custom UITablecell but in the cell it's need to parse data so i connected IBOutlet UILabel *One; to an UILabel but when i'm doing
One.text = #"Lorem..."; an error show's up i imported the UITablecell.h in mijn viewController.
**Use of undeclared identifier 'One'**
/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
ViewControllerCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray* views = [[NSBundle mainBundle] loadNibNamed:#"ViewControllerCell" owner:nil options:nil];
for (UIView *view in views) {
if([view isKindOfClass:[UITableViewCell class]])
{
cell = (ViewControllerCell*)view;
}
}
}
One.text = #"Lorem...";
return cell;
}
In this case your instance of your custom UITableViewCell class will be cell, so you'll need to access it like this
cell.One.text = #"Lorem..";
First you have to Typecast the tableViewCell to your Custom Cell.
{
YourCustomCell *objCustomCell=(YourCustomCell *)[tableView cellForRowAtIndexPath:indexPathForThatRow];
objCustomCell.One.text=#"YourDesiredstringvalue";
}