UILabel (or UITextField) with Title Label - iphone

So, I've tried looking all around for information on how to recreate a UILabel (ideally, or a UITextField) with a title (for lack of better work).
Since it's quite hard to explain (half the reason I couldn't find what I was looking for) I've attached an image from the Facebook App.
How would I go about creating labels like this? Either single, or multiple?
Sorry for the simple question, still new to iPhone development.

If you're doing it in a UITableView like the one shown in your capture, you can use the UITableViewCell textLabel property to display a standard left aligned label. Then you only add the UITextField to the cell.
I've done exactly this in an app, it looks good and is pretty simple.
UPDATE:
Something like this (untested, may have some typos):
UITextField *textField = nil;
UITableViewCell *cell = [tableView dequeueCellWithReuseIdentifier: kIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault];
textField = [[UITextField alloc] init];
textField.tag = 12345;
[cell addSubview: textField];
textField.frame = CGRectMake(50, 5, 70, 25);
}
else
{
textField = [cell viewWithTag: 12345];
}
UPDATE2: UITextField wasn't added to UITableCellView.

Related

iPhone reusable cell does not show the changed data while scrolling

I have a table view. I have created a method that takes all the data it needs with a cell and returns the updated cell. In that method I have:
my cell for row goes like this
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
cell = myCellcreationMethod : cell : reuseIdentifier: other arguments;
MycellCreation method goes like this:
MycellCreation with args some strings , the cell and the reuseIdentifier.
if ( cell == nil ) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
//Here code that creates a custom image view something like:
thumbnailView = [[UIImageView alloc] initWithFrame:CGRectMake(9, 9, 70, 70)];
thumbnailView = [imUtil getImageViewWithRoundedCorners:thumbnailView andRadius:10.0];
thumbnailView.contentMode = UIViewContentModeScaleAspectFit;
[cell.contentView addSubview:thumbnailView];
//same here for another image view,
//same again
//and there same for a UILabel
}
//Here I set in each case the data for each imageView from above and the UIlabel text like
UIImage *thumbnailImage = [imUtil getThumbnailImageWithFile:thumbnail];
thumbnailView.image = thumbnailImage;
//the others go here
textLabel.text = #"something";
return cell;
All cells have the same identifier since they all are of the same type. But when I scroll down, instead of showing the data that corresponds to the next object in my list (new thumbnail new image, new text in uilabel) it replicates the first ones. I guess it loads the reused cell but does not put the new data in.
Any suggestions please?
I found the answer. I shouldn't just add the views with a method in my tableview class. I created a NEW subclass of UITableView, initialized it with my custom views and created a method in there that sets the new data. then used them two and it works like a charm!

UITableViewCell edit mode like contacts app

I'm currently working on an app where a user has to edit his/her profile. I would like to use the edit mode like in the contacts app. (Labels become Textfields)
Is there an easy way to achieve this? Or do I have to replace the cell.detailTextLabel.text with a uitextfield by myself?
Example contacts app editing view:
http://content.screencast.com/users/EricMulder/folders/Jing/media/0839c0c9-43df-4283-bcf1-65f49b43696f/00000047.png
For anyone interested, here is some sample code to add an UITextField to your cell:
cell.textLabel.text = NSLocalizedString(#"nameLabel", #"");
//add textField
if(![cell viewWithTag:101]) {
UITextField *aliasField = [[UITextField alloc] initWithFrame:CGRectMake(93, 13, 225, 44)];
aliasField.text = #"Eric Mulder";
aliasField.font = [UIFont boldSystemFontOfSize:15.0f];
aliasField.tag = 101;
[cell addSubview:aliasField];
}
cell.detailTextLabel.text = nil;
ejazz,
Why do you suppose that they are labels? ; )
You can draw custom cell with UITextField in interface builder. Load it from xib. And change textfields editable property in your implementation of setEditing:animated: method of your view controller.

I have a UISwtch embedded in a UITableViewCell defined in a XIB file. How can I respond to a change to the switch state?

