Working with text fields in a UITableView - iphone

I am making a search form with a number of free text fields that I would like the user to edit in line on a tableview (and not go to a seperate view for each individual item). So far I have it so when you press on the field, a text input dialog appears, here is the relevant code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 130, 25)];
label.textAlignment = UITextAlignmentRight;
label.tag = kLabelTag;
[cell.contentView addSubview:label];
[label release];
UITextField *theTextField = [[UITextField alloc] initWithFrame:CGRectMake(150, 10, 140, 25)];
theTextField.tag = kTextFieldTag;
theTextField.delegate = self;
[cell.contentView addSubview:theTextField];
[theTextField release];
This has the desired effect of allowing a user to press a row and get an edit dialog, but I have a few problems:
1) You can see I have set the text field's delegate to equal self. The class this is run in implements UITextFieldDelegate yet textFieldShouldEndEditing is not being fired.
2)How can i realisticly persist what the user puts in memory. In other words, is it possible to find out which text field the user has selected currently so i can go [myArray getObjectAtIndex:blah] and edit the actual data.
Thanks

1) Are the other delegate methods being called?
2) You can for example set the tag attribute on each text field to identify them, and use that as an index into the array.

Related

How to retrieve the data on selection of a cell from a custom UITableview

Might be a stupid question.
I have a UITableview, with multiple cells. In each cell I am displaying some data. I am not using cell's text property to display data. Instead I have a custom label inside my cell, which is displaying the text.
My question is:
When I click on the cell, I need to retrieve the data from the cell. How can I do this.
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
UILabel *CellTextlabel = [[UILabel alloc] init];
CellTextlabel.tag = 222;
[CellTextlabel setFrame:CGRectMake(40, 5, 200, 20)];
[cell.contentView addSubview:CellTextlabel];
[CellTextlabel release];
}
UILabel *editCellTextlabel = (UILabel *)[cell.contentView viewWithTag:222];
editCellTextlabel.font = [UIFont boldSystemFontOfSize:18];
editCellTextlabel.text = contact.lastName;
In your didSelectRowAtIndexPath method, you may do it as follows:
UITableViewCell *cell = (UITableViewCell *)[self.tableViecellForRowAtIndexPath:indexPath];
UILabel *textLabel = (UILabel *)[cell viewWithTag:222];
Now you can retrieve the data in the cell's UILabel using textLabel.text
You could get access to that label in didSelectRowAtIndexPath: with
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UILabel *myLabel = [cell.contentView viewWithTag:222];
But probably worth asking, why are you adding a sub label instead of using the textLabel property? you can modify its frame, settings etc and then you don't have to worry about tags, since this property is exposed in UITableViewCell by default
In the -tableView:didSelectRowAtIndexPath: method, you can get the data from your tableView's array:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
id objectForCell = [self.myArray objectAtIndex:indexPath.row];
//do what you want with the above data.
}

how to add button cell.contentview not working perfect?

