How to add label and textfiled to each row in the UITableview - iphone

How to add label and textfiled to each row in the UITableview. can i add it through the Interface builder or have to do in the code,
Thanks,
Sam.

You can either make a custom table view cell class yourself, or you can use one of the built-in cell styles. Apple's "Settings" panel, for example, uses UITableViewCellStyleValue1.
Then you would set the detailTextLabel to the text, and add a sub view (your text field) to the contentView for your cell.
static NSString *CellID = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellID] autorelease];
}
cell.detailTextLabel.text = #"a title";
[cell.contentView addSubview: xyz]; // xyz = your text field

Related

Custom Cell in storyboard UITableView not being picked up in ios7 with UITableViewCellStyleDefault

I add a UITableView in storyboard and then add a a cell to that with my customisations. The cells were coming back blank and after debugging I found that the Cell being created isn't my custom one (I gave the cell a tag of 10 to test this).
My code is below:
UITableViewCell *cell = nil;
static NSString *cellIdentifier = #"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier ];
}
Food *curFood = [self.mealFoods objectAtIndex:indexPath.row];
/// Get the labels of cell
UILabel *descLabel = (UILabel *)[cell.contentView viewWithTag:101];
UILabel *quantityLabel = (UILabel *)[cell.contentView viewWithTag:102];
descLabel.text = curFood.Description;
quantityLabel.text = [NSString stringWithFormat:#"%f", curFood.Quantity];
return cell;
In my last project in ios6 the above worked fine, with the exception that the labels were not in contentview. Either I did something last time that I forgot or now in ios7 I need to do something to make it use my prototype/custom cell?
Micantox was correct in his comment. I had missed out setting the Cell identifier in the custom cell in storyboard:
In my case my identifier was simply 'Cell'.

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.

Accessing indexPath from custom UITableViewCell

I'm trying to create a UITableView to allow the user to enter data, similar to a lot of the settings UITableViews, with some textfields and callouts to other tableviews for checkboxes. For such a common feature in apps, this does not seem to be very straight forward.
I'm having trouble accessing the indexPath.row from my custom UITableViewCell. Here is where I allocate my custom UITableViewCell.
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[TextFieldCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
In my TextField class's #implementation, in - (id)initWithStyle: I'm trying to access the indexPath with:
NSIndexPath *indexPath = [(UITableView *)self.superview indexPathForCell:self];
... in order to set the textfield's tag like:
textRow = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 200, 21)];
textRow.tag = [indexPath row];
Could anyone please shed some light on my problem or point me in the direction of creating basic setting-style TableViews programatically.
Thank you
I think the problem might be that at the time of initialisation of your cell it hasn't been added to the UITableView - hence self.superview is returning nil.
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[TextFieldCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
[cell setTag:indexPath.row];
You will need to add a setTag: method to your TextFieldCellClass. I presume that code was inside cellForIndexPath or whatever, so indexPath will be passed to that method.
The set tag method should look something like this:
-(void)setTag:(int)tag {
textRow.tag = tag;
}

How can i make my UITableViewCell contain two labels?

I want my UITableViewCell to look like the image below where there seems to be two labels. Is this possible without subclassing UITableViewCell?
alt text http://img31.imageshack.us/img31/2764/photoobp.jpg
There are different styles of UITableVieWCell. See here:
https://developer.apple.com/documentation/uikit/uitableviewcell/cellstyle
I think you want to use UITableViewCellStyleValue1.
You can initialise your UITableViewCell with the relevant style:
https://developer.apple.com/documentation/uikit/uitableviewcell/1623276-init
When you use a style that has two labels, you can use the textLabel and detailTextLabel properties to set them, respectively.
You do not need to subclass a UITableViewCell in order to add content to it. Here could be a sample cell generation method with an extra label:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = #"Identifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
UILabel *secondLabel = [[UILabel alloc] initWithFrame:cell.textLabel.frame];
secondLabel.textAlignment = UITextAlignmentRight;
secondLabel.tag = 12345;
[cell.contentView addSubview:secondLabel];
}
UILabel *second = [cell viewWithTag:12345];
second.text = #"Second!";
return cell;
}
Let me know if you have any questions. I can clarify some things if needed.
Not sure where u think you see 2 labels...you can set the UILabels number of lines property if you want more lines UILabel ref....Also there is a UITableViewCell type UITableViewCellStyleSubtitle
which contains a detailTextLabel on top of the regular text labels in UITableCell, so you already have a built in cell with 2 text fields, here is a ref ref to UITableViewCell
Its not 2 labels but 2 buttons, you need to add 2 buttons in contentView view of the cell. Or you can create a footer or header View and add these 2 buttons.

no value entered in TableView row

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];).