0

我的应用程序中的 UILabel 行为异常,我想知道是否有人对如何解决它有任何想法或建议。

1.首先我设置了标签的框架,它们都将出现在 tableView 的单元格内。

CGRect CellFrame = CGRectMake(0, 0, 300, 75);
CGRect Label1Frame = CGRectMake(10, 5, 290, 20);
CGRect Label2Frame = CGRectMake(0, 20, 290, 50);
UILabel *lblTemp;

UITableViewCell *cell = [[UITableViewCell alloc] initWithFrame:CellFrame        reuseIdentifier:cellIdentifier]; 
  1. 然后我设置标签的属性

1.

lblTemp = [[UILabel alloc] initWithFrame:Label1Frame];
lblTemp.tag = 1;
lblTemp.font = [UIFont fontWithName:@"Helvetica" size:12.0];
lblTemp.textColor = [UIColor blackColor];
lblTemp.lineBreakMode = UILineBreakModeWordWrap;
lblTemp.numberOfLines = 0;
[cell.contentView addSubview:lblTemp];

//Initialize Label with tag 2.
lblTemp = [[UILabel alloc] initWithFrame:Label2Frame];
lblTemp.tag = 2;
lblTemp.font = [UIFont fontWithName:@"Helvetica" size:10];
lblTemp.textColor = [UIColor lightGrayColor];
lblTemp.lineBreakMode = UILineBreakModeWordWrap;
lblTemp.numberOfLines = 0;
[cell.contentView addSubview:lblTemp];

return cell;

3.然后我设置单元格的内容

//set up the cell

UILabel *lblTemp1 = (UILabel *)[cell viewWithTag:1];
UILabel *lblTemp2 = (UILabel *)[cell viewWithTag:2];

CaseFeed *aCaseFeed = [[CaseFeed alloc] init];
aCaseFeed = [CaseFeedbook objectAtIndex:indexPath.row];

lblTemp1.text = [NSString stringWithFormat:@"%@", aCaseFeed.title];
lblTemp2.text = [NSString stringWithFormat:@"%@", aCaseFeed.description];

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

return cell;

问题: lblTemp2(单元格中的第二个标签)的内容将跨越 2 行或更多行。在运行时,填充 lblTemp2 的文本时,前两行完美显示,但第三行从框架左侧开始。

关于如何解决这个问题的任何想法?为什么会这样?

4

1 回答 1

0

使用以下代码设置 tableView 的单元格

- (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];
    }
    else 
    {
        for (id v in cell.contentView.subviews) 
        {
            UIView *realV = (UIView *) v;
            [realV removeFromSuperview];
        }
    }
    CGRect CellFrame = CGRectMake(0, 0, 300, 75);
    CGRect Label1Frame = CGRectMake(10, 5, 290, 20);
    CGRect Label2Frame = CGRectMake(0, 20, 290, 50);


    UILabel *label1 = [[UILabel alloc] init];
    label1.frame = Label1Frame;
    label1.tag = 1;
    label1.font = [UIFont fontWithName:@"Helvetica" size:12.0];
    label1.textColor = [UIColor blackColor];
    label1.lineBreakMode = UILineBreakModeWordWrap;
    label1.numberOfLines = 0;
    [cell.contentView addSubview:label1];

    UILabel *label2 = [[UILabel alloc] init];
    label2.frame = Label2Frame;
    label2.tag = 2;
    label2.font = [UIFont fontWithName:@"Helvetica" size:10.0];
    label2.textColor = [UIColor lightGrayColor];
    label2.lineBreakMode = UILineBreakModeWordWrap;
    label2.numberOfLines = 0;
    [cell.contentView addSubview:label2];

    CaseFeed *aCaseFeed = [[CaseFeed alloc] init];
    aCaseFeed = [CaseFeedbook objectAtIndex:indexPath.row];

    label0.text = [NSString stringWithFormat:@"%@", aCaseFeed.title];
    label1.text = [NSString stringWithFormat:@"%@", aCaseFeed.description];

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

希望这可以帮助

于 2012-09-07T19:21:31.633 回答