I have created one application in which if i select on particular row in Table View than that selected row will expand(I have implemented row onclick row expand perfectly but i have problem with [cell.contentview addsubview:] please see image i have provided) & there is some button but i can't get perfect view in table view without clicking row i can see other cell's button here is image!
my code for displaying button is here.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
cell = [tableBusiness dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
//cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
//cell.textLabel.borderStyle = UITextBorderStyleNone;
cell.textLabel.text = [NSString stringWithFormat:#"%d",indexPath.row];
UILabel *theText = [[UILabel alloc] initWithFrame:CGRectMake(10.0,10.0, 200, 22.0)];
theText.text = [NSString stringWithFormat:#" Title Text %d",indexPath.row];
[cell.contentView addSubview:theText];
btnMenu = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnMenu.frame = CGRectMake(80, 160, 100, 37);
[btnMenu setTitle:#"Go to Menu" forState:UIControlStateNormal];
[btnMenu addTarget:nil action:#selector(accessoryTypeForRowWithIndexPath:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:btnMenu];
//textView.text = nil;
return cell;
[btnMenu release];
[theText release];
}
& i want to make button events like on clicking particular button open that clicked button product how to do that?
does any have solutions please help me.
i want to create like this !Here is description for the image thread.
here its link for how to expand row.?
If you want to increase the height of cell then u can try with following code
UITableView *movieListTbl;
movieListTbl.rowHeight = "Height of cell";
But remember that you delegate your tableview.

Memory leak found, but I can't find it?

After using Run->Run with Performance tool->Leaks on my application, it finds a leak, but I don't know if it is a glitch by the tool?
I start the application by selecting a button, which opens a tableview (no leaks).
tableview appears and if I don't touch anything (no leaks).
If I select the first cell and cursor appears blinking (3 to 4 seconds later, leak)??
I did find something weird, in my tableview, if I try selecting any other cell, besides the first one, it will not become the first responder (this might not have to do with anything)?
If anyone has come across this or can spot or direct me to where this could be happening please let me know? Thank you.
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
UITextField *FirstField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 130, 25)];
FirstField.delegate = self;
FirstField.tag = indexPath.row; [cell.contentView addSubview:FirstField];
FirstField.returnKeyType = UIReturnKeyNext;
[FirstField release];
return cell;
}
The memory leak is that due to every time you call the function you are allocating a UITextField and adding it to the cell.
Cells are reused and each time the cell is accessed to determine the content a new UITextField is added to the cell and stacked onto of each other
Move the allocation of FirstField to where the cell is allocated so that it can be reused.
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UITextField *FirstField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 130, 25)];
FirstField.tag = 1
[cell.contentView addSubview:FirstField];
[FirstField release];
}
UITextField *field = (UITextField *)[cell.contentView viewWithTag:1];
field.delegate = self;
field.returnKeyType = UIReturnKeyNext;
return cell;
I also see that you are using the field.tag to store the indexPath.row value of the cell, in that cause you might have to search the view hirachy of cell.contentView to find the TextField subview.

Having trouble with customized cell UITableView

I am trying to add a customCell which simply will allow me to left align one string, and right align the other. In a previous question someone had suggested the following, but I am having problems getting it to work with my code. Xcode says: RootviewController may not respond to --contentView, and that call [[self contentView] addSubView:item] or [[self contentView] addSubView:rank] will crash my app at runtime
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
UILabel *rank = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 100, 20];
//Mess around with the rects, I am just merely guessing.
[rank setTag:5];
[[self contentView] addSubView:rank];
[rank release];
UILabel *item = [[UILabel alloc] initWithFrame:CGRectMake(110, 5, 220, 20];
//Important
[item setTextAlignment:UITextAlignmentRight];
[item setTag:6];
[[self contentView] addSubView:item];
[item release];
}
UILabel *rank = (UILabel *)[cell viewWithTag:5];
UILabel *item = (UILabel *)[cell viewWithTag:6];
rank = #"leftside";
item = #"rightside";
}
Thanks for any ideas
You should be looking at
[cell.contentView addSubview:item];
[cell.contentView addSubview:rank];
rank.text = #"leftside";
item.text = #"rightside";
One more thing to note here. If your UITableView is scrollEnabled, you will have problems with cellReusability and your labels will get messed up with subsequent scrolls. I would suggest that you subclass UITableViewCells and add those in the layout, and then use the CustomUITableViewCells.
self here is the controller. Instead of making self the receiver, you should be sending contentView to the cell you just created.

Buggish behavior of a UILabel Text when selecting a Custom built UITableViewCell: the text get mixup or overlaid with a previously loaded label

