how to set height of uilabel equal to uitableviewcell - iphone

I have a grouped style Tableview which have uilabels in Tableviewcell. Now i want to set height of uilabels equal to height of cell how can i do ths???
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// here i want to make height of label equal to height of cell
UILabel *category = [[UILabel alloc] initWithFrame:CGRectMake(95,1,140,25)];
category.font = [UIFont fontWithName:#"Arial" size:14.0f] ;
category.textAlignment = NSTextAlignmentRight;
[category setBackgroundColor:[UIColor clearColor]];
[cell addSubview:category];
}

In the cellForRowAtIndexPath add; this will return current height set for your tableview:
CGFloat currentCellHeight = [self tableView:tableView heightForRowAtIndexPath:indexPath];
UILabel myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, customWidth, currentCellHeight)];

Get default cell height
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
UILabel *category = [[UILabel alloc]
initWithFrame:CGRectMake(0,0,cell.frame.size.width,cell.frame.size.width)];

If you want your cell's height, just do the following:
category.frame = CGRectMake(0,0,width,cell.bounds.size.height);

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Look at this
UILabel *category = [[UILabel alloc]
initWithFrame:CGRectMake(0,0,cell.bounds.size.width,cell.bounds.size.height)];
category.font = [UIFont fontWithName:#"Arial" size:14.0f] ;
category.textAlignment = NSTextAlignmentRight;
[category setBackgroundColor:[UIColor clearColor]];
[cell addSubview:category];
}
use this code...

YOu can use following to find height in
- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGSize textSize = [[NSString stringWithFormat:#"%# %#",label.text] sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:CGSizeMake(200, 1000.0f) lineBreakMode:NSLineBreakByCharWrapping];
return textSize.height; //height for cell
}

I think there's two things you can do; first try reading this link from apple and review your approach. If you only need a label, why not use the cells textLabel and customize its appearance?
If you really want to use the label approach then consider adding it to the contentView instead of the cell. You can also try to get the bounds of this property, instead of the c
UILabel *category = [[UILabel alloc] initWithFrame:CGRectMake(0,0,width,cell.contentView.bounds.size.height)];
[cell.contentView addSubview:category];
Hope this helps.

// try this code
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(95,1,140,cell.frame.size.height)];

just set the cell bounds to label's frame,like below.
UILabel *myLabel = [[UILabel alloc] initWithFrame:cell.bounds];

Related

FPPopover cell.detailTextLabel.text

