no value entered in TableView row - iphone

I have a basic view with a UITextField and a Tableview. I am able to enter text to the TextView and capture the value through an IBOutlet and IBAction.
My IBAction is setMyNote within which I call the cellForRow... method.
In my cellForRow.. method I have this snippet code
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSString *enNote = [[NSString alloc] initWithString:enteredNote.text];
NSInteger nRow = [indexPath row];
switch (nRow) {
case 1:
cell.textLabel.text = enNote;
break;
default:
break;
}
[enNote release];
return cell;
}
When I run my debugger, I can see that enNote has the new text I entered on the textView and it seems to get assigned to my cell.textLabel.text too -- but my table does not show this new text. What am I missing?

You shouldn't call the cellForRow directly.
When the table view renders itself it calls the cellForRow method in order to create the cells that should be visible.
I think that you should update your table view data source instead of trying to update the cell directly and then reload the table view ([tableView reloadData];).

Related

TableView adding a cell

I'm doing a project in which I have a TableView in which I have different Rows/Cell and in each cell there is a add button. What I need to do is on clicking the add button the new row should add below the row in which I clicked the button, means I have to add a cell to the next of the selected button on the row.and also a user can enter a text on the new row. Please tell me how to do this . As I'm new in iPhone development. I will be very thankful ....
Here is a click button function in which I want to perform an action to add new cell.
- (IBAction)AddButtonAction:(id)sender
{
[_choices insertObject:#"avav" atIndex:[_choices count]];
[self.tableView reloadData];
NSLog(#"here");
}
Use this delegate method to add a new row below the selected row. And use custom cell to have a text field on it...
rowCount ++;//Here rowcount refers the no. of rows in the table.
selectedIndexPath = indexPath;//Assign the selected indexpath for creating custom cell on it.
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationBottom];//Indexpath refers the currently selected/targeted cell.
In cellforRowAtIndexPath use like this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if((selectedIndexPath) && (indexPath.row == selectedIndexPath.row) && (indexPath.section == selectedIndexPath.section))
{
static NSString *cellIdentifier=#"cell";
NewCell *cell = (NewCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
NSString *customeCellName = [NSString stringWithFormat:#"%#",[NewCell class]];
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:customeCellName owner:self options:nil];
for (id currentObject in topLevelObjects)
{
if ([currentObject isKindOfClass:[UITableViewCell class]])
{
cell = (NewCell *) currentObject;
break;
}
}
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
return cell;
}
else
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [NSString stringWithFormat:#"%d",indexPath.row];
// Configure the cell...
return cell;
}
}
OutPut will be like this
need to customize as per your requirement.
I consider you have textfeild in all cells. In AddButtonAction function save index of row. While creating cells make selected cell textfeild firstresponder. Once user enter data save it in array and again reload. Just keep track of selectedIndex properly.
Ifeel this will help.
If you add button to cell by addSubview method, simply you can get indexPath with the following code:
- (IBAction)AddButtonAction:(id)sender
{
UIButton *button = (UIButton *)sender;
UITableViewCell *cell = (UITableViewCell *)[button superview];
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
// Adding methods comes here...
}
If you are using some addSubview levels, you should use same superview methods inverse to access the UITableViewCell level. If using custom cells, replace your custom cell classname with UITableViewCell in the above code.
For adding cells, take a look at WWDC 2011 Videos, "UITableView Changes, Tips & Tricks" video. It shows some nice and useful methods to insert, delete and reload new rows/sections between other rows/sections like insertRowsAtIndexPaths:withRowAnimation: method. Or see Apple Documents.

uitextfield of custom table cell

I am creating a table with three different types of custom cell, One of them, one custom cell is having UITextField. I create two rows using this custom cell. I set tag and delegate for textfields of both row.
My problem is, when I scroll the table, these rows with textfields move up and disappear from the screen, when scroll down, that time my app gets crash.
I get an error like
-[CellImageViewController txtField]: unrecognized selector sent to instance 0xa0ea5e0
Here is my code for cellForRowAtIndexPath:
if (indexPath.row == 0 )
{
if (cell == nil) {
cell = [[[CellWithTextFieldViewController alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID] autorelease];
}
cell.txtField.tag =1;
cell.txtField.delegate = self;
cell.txtField.text = #"kjfkd";
}
return cell;
}
else if(indexPath.row==1)
{
if (cell == nil) {
cell = [[[CellWithTextFieldViewController alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID] autorelease];
}
cell.txtField.tag = 2;
cell.txtField.text = #"glk";
cell.txtField.delegate = self;
return cell;
}
Any one have idea about this issue?
you have different type of cells, so you need to use a cellIdentifier different for each one.
Try this:
customOrNormalCell *cell [tableView dequeueReusableCellWithIdentifier:CellIdentifier]
if(!cell){
cell = [[CellWithTextFieldViewController alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:#"%i", indexPath.row]];
}
With this code, each cellIdentifier will be 1,2,3,4... (All different)
I'm pretty sure that it will solves the problem
create UITextField before create UITableView and Add your Textfield object in cell Content view
[cell.contentView addSubview:yourTxtFieldOblect];
It looks like you might be reusing a cell of a different type.
Try making sure which kind of class the cell you are reusing is before trying to access it's properties.
why you are define cell for each row?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:#"Cell%d",indexPath.row];
CellWithTextFieldViewController *cell = (CellWithTextFieldViewController *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell =[[[CellWithTextFieldViewController alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]autorelease];
cell.txtField.tag = indexPath.row;
cell.txtField.text = [SET TEXT FROM ARRAY];
cell.txtField.delegate = self;
}
}
I had also faced this problem in my project.
just try this:
Disabled the scrolling property in Tableview and create a scrollview.Then add your Tableview in your scrollview.
UITableView *table=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, 976, 395) style:UITableViewStylePlain];
[table setSeparatorStyle:UITableViewCellSelectionStyleNone];
UIScroll *scroll=[[UIScrollView alloc]initWithFrame:CGRectMake(24, 168, 978, 395)];
[table setScrollEnabled:NO];
[scroll addSubview:table];
[self.view addSubview:scroll];
[scroll sendSubviewToBack:table];
Try this
static NSString *cellIdentifier= #"Cell";
CellWithTextFieldViewController *cell = (CellWithTextFieldViewController *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[CellWithTextFieldViewController alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.txtField.tag =indexPath.row+1;
cell.txtField.delegate = self;
cell.txtField.text = #"kjfkd";
return cell;
While scrolling tableView your cell is getting reused. Thats why textField is disappearing. Try to use different cell id for different custom cells. Dont forget to give the same id in nib file for cell identifier
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *customCellOne = #"customCell1";
static NSString *customCellTwo = #"customCell2";
UITableViewCell *cell =nil;
if (0 == indexPath.row)
{
cell = (CustomCellOne *)[tableView dequeueReusableCellWithIdentifier:customCellOne];
if (nil == cell)
{
// Create custom cell one and do what ever you want
}
}
else if (1 == indexPath.row)
{
cell=(CustomCellTwo *)[tableView dequeueReusableCellWithIdentifier:customCellTwo];
if (nil == cell)
{
// Create custom cell two and do what ever you want
}
}
return cell;
}
I don't know,may be when reused the cell,the textfield delegate was released.
may be you can set the
textfield.deleagate = self;
in CellWithTextFieldViewController.m

protect cell.textLabel.text on scroll event of UITableView

How can I protect Text of UITableViewCell, which gets changed on scrolling of the UITableView.
static NSString *CellIdentifier = #"Cell";
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(indexPath.row != [destinationList count])
{
if (cell == nil)
{
cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.customLable.text = #"MyCustomLabel";
else
{
if (cell == nil)
{
cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = #"Static Text to be set";
[cell.customLable removeFromSuperview];
}
Problem: Every time I scroll the UITableView, #"Static Text to be set" gets overwritten on #"MyCustomLabel".
How can I prevent this? I want all the cells of UITableView to retains their TextLabels through Table's LifeTime.
Two possible answers:
Generally it doesn't matter. Reusing the cell is how it's supposed to work and you should fully "reset" each cell each time. You shouldn't be storing state in the cell anyway
Create a new reuseIdentifier, one for the custom label and another for the static text
They will all retain their UILabels property, because cells get reused which is why you use:
[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
However UITableView uses lazy loading meaning it ONLY keeps in memory visible UITableViewCells, therefore when you scroll the now invisible cells get reused, and now the new visible cells have the same UILabel as the ones that you can no longer see. That means there is really just a handful of UITableViewCells getting reused every time.
This is why in the docs UITableViewCell in the discussion for the instance method prepareforReuse:
If a UITableViewCell object is reusable—that is, it has a reuse identifier—this method is invoked just before the object is returned from the UITableView method dequeueReusableCellWithIdentifier:. For performance reasons, you should only reset attributes of the cell that are not related to content, for example, alpha, editing, and selection state. The table view's delegate in tableView:cellForRowAtIndexPath: should always reset all content when reusing a cell. If the cell object does not have an associated reuse identifier, this method is not called. If you override this method, you must be sure to invoke the superclass implementation.
You should make different identifiers for different cells.
Anyway, to store something in UITableView is a wrong approach, it just to display data, not to store.
Try this
static NSString *CellIdentifier = #"Cell";
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = nil;
cell.customLabel.text = nil;
if(indexPath.row != [destinationList count])
{
cell.customLable.text = #"MyCustomLabel";
}
else
{
cell.textLabel.text = #"Static Text to be set";
}
why do you remove the custom label? Just remove the text of the label.
if(indexPath.row != [destinationList count])
{
/* ... */
cell.textLabel.text = #"";
cell.customLable.text = #"MyCustomLabel";
else
{
/* ... */
cell.textLabel.text = #"Static Text to be set";
cell.customLable.text = #"";
}
and make sure that does labels are transparent.

Passing a value back from a view

Hey everybody, I had a question about editing the value of a table cell and returning the edited value back to the original cell.
I have a UITableView which contains 5 sections of 1 row each. Within cellForRowAtIndexPath I have the following:
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
NSString *fieldTitle;
switch (indexPath.section)
{
case 0:
fieldTitle = #"First Name";
break;
case 1:
fieldTitle = #"Last Name";
break;
case 2:
fieldTitle = #"Company";
break;
case 3:
fieldTitle = #"Email Address";
break;
case 4:
fieldTitle = #"Password";
break;
}
cell.textLabel.text = fieldTitle;
When a row is clicked, didSelectRowAtIndexPath is fired as follows:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
AddField *addField = [[AddField alloc] initWithStyle:UITableViewStyleGrouped];
addField.field = cell.textLabel.text;
addField.title = cell.textLabel.text;
addField.value = cell.detailTextLabel.text;
[self.navigationController pushViewController:addField animated:YES];
}
This sets up another UITableView that contains one section and one row. This table utilizes a custom cell I wrote that contains a text field the user can edit. There is a done button the user can click to go back to the previous view.
My question is this: How do I get the value the user entered in the AddField view to appear in the detailText label on the selected table cell in the previous view? This functionality would be very similar to adding the title of a new event in the iPhone's native calendar application.
Thanks for any help I can get, and let me know if you need more information.
Have you tried calling reloadData on the original tableView? Of course you would need chnage your code so that fieldTitle to get its values from an array of NSStrings......and set the tableview datasource to that array....
You would use delegates for this. Declare a protocol which is your cell delegate as follows (from memory so apologies if it doesn't compile):
#protocol CellDelegate
#required
- (void) fieldValueChanged:(AddField *)field;
#end
Add an "id delegate" to your custom cell class, and add a property (nonatomic, assign) and synthesise it.
Then when you setup your cell in didSelectRowAtIndexPath you do the following:
[cell setDelegate:self];
Now in you view controller you simply implement that protocol (CellDelegate), implement the method fieldValueChanged.... this will get called by the cell.
Now in the cell when the value is changed simply call [delegate fieldValueChanged:addfield];
NOTE: really you should be setting up the AddField WITHIN the custom cell class you write...

reloadData not working - suspect cell's reuseIdentifier

I have a view controller that gets presented modally and changes some data that effects the data in a uitableview in the modal's parent view controller (a table view).
I call the tableview's reloadData method when the parent view reappears. I have confirmed that this code gets hit with a break point. My trouble is, reloadData isn't working. Here's the kicker - if I don't use reuseIdentifiers in the - (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath method, the data reloads correctly. It looks like the reuseIdentifier is to blame.
I really want to continue to use the reuseIdentifier for my cells - how do I make this work?
Got it figured, and you're right, it's not the reuseIdentifer.
I was nesting the content assignment right below the cell allocation like so:
// Dequeue or create a cell of the appropriate type.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = [targetRow.children count] ? UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone;
// Configure the cell.
cell.textLabel.text = [NSString stringWithFormat: targetRow.title];
}
Since the dequeued cell was found, the content wouldn't get updated.
I changed it to this and all works great now...
// Dequeue or create a cell of the appropriate type.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.accessoryType = [targetRow.children count] ? UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone;
// Configure the cell.
cell.textLabel.text = [NSString stringWithFormat: targetRow.title];
The problem is unlikely to do with the reuse identifier. Please post your code for your tableView:cellForRowAtIndexPath: method.