开发者

UITableview, vertically center a object of a cell with dynamic height?

I have some UITableViewCell with a TextView that is dynamic and some other objects that should be vertically centered.

At the moment i calculate the height of the UITextView and uses it as reference to set everything in place.

The problem i have is that the sometimes it calculates wrong and the "priceLabel" that always should be in middle ends up in the bottom? Happens like 20% of the times i scroll so the cell will become visiable again.

Is there a way to get the higeht of the cell? And why does it calculate wrong? It also cuts the UITextView in half when this problem appear?

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    CGSize size;
    NSString *text;
    /* Height of each cell */
     text = @"Some different lengths of text";
     size = [text sizeWithFont:[UIFont fontWithName:@"TrebuchetMS" size:17] constrainedToSize:CGSizeMake(theTableView.bounds.size.width - 260, 800) lineBreakMode:UILineBreakModeWordWrap];

     float totalSize = size.height + 10;
     if (totalSize < 120) {
          return 120;
     } else {
         return totalSize;
     }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {

        CGSize size;
        NSString *text;
        float totalSize;
        /* Height of each cell */
        text = @"Some d开发者_Go百科ifferent lengths of text";
        size = [text sizeWithFont:[UIFont fontWithName:@"TrebuchetMS" size:17] constrainedToSize:CGSizeMake(theTableView.bounds.size.width - 260, 800) lineBreakMode:UILineBreakModeWordWrap];
            if (size.height + 10 < 120) {
                totalSize = 120;
            } else {
                totalSize = size.height + 10;
            }
        }

        /* Createing a new Cell */
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];

        /* Create the text Label */
        textView = [[[UITextView alloc] initWithFrame: CGRectMake(90,35,theTableView.bounds.size.width - 260 , totalSize)] autorelease];
        [cell.contentView addSubview:textView];
        textView.tag = TEXT_LABEL_TAG;
        textView.backgroundColor = [UIColor clearColor];
        textView.textColor = [UIColor blackColor];
        textView.font = [UIFont fontWithName:@"TrebuchetMS" size:17];
        textView.editable = NO;
        textView.userInteractionEnabled = NO;

        /* Create price Label */
        priceLabel = [[[UILabel alloc] initWithFrame: CGRectMake(theTableView.bounds.size.width - 225, (totalSize/2) - (35/2), 120 , 35)] autorelease];
        [cell.contentView addSubview:priceLabel];
        priceLabel.tag = PRICE_LABEL_TAG;
        priceLabel.textAlignment = UITextAlignmentRight;
        priceLabel.backgroundColor = [UIColor clearColor];
        priceLabel.textColor = [UIColor redColor];
        priceLabel.font = [UIFont fontWithName:@"TrebuchetMS-Bold" size:22];


I've had a couple of glitches when I defined the frame frame of a subview and its positions at the same time as you do in your initWithFrame: calls. It seems the frame isn't very robust during transformations. I've had better luck setting bounds and center properties after the subview is initialized. This might give you better control of the centering, too.

label.bounds = CGRectMake(0,0,0.6*self.view.frame.size.width,30);
label.center = CGPointMake(0.5*self.view.frame.size.width, 0.23*self.view.bounds.size.height);


Your code looks fine, except there is an integer math in coordinates. So, try to use

priceLabel = [[[UILabel alloc] initWithFrame: CGRectMake(theTableView.bounds.size.width - 225, (totalSize/2.f) - (35.f/2.f), 120 , 35)] autorelease];

It can help, but i'm not sure.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