For those that are familiar with FPPopOver: https://github.com/50pixels/FPPopover
Can the cells display subtitle? I had to attach my UITableViewController to a nib.
In the Storyboard I have the cell set up to show subtitle, however, when the popOver appears on the iPhone, only cell.textLabel.text is visible - cell.detailTextLabel.text is not appearing.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell==nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSString *entityName= [[managedObject entity]name];
cell.textLabel.text = [NSString stringWithFormat:#"%# %i", entityName, [indexPath row]];
cell.detailTextLabel.text = #"Subtitle";
}
Here is the image:
Update
After using Labels to display the subtitle, after scrolling the cell back into the view, the following happens:
if it's not working at all then alternative of that is you can use lable and set frame of lable as detail text has and add that lable as cell.contentview addsubview so this way you'll get your detail text on your tableview cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = (UITableViewCell*)[tblUser dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
}
NSMutableDictionary *objUser = [arrUser objectAtIndex:indexPath.row];
strName = [objUser objectForKey:#"Name"];
strDate = [objUser objectForKey:#"Date"];
UILabel *lblName = [[UILabel alloc] initWithFrame:CGRectMake(85, 10, 215, 20)];
lblName.text = strName;
lblName.textColor = [UIColor whiteColor];
[lblName setBackgroundColor:[UIColor clearColor]];
[cell.contentView addSubview:lblName];
UILabel *lblDate = [[UILabel alloc] initWithFrame:CGRectMake(85, 34, 215, 20)];
lblDate.text = strDate;
lblDate.textColor = [UIColor whiteColor];
[lblDate setBackgroundColor:[UIColor clearColor]];
[cell.contentView addSubview:lblDate];
return cell;
}

Array displaying repeated object in table view cell

I am having a NSArray of size 11 as it formed below.
labels = [NSMutableArray arrayWithObjects:#"DIN #",#"Brand Name",#"Full Name",
#"Strength",
#"Medication Type",
#"Presciption ID",
#"Next Dosage",
#"Required Dosage",
#"ada",
#"dasdada",
#"dasdasad",
nil];
but when i am displaying this array in tableview cell Using this code.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dueueReusableCellWithIdentifier:CellIdentifier];
UILabel* nameLabel = nil;
if(cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];nameLabel = [[UILabel alloc] initWithFrame:CGRectMake( 7.0, 10.0, 160.0, 44.0 )];
nameLabel.text =[labels objectAtIndex:indexPath.row];
nameLabel.lineBreakMode = UILineBreakModeWordWrap;
nameLabel.numberOfLines =0;
[nameLabel sizeToFit];
CGSize expectedlabelsize = [nameLabel.text sizeWithFont:nameLabel.font constrainedToSize:nameLabel.frame.size lineBreakMode:UILineBreakModeWordWrap];
CGRect newFrame =nameLabel.frame;
newFrame.size.height =expectedlabelsize.height;
[cell.contentView addSubview: nameLabel];
return cell;}
O/P is as below .
DIN
Brand Name
Full Name
Strength
Medication Type
Presciption ID
Next Dosage
Required Dosage
DIN
Brand Name
Full Name
see after required Dosage again DIN,Brand Name,Full Name showing ,which are already shown
Use viewWithTag method and i have used it following method. Try like this. I think it will be helpful to you.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"myCell";
UILabel* nameLabel = nil;
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
else
{
UILabel *titleLbl = (UILabel *)[cell viewWithTag:1];
[titleLbl removeFromSuperview];
}
nameLabel = [[UILabel alloc] initWithFrame:CGRectMake( 7.0, 10.0, 160.0, 44.0 )];
nameLabel.text = [labels objectAtIndex:indexPath.row];
nameLabel.lineBreakMode = UILineBreakModeWordWrap;
nameLabel.tag =1;
nameLabel.numberOfLines =0;
[nameLabel sizeToFit];
CGSize expectedlabelsize = [nameLabel.text sizeWithFont:nameLabel.font constrainedToSize:nameLabel.frame.size lineBreakMode:UILineBreakModeWordWrap];
CGRect newFrame =nameLabel.frame;
newFrame.size.height =expectedlabelsize.height;
[cell.contentView addSubview: nameLabel];
return cell;
}

How to clear UILabels inside UITableViewCells?

