0

私はiPhoneの開発に非常に慣れていないので、ある時点で打撃を受けました。実際に必要なのは、いくつかの結果をaUITableViewに表示し、次にセルを表示するUILablesことです。テキストに応じてコンテンツを調整するにUITableView'sはどうすればよいですか。UILabel's実際には、テキストは静的ではありません。実行時に変更される可能性があります。したがって、動的サイズをUILabelに設定する必要があります。null次に、テキストがその場合はスペースが表示されず、すぐに次のテキストがUILabel開始されると仮定します。それは可能ですか?これが私のコードです。

self.tableView=[[UITableView alloc] initWithFrame:CGRectMake(0,150,320,800) style:UITableViewStylePlain];
            self.tableView.delegate=self;
            self.tableView.dataSource=self;
            self.tableView.scrollEnabled = NO;
            [self.view addSubview:self.tableView];

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(currentHtmlElement==@"3")
    {
        static NSString *CellIdentifier=@"Cell";

        UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if(cell == nil)
        {
            cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault    reuseIdentifier:CellIdentifier]autorelease];
        }
        NSMutableDictionary *d = (NSMutableDictionary *) [arr2 objectAtIndex:indexPath.row];
        cell.accessoryType= UITableViewCellAccessoryDetailDisclosureButton;
       UILabel  *name1= [[UILabel alloc]initWithFrame:CGRectMake(10, 5, 320, 40)];
        name1.font=[UIFont boldSystemFontOfSize:16];
        [name1 setTextAlignment:UITextAlignmentLeft];
        [name1 setText:[d valueForKey:@"Name"]];
        name1.tag = 111;
        [cell addSubview:name1];
        [name1 release];

        UILabel *codetype=[[UILabel alloc]initWithFrame:CGRectMake(10, 45, 320,15)];
        codetype .font=[UIFont boldSystemFontOfSize:12];        
        [codetype setTextAlignment:UITextAlignmentLeft];
        [codetype  setText:[d valueForKey:@"Code"]];
        codetype.tag=112;
        [cell addSubview:codetype];
        [codetype  release];    

        UILabel *id=[[UILabel alloc]initWithFrame:CGRectMake(10, 68, 320,10)];
        id .font=[UIFont fontWithName:@"arial" size:12];        
        [id setTextAlignment:UITextAlignmentLeft];
        [id  setText:[d valueForKey:@"ID"]];
        id.tag=113;
        [cell id ];
        [id  release]; 

        UILabel *address=[[UILabel alloc]initWithFrame:CGRectMake(10, 85, 320,10)];
        address .font=[UIFont fontWithName:@"arial" size:12];        
        [address setTextAlignment:UITextAlignmentLeft];
        [address  setText:[d valueForKey:@"address"]];
        line2.tag=114;
        [cell addSubview: address];
        [address  release]; 

        UILabel *city = [[UILabel alloc]initWithFrame:CGRectMake(10, 105, 320, 10)];
        city .font=[UIFont fontWithName:@"arial" size:12];        
        [city  setTextAlignment:UITextAlignmentLeft];
        [city  setText:[d valueForKey:@"City"]];
        city.tag=115;
        [cell addSubview:city ];
        [city  release];
}

ここでは、すべてのUILabelを静的サイズに設定しましたが、必要なのは動的サイズです。アドレスラベルがnullの場合、スペースは表示されず、次のラベル、つまり都市はidの直後に開始する必要があります。可能?

4

3 に答える 3

0

UILable以下のコードでコンテンツを調整できます

CGSize sizeToMakeLabel = [name1.text sizeWithFont: name1.font]; 
name1.frame = CGRectMake(name1.frame.origin.x, name1.frame.origin.y, 
sizeToMakeLabel.width, sizeToMakeLabel.height); 

そしてここに次のように2番目のラベルを設定します。