I have been looking for a solution to this issue for a few days now... and cannot find someone with a similar problem or a solution that would work for me. At this point I am not even sure that I am doing something wrong, as I have read and analyzed many sample code and I am almost 100% sure that I am doing this the way it should...
Anyway here it comes:
I have a UITableView to which I display custom built UITableViewCell, here is where I create them:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CellIdentifier";
NSDictionary *myDictionary = [myArray objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero
reuseIdentifier: CellIdentifier] autorelease];
CGRect textViewRect = CGRectMake(10.0, 0.0, 250, 25);
UILabel *textView = [[UILabel alloc] initWithFrame:textViewRect];
textView.text = [myDictionary objectForKey:#"name"];
textView.font = [UIFont systemFontOfSize:12.0];
textView.textColor = [UIColor blackColor];
textView.highlightedTextColor = [UIColor whiteColor];
textView.textAlignment = UITextAlignmentRight;
[cell.contentView addSubview:textView];
[textView release];
CGRect imageRect = CGRectMake(270.0, 15, 16, 16);
UIImageView *icon = [[UIImageView alloc] initWithFrame:imageRect];
icon.image = [myDictionary objectForKey:#"color-icon"];
icon.highlightedImage = [myDictionary objectForKey:#"gray-icon"];
[cell.contentView addSubview:icon];
[icon release];
}
So as you can see, pretty standard stuff... Then when I click on one of the cell, another view gets loaded instead of the table.
Until now, everything is fine, but then when I come back to that table view and that it has to reload the problem starts...
By the way I have added this to the delegate methods so the cells never stay selected:
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
// [...] a lot more code here
}
When I clicked on my cell in the simulator with the mouse and keep it down the cell stays selected as expected (same way on the device itself) and this is why I get:
[Image 1]
The UILabel Text is totally mixed up ! The screenshot is actually of the last cell of the table and the overlapping text is the one of the first cell. And the behavior on the first is similar, if I maintain the click on it, it will show the same behavior mixing up with the UILabel text of the last cell. I am guessing that is is due to the way the dequeueReusableCellWithIdentifier works (probably a FILA queue).
Of course I tried to work around that and found some really weird stuff.
If I don't click the cells are perfect, no bugs, the correct text is displayed etc:
[Image 2]
Then I tried to mess up a little bit with the parameters of my UILabel. I added this:
textView.backgroundColor = [UIColor clearColor];
And when I do this, as soon as the table reload, then I don't even need to highlight the cell to see the screw up behavior:
[Image 3]
The only way I was about to get rid of the problem to always instantiate a new cell rather than dequeueing one...
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero
reuseIdentifier: CellIdentifier] autorelease];
was replaced by:
UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero
reuseIdentifier: CellIdentifier] autorelease];
But of course this won't work on the long term because of the memory leak...
Also, it is to be noted that I have the exact same behavior for another UITableView in another view somewhere in this App... The other custom cell is more complex, more labels, more images, etc, but all the label exhibit the same behavior. And this more complex Table View is managed by a Navigation Controller, so no custom loading / unloading like the first one...
That's all I have, and I can't find a solution... please help !
Arghh this is really annoying... I am new so I cannot post images... :(
Here is a link where you can see the referenced images: https://skitch.com/aponsin/rne9k/fullscreen.png-100-layer-3-rgb-8
Alex
The problem is that you create new label and add it to cell each time cell is being reused. To fix that you must create your label only when your cell is created:
if (cell == nil){
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero
reuseIdentifier: CellIdentifier] autorelease];
CGRect textViewRect = CGRectMake(10.0, 0.0, 250, 25);
UILabel *textView = [[UILabel alloc] initWithFrame:textViewRect];
textView.tag = kLabelTag;
textView.font = [UIFont systemFontOfSize:12.0];
textView.textColor = [UIColor blackColor];
textView.highlightedTextColor = [UIColor whiteColor];
textView.textAlignment = UITextAlignmentRight;
[cell.contentView addSubview:textView];
[textView release];
CGRect imageRect = CGRectMake(270.0, 15, 16, 16);
UIImageView *icon = [[UIImageView alloc] initWithFrame:imageRect];
icon.image = [myDictionary objectForKey:#"color-icon"];
icon.highlightedImage = [myDictionary objectForKey:#"gray-icon"];
[cell.contentView addSubview:icon];
[icon release];
}
And after that you can get your label from cell using tag property you've assigned:
UILabel *textView = (UILabel*)[cell.contentView viewWithTag:kLabelTag];
textView.text = [myDictionary objectForKey:#"name"];
same logic also applies to setting up your icon imageview if it varies in different cells
Also have a look at components standard UITableViewCell already provides (depending on its cell style) - there's already UIImageView and UILabels there and you can set your custom properties to them and use them without creating extra components