Adding UILabel to detailTextLabel of a UITableViewCell - iphone

In my app that consist of a grouped tableView,the detailTextLabel displays dates entered through a datepicker. and i use this dates to retrieve data from webservice. What i want to do is to add UILabels to detailTextLabel of the cells so that i can access the label's text property to get the dates and send request to webservice. i tried adding label to the detailTextLabel like this
cell.detailTextLabel = self.fromDate //fromDate is an instance of UILabel;
But its showing me error...
How can i fix this

You should set cell.detailTextLabel.text to modify it's contents, and possibly change the layout of cell.detailTextLabel, but not try to assign another UILabel to it.
You might want to add your own label as a subview, then do something along the lines of
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(160.0f,23.0f,150.0f,20.0f)];
lbl.textAlignment = UITextAlignmentRight;
lbl.font = [UIFont fontWithName:#"Arial" size:14.0f];
lbl.textColor = [UIColor lightGrayColor];
lbl.text = [#"Ref.No. " stringByAppendingString:refno];
[cell addSubview:lbl];

Related

how to add UILabel within a UILabel?

This question looks very usual.. but I had a problem yesterday, I have added a label in xib and created outlet for it, I want to have multiple lines, buttons inside that label, So I have decided to add new sub labels inside it...
UILabel *label;
label = [[UILabel alloc] initWithFrame:initial_rect];
label.font = [UIFont systemFontOfSize:[UIFont smallSystemFontSize]];
label.text = sel_obj->start_time;
label.backgroundColor = [UIColor whiteColor];
[xib_label addSubview:label]
When I tried like that, it was not working, then I have added the same label in self.view it works fine.. So what do I need to do when I want to add a label within a label which was added using xib. Am I missing anything here.. thanks...
I came across this situation long ago.
Just FYI :
I think the problem is the initial_rect, which is in the view's frame but out of the xib_label's frame.
The frame of label is relative to xib_label, not self.view.
You can try this :
UILabel *label;
CGRect rect = (CGRect){0, 0, 10, 10};
label = [[UILabel alloc] initWithFrame:rect];
label.font = [UIFont systemFontOfSize:[UIFont smallSystemFontSize]];
label.text = sel_obj->start_time;
label.backgroundColor = [UIColor redColor];
[xib_label addSubview:label];
Change the backgroundColor to red to see the label clearly.
And if you want to show multiple lines in a label, you can :
label.numberOfLines = 0;.
you can also try after the above comments to bring subview to front [xib_lable bringSubViewToFront:label];

iphone : Label values over write.

I am inserting label in UIButton using addSubView
Here the label's value will be change every time.
UILabel *backTopLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, 448, 66)];
backTopLabel.backgroundColor = [UIColor clearColor];
backTopLabel.textColor = [UIColor whiteColor];
backTopLabel.font = [UIFont fontWithName:#"Georgia" size:56];
backTopLabel.textAlignment = UITextAlignmentCenter;
backTopLabel.text = [[selectedUsers objectAtIndex:userIndex] valueForKey:#"FirstName"]; // UserIndex will be change and new data will be load in the label
[btnBackLeftCard addSubview:backTopLabel]; // btnBackLeftCard is the UIButton
[backTopLabel release];
The Problem is : It successfully changes the label value but it is over writing it.
I am getting the new label value over the old label value.
How can I solve this ?
Every time you call [[UILabel alloc] initWithFrame:CGRectMake(0, 30, 448, 66)] you are creating new memory, not accessing the existing memory.
So, you need to have something like
if(!backTopLabel)
backTopLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, 448, 66)];
so that you only create the memory if it doesn't exist.
However since your label isn't a property, you're essentially losing access to it anyway.
So you either need to add the label as a property of the class you're in OR tag the view so you can find it again & remove it.
Tag it & find again like this:
for(UIView* labelView in btnBackLeftCard.subviews)
{
if(labelView.tag = 100)
[labelView removeFromSuperView];
}
UILabel *backTopLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, 448, 66)];
backTopLabel.backgroundColor = [UIColor clearColor];
backTopLabel.textColor = [UIColor whiteColor];
backTopLabel.font = [UIFont fontWithName:#"Georgia" size:56];
backTopLabel.textAlignment = UITextAlignmentCenter;
//here is where you tag the view, so you can find it again
backTopLabel.view.tag = 100;
backTopLabel.text = [[selectedUsers objectAtIndex:userIndex] valueForKey:#"FirstName"];
[btnBackLeftCard addSubview:backTopLabel]; // btnBackLeftCard is the UIButton
[backTopLabel release];
Assuming you have only one subview to be inserted in UIButton you can use
for(UILabel *lblViews in [btn subviews]) //Remove all subviews which are labels under button first if any
{
if(lblViews.tag == sometagno) //Compare tag number and remove only if that label found.
[lblViews removeFromSuperview];
}
backTopLabel.tag = sometagno; //Assign some tag number while adding.
[btnBackLeftCard addSubview:backTopLabel]; //Than Add your subview
This basically happens when a view gets added as subview on another view.. In your case it's been added multiple times.
Create one UILabel and then simple change it's text property.
In your case you're creating it multiple times and adding on top of the previous one.
Or if you cannot reuse previously created one, try removing it from the superview then add the latest one..
I think you are adding the subview (UILabel) again.
YOu should only change the text of UILabel
I am working in COCOS2D. If you have declared labelname globally then use
[labelName removeFromParentAndCleanup:YES]

display label with UITableViewCellAccessoryDisclosureIndicator

I hope display label as below
So I used the codes below to display label with UITableViewCellAccessoryDisclosureIndicator
cell=[[[UITableViewCell alloc] initWithFrame:CGRectZero// cellframe //SimpleTableIdentifier1
reuseIdentifier:SimpleTableIdentifier1] autorelease];
cell.accessoryType= UITableViewCellAccessoryDisclosureIndicator;//
UILabel *labelCell =[ [[UILabel alloc]init] autorelease];
[labelCell setTag:11101];
labelCell.frame = CGRectMake(180.9f,15.0f,60.0f,23.0f) ;
[labelCell setBackgroundColor:[UIColor clearColor]];
labelCell.textAlignment=UITextAlignmentRight;
[labelCell setFont:[UIFont systemFontOfSize:13.0]];
[labelCell setTextColor:[UIColor blackColor]];
[cell.contentView addSubview:labelCell];
UITableViewCellAccessoryDisclosureIndicator will display correctly
but the labelCell does not display, except I tap the row of the UITasbleViewCell
Welcome any comment
change the frame property of your UILabel,
try with changing value for x and y.
labelCell.frame = CGRectMake(180.9f,15.0f,60.0f,23.0f) ;
You could also consider to implement the accessory using accessoryView.
Implement a Custom Accessory View For UITableView in iPhone
Somehow I think doing this,
[cell.contentView bringSubviewToFront:labelCell];
will help but since there are other subviews in the table cell you will have to deal with their layout as well. Rather I suggest you create a new custom UITableViewCell subclass.

How to insert more than one line of data in a single cell of UITableView?

Can I know how can we insert more than one line of data into a single cell of a UITableView,
Just Like, I want the output in a single cell to be like :
KPK
Developer
Infosystems
INDIA
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 80)];
myLabel.text = #"KPK";
myLabel.textAlignment = UITextAlignmentCenter;
myLabel.textColor = [UIColor yellowColor];
[cell addSubview:myLabel];
UILabel *myLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 201, 200, 80)];
myLabel1.text = #"Developer";
myLabel1.textAlignment = UITextAlignmentCenter;
myLabel1.textColor = [UIColor yellowColor];
[cell addSubview:myLabel1];
Use \n as you are using in your string.
Set numberOfLines to 4 to allow for any number of lines.
label.numberOfLines = 4;
Update the label frame to match the size of the text using sizeWithFont:. If you don't do this your text will be vertically centered or cut off.
UILabel *label; // set frame to largest size you want
...
CGSize labelSize = [label.text sizeWithFont:label.font
constrainedToSize:label.frame.size
lineBreakMode:label.lineBreakMode];
label.frame = CGRectMake(
label.origin.x, label.orgin.y,
label.size.width, labelSize.height);
label.text=[NSString stringWithFormat:#"KPK\nDeveloper\nInfosystems\nIndia"];
Now you just need to add the label in your cell.
[cell addSubView:label];
you can place \n in text.Not sure if that works else you can make custom cell for multiple line records.
set tablerow height and Use label in your tableview
Create a custom cell, add 4 labels in it, to display all of your required info. Checkout the following tutorial to see how you can make custom cell.
http://adeem.me/blog/2009/05/30/iphone-sdk-tutorial-part-6-creating-custom-uitableviewcell-using-interface-builder-uitableview/
Use a uilabel and add that one as the subview if your tableview cell.you can specify the number of lines of uilabel
label.numberOfLines = 4;
I think that the best way is creating a custom UITableViewCell implementation, or programmatically adding UILabels to the cell's contentView.
I.e.:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 200, 30)];
label.text = #"MyText";
label.textColor = [UIColor blackColor];
[cell.contentView addSubview:label];
[label release]; // Never forget this!
When adding the additional rows you should update the first line definition to correctly offset the new rows. Eg. the second label should have something like this:
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(5, 35, 200, 30)];
When you are done, you should implement the tableView:heightForRowAtIndexPath: method in the table's delegate, because you have to define the correct height for the new cell. In your example, for 4 labels you should implement something like this:
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath)indexPath {
if (is_one_of_my_modified_cells) {
return 35*4 + 5;
}
return 44; // default height
}
Try it and let me know.