アップデート

     if([[d valueForKey:@"address"] isEquelToString:@""]){
          UILabel *city=[[UILabel alloc]initWithFrame:CGRectMake(10, idLable.frame.origin.y + id.frame.size.height+10 , 320,15)];
          city .font=[UIFont fontWithName:@"arial" size:12];        
          [city  setTextAlignment:UITextAlignmentLeft];
          [city  setText:[d valueForKey:@"City"]];
          city.tag=115;
          [cell addSubview:city ];
          CGSize sizeToMakeLabel = [city.text sizeWithFont: city.font]; 
          city.frame = CGRectMake(city.frame.origin.x, city.frame.origin.y, 
sizeToMakeLabel.width, sizeToMakeLabel.height); 
        }
      else{
          UILabel * address =[[UILabel alloc]initWithFrame:CGRectMake(10, idLable.frame.origin.y + id.frame.size.height+10 , 320,15)];
address .font=[UIFont fontWithName:@"arial" size:12];        
          [address setTextAlignment:UITextAlignmentLeft];
          [address  setText:[d valueForKey:@"address"]];
          line2.tag=114;
          [cell addSubview: address];
          CGSize sizeToMakeLabel = [address.text sizeWithFont: address.font]; 
          address.frame = CGRectMake(address.frame.origin.x, address.frame.origin.y, 
sizeToMakeLabel.width, sizeToMakeLabel.height); 
        }

など、すべてのラベルについて。

ラベル全体については、このロジックに従ってください

これがお役に立てば幸いです...

于 2012-11-23T08:01:31.840 に答える
0

重要なのは、heightForRowAtIndexPathを使用する必要があるということです。問題は、cellForRowAtIndexPathの前に呼び出されるため、テキストがコンポーネントに含まれておらず、コンポーネントのサイズをテキストに合わせて単純に調整できないことです。したがって、sizeWithFontを使用してテキストサイズを計算します。

通常/標準テキストの場合

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //here you get your text string
    NSString* theText =  ... ; 

    //then calculate the size. systemFontOfSize depends on your font size
    //yourLabelWidth must be known and usually depends on scree size or if the tableview has an index
    CGSize textSize = [theText sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(yourLabelWidth, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];

    return textSize.height;
}

属性付きテキストの場合

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //here you get your text string
    NSAttributedString* theText =  ...;

    //then calculate the size.
    //yourLabelWidth must be known and usually depends on scree size or if the tableview has an index
    CGRect rectSize = [theText boundingRectWithSize:CGSizeMake(yourLabelWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin context:NULL];

return rectSize.size.height;

    return textSize.height;
}
于 2012-11-23T08:05:32.497 に答える
0

私は自分のコードでこのタスクを実行しました....このコードを使用して、コードに応じて変更を加えることができます....問題が発生した場合はお知らせください。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    NSMutableDictionary *dicNotification =[[NSMutableDictionary alloc]initWithDictionary: [myNotification objectAtIndex:indexPath.row]];

    //code to get notification text height
    NSString *text = [dicNotification  objectForKey:@"message"];
    CGSize constraint = CGSizeMake(175, 100.0f);
    CGSize size = [text sizeWithFont: [UIFont fontWithName:@"Verdana" size:12] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
    return size.height;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) 
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

        //label for message
        UILabel *lblMsg = [[UILabel alloc] initWithFrame:CGRectZero];
        lblMsg.tag = 12;
        lblMsg.backgroundColor = [UIColor clearColor];
        [lblMsg setFont:[UIFont fontWithName:@"Verdana" size:12]];
        [lblMsg setLineBreakMode:UILineBreakModeWordWrap];

        [cell addSubview:lblMsg];
    }

    NSMutableDictionary *dicNotification =[[NSMutableDictionary alloc]initWithDictionary: [myNotification objectAtIndex:indexPath.row]];

    //get the message height set frame height to imageView(BubbleMid & Top) & labelMSG
    NSString *txt = [dicNotification  objectForKey:@"message"];
    CGSize constraint = CGSizeMake(175, 2000.0f);
    CGSize size = [txt sizeWithFont: [UIFont fontWithName:@"Verdana" size:12] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];

    //set frame height to labelMsg
    UILabel *lblMsg = (UILabel *)[cell viewWithTag:12];
    lblMsg.frame = CGRectMake(15,25, 175, size.height);
    [lblMsg setNumberOfLines:size.height/16];

    //set labelMsg text
    lblMsg.text = [dicNotification  objectForKey:@"message"];

    return cell;
}
于 2012-11-23T14:27:19.483 に答える