2012-01-13 34 views
5

Tôi có ba UILabel tùy chỉnh trong UITableViewCell. Thiết kế ô như thế này,Thay đổi chiều cao UITableviewCell tự động trong iPhone?

Date(12.1.2012) Time(12.00pm) 
Cost: $20.00 
Details 

Tôi đã chỉ định chiều cao hàng 100 trong - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;. Nhưng, bây giờ các chi tiết một số lần trở lại trống (Null) từ máy chủ. Vào thời điểm đó, tôi cần phải loại bỏ nhãn Chi tiết khỏi ô và thay đổi chiều cao ô (hàng) cụ thể thành 70. Làm cách nào tôi có thể thực hiện việc này? Tôi đã tìm kiếm mức độ tốt nhất của mình trong Google. Nhưng, tôi vẫn chỉ bối rối để làm điều này. Bạn có thể giúp tôi không? Cảm ơn. Tôi có một số ý tưởng từ liên kết này - http://www.cimgf.com/2009/09/23/uitableviewcell-dynamic-height/. Họ chỉ sử dụng một nhãn để thay đổi kích thước chiều cao hàng. Làm ơn giúp tôi.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return 90; 
} 

    - (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 *dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 30)]; 
            dateLabel.tag = 100; 
            dateLabel.backgroundColor = [UIColor clearColor]; 
            dateLabel.font = [UIFont fontWithName:@"Helvetica" size:16]; 
            dateLabel.font = [UIFont boldSystemFontOfSize:16]; 
            dateLabel.highlightedTextColor = [UIColor whiteColor]; 
            [cell.contentView addSubview: dateLabel]; 

      UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(120, 0, 200, 25)]; 
            timeLabel.tag = 101; 
            timeLabel.backgroundColor = [UIColor clearColor]; 
            timeLabel.font = [UIFont fontWithName:@"Helvetica" size:14]; 
            timeLabel.highlightedTextColor = [UIColor whiteColor]; 
            [cell.contentView addSubview: timeLabel]; 

      UILabel *costLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 300, 25)]; 
            costLabel.tag = 102; 
            costLabel.backgroundColor = [UIColor clearColor]; 
            costLabel.font = [UIFont fontWithName:@"Helvetica" size:14]; 
            costLabel.font = [UIFont boldSystemFontOfSize:14]; 
            costLabel.highlightedTextColor = [UIColor whiteColor]; 
            [cell.contentView addSubview: costLabel]; 

            UILabel *eventLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 60, 300, 25)]; 
            eventLabel.tag = 103; 
            eventLabel.backgroundColor = [UIColor clearColor]; 
            eventLabel.font = [UIFont fontWithName:@"Helvetica" size:14]; 
            eventLabel.font = [UIFont boldSystemFontOfSize:14]; 
            eventLabel.highlightedTextColor = [UIColor whiteColor]; 
            [cell.contentView addSubview: eventLabel]; 
     } 

        UILabel *dateLabel = (UILabel *) [cell.contentView viewWithTag:100]; 
        dateLabel.text = [DateArray objectAtIndex:indexPath.row]; 

     UILabel * timeLabel = (UILabel *) [cell.contentView viewWithTag:101]; 
        timeLabel.text = [timeArray objectAtIndex:indexPath.row]; 

     UILabel * costLabel = (UILabel *) [cell.contentView viewWithTag:102]; 
        costLabel.text = [costArray objectAtIndex:indexPath.row]; 

        UILabel *eventLabel = (UILabel *) [cell.contentView viewWithTag:103]; 
     NSString *description = [eventArray objectAtIndex:indexPath.row]; 
     if([description isEqualToString:@""]) 
     { 
     eventLabel.text = @""; // Here i don't want to show this label and resize(reduce) the row height to 60; 
     } 
     else 
     { 
        eventLabel.text = description; 
     } 
        return cell; 
    } 

Tôi có thể làm như thế nào? Cảm ơn.

Trả lời

8

Trong khi trả lại chiều cao, hãy kiểm tra xem mô tả chi tiết có trống không. Nếu đó là quay trở lại 60.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
NSString *description = [eventArray objectAtIndex:indexPath.row]; 
if([description isEqualToString:@""]) 
    return 60 
else 
{ 
    return 90; 
} 
} 

Ngoài ra, bạn cần phải loại bỏ nhãn với @"Details" trong cùng một câu lệnh if trong cellForRowAtIndexPath:. Tag nó trước, xác định nó từ các subviews và loại bỏ nó từ các tế bào.

+0

Mr.MadhavanRP cảm ơn câu trả lời của bạn. Tôi đã thử câu trả lời của bạn trong mã của tôi. Tôi đã kiểm tra điều kiện trong cellForRowAtIndexPath: cũng vậy. Lần đầu tiên nó hoạt động tốt. Khi tôi cuộn bảng xem, kích thước ô đã thay đổi và nhãn mô tả đã thay đổi vị trí. Điều đó có nghĩa là nhãn mô tả đã xóa cho tất cả các hàng. Bạn có thể giúp tôi không. thanks – Gopinath

+0

@Gopinath tại sao bạn không thử thêm nhãn chi tiết dưới dạng subview của ô chỉ khi bạn có một số mô tả. Bạn có thể cần phải tạo một lớp tùy chỉnh để giải quyết vấn đề di chuyển. tham khảo câu trả lời của câu hỏi này http://stackoverflow.com/questions/8352530/changing-the-position-of-custom-uibutton-in-custom-uitableviewcell – MadhavanRP

0

Bạn có thể kiểm tra kích thước của chuỗi lợi nhuận bằng cách sử dụng CGSize như thế này -

CGSize size = [detailStr sizeWithFont:[UIFont boldSystemFontOfSize:12.0f] constrainedToSize:CGSizeMake(190, 40) lineBreakMode:UILineBreakModeWordWrap]; 

bạn có thể thay đổi chiều rộng, chiều cao và phông chữ theo tình trạng của bạn. Ngoài ra vui lòng kiểm tra xem detailStr có bằng 0 không trước khi tính toán kích thước.

+0

Mr.Sasdnib cảm ơn câu trả lời của bạn. Tôi đã chỉnh sửa câu hỏi của mình. Làm ơn kiểm tra cái này. Tôi chỉ nhầm lẫn câu trả lời của bạn, bạn có thể giúp tôi. Cảm ơn. – Gopinath

Các vấn đề liên quan