UITableViewCell text alignment and overlapped by subview

I am working on a todo list application. It's however i cant seems to align my checkbox image properly with the cell.textLabel.text.
My problem for the code below is the words "test test" got cover by my checkbox image, which only left with "st test"
My code for the checkbox button image like
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(5.0, 7.0, 25.0, 25.0)];
UIImage *uncheckedButtonImage = [UIImage imageNamed:#"unchecked_checkbox.png"];
[button setImage:uncheckedButtonImage forState:UIControlStateNormal];
[cell.contentView addSubview:button];
and my cell textlabel is just
cell.textLabel.text = #"test test";
Is there anyway the align the textLabel text to move a bit to the right?
I have suggested to Add UILabel because in the future if you needed to change the view cell it will be more Dynamic and easier to play around anything new to the Cell.
take a look to the following code snippet:
CGRect DeviceNameFrame = CGRectMake(101, 32, 522, 21);
UILabel *lblDeviceName = [[UILabel alloc] initWithFrame:DeviceNameFrame];
lblDeviceName.backgroundColor = [UIColor clearColor];
lblDeviceName.font = [UIFont fontWithName:#"Helvetica-Bold" size:17];
lblDeviceName.text = #"test test";
lblDeviceName.lineBreakMode = UILineBreakModeMiddleTruncation;
[cell.contentView addSubview:lblDeviceName];
[lblDeviceName release];
I hope this will help you.
The simplest solution would be to look at the "Indentation" properties of UITableViewCell. See the documentation for more info. Subclassing the cell and adding your own UILabel would give you the most control though.