I built the cell with Interface Builder. I load the cells like this:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"MassCircleNGTableCell" owner: self options: nil];
cell = circleNGCell;
self.circleNGCell = nil;
}
UISwitch *s = (UISwitch*)[cell.contentView viewWithTag: 20];
UILabel *label = (UILabel*)[cell.contentView viewWithTag:19];
label.text = #"some useful text";
And that part works, I get my table with custom cells. But, although I can set the initial state, I don't know how to respond to the user flipping the switch! I tried this:
[leftSwitch addTarget:self action:#selector(setCircle) forControlEvents:UIControlEventTouchUpInside];
But the app crashes with "[MassCircleNGViewController setCircle]: unrecognized selector sent to instance 0xdd024b0'"
Any ideas out there?
[Added Later (as requested)]:
// implementation of [MassCircleNGViewController setCircle] really just a stub here
- (IBAction) setCircle: (id) sender {
NSLog(#"setCircle sender == %#", (UISwitch*)sender);
}
The real problem was how I was trying to add the target. Need that colon after the selector name in the argument list!
[leftSwitch addTarget:self action:#selector(setCircle:) forControlEvents:UIControlEventValueChanged];
Okay, I found the problem. Where I had done this:
[leftSwitch addTarget:self action:#selector(setCircle) forControlEvents:UIControlEventTouchUpInside];
I needed to add a COLON after the selector name to get this:
[leftSwitch addTarget:self action:#selector(setCircle:) forControlEvents:UIControlEventTouchUpInside];
I know I saw a comment about doing that somewhere in these fora, but it took a couple of hours to sink in, and I have forgotten where I saw it. Oh, I did find a Very Nice Tutorial on XIB based custom UITableViewCells
here
There may be 1 of these problems:
1/ Do you have a method called setCircle inside your UITableViewCell, MassCircleNGViewController
2/ Double check all the release. Maybe your UITableViewCell is released when the method is invoked and another object in the same memory area now

iPhone: cornerRadius quality gets degraded

I have a UILabel that I create a radius on the layer, using cornerRadius. The ultimate goal is to make the label look like Apple does in the mail app.
It looks great at first, but once you drill down into that row and back a few times, the quality of the rounded edge starts to degrade. You can see in the screen shot, the left side is blocky.
Why would this be happening? It seems to happen after about 2 times of loading that view.
(source: puc.edu)
Here is my cell creation method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
Trip *trip = [fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = trip.title;
cell.detailTextLabel.text = #"Date of trip";
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// Create a nice number, like mail uses
UILabel *count = [[UILabel alloc] initWithFrame:CGRectMake(cell.contentView.frame.size.width - 50, 12, 34, 20)];
[count setText:[NSString stringWithFormat:#"%i",[[trip.rides allObjects] count]]];
[count setFont:[UIFont fontWithName:#"Helvetica-Bold" size:16]];
count.textAlignment = UITextAlignmentCenter;
count.backgroundColor = [UIColor grayColor];
count.textColor = [UIColor whiteColor];
count.layer.cornerRadius = 10;
[cell addSubview:count];
[count release];
return cell;
}
In every call to cellForRowAtIndexPath, you are creating a new count UILabel. Eventually there will be several overlapping views in the same place with the same antialiased curve, so it will look blocky.
Try creating the count UILabel only when a new cell is created, in the if (cell == nil) block. Otherwise, get the count label by tag.
if ( cell == nil ) {
cell = ...
count = ...
...
count.tag = 'coun';
[cell.contentView addSubview:count];
[count release];
} else {
count = (UILabel *)[cell viewWithTag:'coun'];
}
[count setText:[NSString stringWithFormat:#"%i",[[trip.rides allObjects] count]]];
Check to see if the tableview/cell is set to clear its context before drawing. I noticed I had similar issues with text on the cell.
I've seen some strange issues wherein it looks like Core Animation based properties are accumulative even though they shouldn't be. Your problem here might be caused by some kind of accumulative creep from changing the value of the corner radius repeatedly every time the cell is returned.
I would suggest testing if the corner radius already equals 10 before setting it again. (Although I would expect that to show up more with scrolling up and down than in reloading the view.)
Another possible problem would be that some subview is migrating causing a visual artifact.
Edit:
Instead of adding the label to the cell itself. Try adding it as a subview of the cell's content view.

Is it possible to accept a button tag to be in some range iphone sdk

In my application I'm doing dynamic resizing of cells and label in it depending upon the text in it.
I'm adding button to cells in uitableview.
I'm taking the label instance and button instance in a new label and button variable respectively and setting their frames to arrange them properly after resizing.
if(cel==nil)
{
//some code
original_label=[[UILabel alloc]init];
original_label.tag=111;
//SOME MORE CODE GOES HERE
original_button=[[UIButton alloc]init];
original_button.tag=222;
//SOME MORE CODE GOES HERE
}
new_label=(UILabel *) [cell viewWithTag:111]; //This' how I'm taking the label instance on cell and below button instance on cell in new variables
new_button = (UIButton * ) [cell viewWithTag:222];
Earlier I kept the tags of all the buttons on cells same, so it was easier to get button instances on cells properly and were being arranged properly. But now I want to recognize these buttons separately as I'm adding some functionality on button_click. I'm giving the buttons that are added to the cells incremental tags[1,2,3...9 and so on]. Now, how can I take these button tags in some range like[suppose 1-9]?
Can anybody help?
Thanks in advance.
You can keep the button tags the same as you had before.
Instead, in the button_click method, figure out which row the button is in like this:
- (void)button_click:(UIButton *)button
{
UITableViewCell *cell = (UITableViewCell *)[[button superview] superview];
NSIndexPath *indexPath = [tableView indexPathForCell:cell];
//code to handle this indexPath.section and indexPath.row...
}
This assumes you have added the button to cell.contentView which is what the first superview gets. The second superview gets the cell.
The addTarget for the button should look like this (note colon after button_click):
[original_button addTarget:self action:#selector(button_click:) forControlEvents:UIControlEventTouchUpInside];