0

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];
}
4

8 回答 8

1

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)];
于 2013-10-02T07:52:02.813 回答
1

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)];
于 2013-10-03T05:02:01.827 回答
0

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
}
于 2013-10-02T07:51:53.547 回答
0

If you want your cell's height, just do the following:

category.frame = CGRectMake(0,0,width,cell.bounds.size.height);
于 2013-10-02T07:53:13.510 回答
0
    - (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...

于 2013-10-02T07:54:14.263 回答
0

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.

于 2013-10-02T08:11:26.263 回答
0

// try this code

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(95,1,140,cell.frame.size.height)];
于 2013-10-02T09:59:10.653 回答
0

just set the cell bounds to label's frame,like below.

UILabel *myLabel = [[UILabel alloc] initWithFrame:cell.bounds];

于 2014-11-17T11:23:36.320 回答