How to make tableviewcell text editable on touch in iPhone SDK? - iphone

In my iPhone app, I have a table view.
I want that when the user selects (or basically touches) on the cell of the tableview then he/she should be able to edit the cell contents.
How can I do that?

#PARTH in order to make your UITableView cells editable i suggest u to give textfields to your cell....
eg:- In
-(UITableViewCell *)reuseTableViewCellWithIdentifier:(NSString *)identifier withIndexPath:(NSIndexPath *)indexPath {
CGRect cellRectangle = CGRectMake (0, 10, 300, 70);
CGRect Field1Frame = CGRectMake (10, 10, 290, 70);
UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:cellRectangle reuseIdentifier:identifier] autorelease];
UITextField *textField;
//Initialize Label with tag 1.
textField = [[UITextField alloc] initWithFrame:Field1Frame];
textField.tag = 1;
[cell.contentView addSubview:textField];
[textField release];
return cell;
}
and in your cellForRowAtIndexPath method // Configure the cell.
UITextField *txtTemp = (UITextField *)[cell.contentView viewWithTag:1];
use this where u want in your tableview accordingly otherwise hide it.....Hope it will help u!!

you need to put text field inside the table cell use this link.

Related

Adding UILabel to UIImageView in CellForRow

I have a customCell in StoryBoard "CategoryCell", I have UIImageView with the cell which is also part of the cell also tied up in StoryBoard. The UIImageView is filled with a plain yellow color. I intend to add a label to this plain yellow image, the label varies depending on the cell.
The code below works fine initially, but when I scroll the the tableView, I see label in the image getting messed up, it's like its trying to write new text in top of the text. I figured it's hitting cellForRow again and it's adding new label, How can I make it not create new label on top of old one?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if([tableView isEqual:wordsTableView])
{
CategoryCell *cell = [CategoryTableView dequeueReusableCellWithIdentifier:#"CategoryCellIdentifier" forIndexPath:indexPath];
if(!cell)
cell = [[CategoryCell alloc] initWithStyle:UITableViewStylePlain reuseIdentifier:#"CategoryCellIdentifier"];
NSString *text = [[theCategories objectAtIndex:indexPath.row] uppercaseString];
//Add label now
catLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 40, 20)];
catLabel.text = text;
[cell.codeImageView addSubview:catLabel];
return cell;
}
You could give the label a tag and use that to check to see if it exists before creating it again :
UILabel *catLabel = [cell.codeImageView viewWithTag:100];
if (nil == catLabel) {
catLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 40, 20)];
catLabel.tag = 100;
[cell.codeImageView addSubview:catLabel];
}
catLabel.text = text;
Though if it gets any more complicated I might look at subclassing UITableViewCell and using a xib to instantiate the label.

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 can I resize the size of the cell.textLabel?

I want to resize the default textLabel of a UITableViewCell because I display a image at the right of the rows. I Tryed with this code but it doesn't works, and I don't understand why.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//...
cell.textLabel.frame = CGRectMake(5, 5, 100, 50);
//...
}
u should used custom label
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
UILabel *Lbl = [[UILabel alloc]initWithFrame:CGRectMake(75.0f, 4.5f, 360.0f, 20.0f)];
[cell.contentView addSubview:Lbl];
[Lbl release];
}
I think that is impossible.
Make custom cell.
UILabel myTextLabel;
//Set Frame and do something.
[cell.contentView addSubview:myTextLabel];
textLabel is readonly property so we can't set frame..
#property(nonatomic,readonly,retain) UILabel *textLabel __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0); // default is nil. label will be created if necessary.
#property(nonatomic,readonly,retain) UILabel *detailTextLabel __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0); // default is nil. label will be created if necessary (and the current style supports a detail label).
use custom cell ...
You can not change a cell's textLabel's frame except or you go with custom cell and use UILabel, UIImageView as a subview of the cell.

Working with text fields in a UITableView

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.

iphone sdk - TableView Cell with custom TextView - Get New Data

I have a tableview cell with a custom textview in it, I am now left wondering how do I possibly access the text in the textbox after text has been edited/added.
I would normally know how this is done when I draw the textfield through IB, but my textviewcell is dynamically drawn.
(i.e. I would like to capture the data that is updated in detailLabel.text)
Here is the related code to adapt to your answer.
Thanks again!
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
//Big Text Box
UITextView *detailLabel = [[UITextView alloc] initWithFrame:CGRectMake(30, 80, 500, 150)];
detailLabel.tag = 20;
[cell.contentView addSubview:detailLabel];
detailLabel.layer.borderWidth = 1;
detailLabel.layer.borderColor = [[UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:0.9] CGColor];
detailLabel.layer.cornerRadius = 10;
detailLabel.font = [UIFont fontWithName:#"Helvetica" size:17];
[detailLabel release];
}
UITextView * detailLabel = (UITextView *) [cell.contentView viewWithTag:20];
switch (indexPath.row) {
case 0:
detailLabel.text = #"no";
break;
default:
detailLabel.text = [NSString stringWithFormat:#"%d", indexPath.row];
detailLabel.hidden = NO;
}
You'll want to listen for messages sent by the UITextView. So look to implement the UITextViewDelegate protocol and register the implementing class with the detailLabel using its delegate property.
When your delegate is notified of a change in the UITextView you'll have to identify the cell which currently owns the UITextView. In doing so you must remember that cells can be reused.
I would start by looking at the following method in the UITableView class:
- (NSArray *)visibleCells
We know that if the user has made a change to the UITextView's contents it must currently be on screen and therefore be present in the pre-mentioned array. To find it we use the pointer to the UITextView which changed (it's a parameter in the textViewDidChange protocol method). So simply iterate over the visibleCells array and retrieve the UITextView and compare it against the UITextView which changed.
- (void)textViewDidChange:(UITextView *)textView {
....
cellsLabel = (UITextView *) [cell.contentView viewWithTag:20];
if (cellsLabel == textView)
...
You'll now have a handle to a cell in the UITableView. To find the index use the following UITableView method:
- (NSIndexPath *)indexPathForCell:(UITableViewCell *)cell