1

My UILabel is in UITableViewCell and Here is my code

lblgoal = [[UILabel alloc] initWithFrame:CGRectMake(60,10, 250, 20)];
CGSize maximumLabelSize1 = CGSizeMake(230,9999);
            lblgoal.numberOfLines = 0;
            lblgoal.adjustsFontSizeToFitWidth = NO;
            CGSize expectedLabelSize = [sg.subgoal_name sizeWithFont:lblgoal.font
                                                   constrainedToSize:maximumLabelSize
                                                   lineBreakMode:lblsubgoal1.lineBreakMode];
CGRect newFrame = lblgoal.frame;
            newFrame.size.height = expectedLabelSize.height;
            lblgoal.frame = newFrame;

But this only works if i put the width of UILabel to 200, if i put more than 200 width, then it all comes in one line with dotted at the end. And, UITableView cell width is more than 500.

4

3 回答 3

1

实际上,请确保在 sizeWithFont 函数和 lblgoal 标签中都设置为,如下所示linebreakmodeUILineBreakModeWordWrap

lblgoal.numberOfLines = 0;
lblgoal.lineBreakMode = UILineBreakModeWordWrap; //NSLineBreakByWordWrapping for iOS 6

sizeToFit编辑:设置后尝试lineBreakMode

[lblgoal sizeToFit];

还要确保在cellForRowAtIndexPath方法中执行此操作。

您还应该实现以下委托方法,以确保您的单元格的高度也相应调整。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
于 2013-06-03T07:01:17.203 回答
0

当您检查 MaximumLabelSize 时,您有一个固定的宽度。

CGSize maximumLabelSize1 = CGSizeMake(230,9999);

而是使用

CGSize maximumLabelSize1 = CGSizeMake(MAXFLOAT, MAXFLOAT);

但这将使 Oppurtunity 将宽度扩展到任何范围。这对你不起作用。所以将最大宽度设置为 500。

CGSize maximumLabelSize1 = CGSizeMake(500, MAXFLOAT);

并为标签设置框架:

CGRect newFrame = lblgoal.frame;
newFrame.size.height = expectedLabelSize.height;
newFrame.size.width = expectedLabelSize.width;
于 2013-06-03T06:54:35.357 回答
0
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  {
     if (!self.lastIndexPath) 
     {
        self.lastIndexPath = indexPath;
     }

    if ([self.lastIndexPath row] != [indexPath row])
    {
        UITableViewCell *newCell = [tableView cellForRowAtIndexPath: indexPath];
        newCell.accessoryType = UITableViewCellAccessoryCheckmark;

        UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:self.lastIndexPath]; 
        oldCell.accessoryType = UITableViewCellAccessoryNone;

        self.lastIndexPath = indexPath;  
    }
    else {
        UITableViewCell *newCell = [tableView cellForRowAtIndexPath: indexPath];
        newCell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
于 2013-06-03T07:22:17.770 回答