I put multiple UILabels inside every cell in a UITableView instead of a single cell.textLabel.text. I then use reloaddata to put new uilabels. How do i get rid of the old labels ?
edit: If i put 5 labels in a cell then reload the cell using only 2 labels, there are 3 more labels left over from the last time i called cellForRowAtIndexPath.
If i use viewWithTag like Goldeen said, i can reuse old labels but can i remove labels i dont want from memory ?
edit:
this is my method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
MyTableCell *cell = (MyTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[MyTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(j*50.0, 0, 49.0,logicTable.rowHeight)] autorelease];
label.tag = 1;
label.text = [NSString stringWithFormat:#"ABC"];
label.textAlignment = UITextAlignmentCenter;
label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:label];
return cell;
}
What it sounds like you are doing is, in your cellForRowAtIndexPath method, you are setting up your UITableViewCells with some labels in them and each time, you are making the labels from scratch. What you should be doing is, setting up the labels if you are making a new cell, and then setting the values on the labels outside of this to fully utilize the ability to reuse table view cells to improve performance of scrolling the table view.
The key method is -viewWithTag: which, along with the tag property on UIView you can use to find a specific subview.
A little sample code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"MyCellIdentifier";
UITableViewCell *cell = (WHArticleTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel *firstLabel = nil;
UILabel *secondLabel = nil;
UILabel *thirdLabel = nil;
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
firstLabel = [[[UILabel alloc] initWithFrame: CGRectMake(0.0, 0.0, 20.0, 20.0)] autorelease];
firstLabel.tag = 1;
[cell addSubview:firstLabel];
secondLabel = [[[UILabel alloc] initWithFrame: CGRectMake(20.0, 0.0, 20.0, 20.0)] autorelease];
secondLabel.tag = 2;
[cell addSubview:secondLabel];
thirdLabel = [[[UILabel alloc] initWithFrame: CGRectMake(40.0, 0.0, 20.0, 20.0)] autorelease];
thirdLabel.tag = 3;
[cell addSubview:thirdLabel];
}
else
{
firstLabel = (UILabel *)[cell viewWithTag:1];
secondLabel = (UILabel *)[cell viewWithTag:2];
thirdLabel = (UILabel *)[cell viewWithTag:3];
}
firstLabel.text = #"First Label's Text Here";
secondLabel.text = #"Second Label's Text Here";
thirdLabel.text = #"Third Label's Text Here";
return cell;
}

tableview text over ride with previous text

i am displaying array of data in a table view.
my code for that is like this.
- (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];
}
// Set up the cell
CGRect labelFrame = CGRectMake(0,0 , 100, 25);
itemNameLabel = [[[UILabel alloc] initWithFrame:labelFrame] autorelease];
itemNameLabel.textAlignment = UITextAlignmentLeft;
itemNameLabel.backgroundColor = [UIColor clearColor];
itemNameLabel.text = [tableData objectAtIndex:indexPath.row];
CGRect anotherLabelFrame = CGRectMake(120, 0, 100, 25);
quantityLabel = [[[UILabel alloc] initWithFrame:anotherLabelFrame] autorelease];
quantityLabel.textAlignment = UITextAlignmentLeft;
quantityLabel.backgroundColor = [UIColor clearColor];
MyGroceryListAppDelegate *appDelegate = (MyGroceryListAppDelegate *)[[UIApplication sharedApplication] delegate];
Category *obj = (Category *)[appDelegate.itemsList objectAtIndex:indexPath.row];
quantityLabel.text = [NSString stringWithFormat:#"%d",obj.quantity];
[cell.contentView addSubview:itemNameLabel];
[cell.contentView addSubview:quantityLabel];
return cell;
}
how can i resolve this.
can any one please help me.
Thank u in advance.
i am giving text for itemNameLabel is using array and for quantityLabel using class object.
so,when i scrolling table view the array names are override on other label,but quantity label is not overriding because it is from class.
i need to display itemNameLabel from array since it changes it value.
when i scroll table view my tableview is looking like this.
Ideally you should alloc all lable in this section
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
and in the else part you should just Tag those labels and reuse after it but there is another short and sweet way
if Application is not much resource consuming
change
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
to
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
Try setting Height for each row using the function
- (CGFloat)tableView:(UITableView *)tv heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 100.0f;
}

UITextField as subview in a UITableView

How can I achieve something that we can see on login view of Skype app?
It looks like a table View to me with UILabel and UITextField inside it as a subview. I've never done something like so I'd like to know how can I do that.
You can do this. Check the following 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 *startDtLbl = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 80, 25)];
startDtLbl.backgroundColor = [UIColor clearColor];
startDtLbl.tag = 1;
[cell.contentView addSubview:startDtLbl];
UITextField *passwordTF = [[UITextField alloc] initWithFrame:CGRectMake(100, 5, 200, 35)];
passwordTF.tag = 2;
[cell.contentView addSubview:passwordTF];
}
return cell;
}
You can add the fields like the above.