2

2 つのデザインに 2 種類のセルを追加するアプリケーションがあります。そのため、カスタムUITableViewCellオブジェクト クラスを使用しています。しかし、私の問題は、このセルでテキスト ビューを使用していることです。コンテンツのサイズに応じて行の高さを調整する必要があります。これは私のコードがどのように見えるかです:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier1 = @"CellIdentifier";
    static NSString *CellIdentifier2 = @"Cell";
        
    if(messagesarray==nil||[messagesarray count]==0)
    {
        
    }
    else
    {
        NSMutableDictionary *dicttable=[messagesarray objectAtIndex:indexPath.row];
        NSString *head=[dicttable objectForKey:@"gfgfg"];
        NSString *head1=[dicttable objectForKey:@"gfgfgf"];
        if([type isEqualToString:@"m"])    
        {
            if([head isEqualToString:@"NA"])
            {
             
                CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
                if (cell == nil)
                {
                    cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier1] autorelease];
                    cell.backgroundView = [ [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"white_top_portion.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]autorelease];
                }
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                cell.nameLabel.text=head;
                cell.msg.text=head1;
                [cell.button addTarget:self action:@selector(callAction:) forControlEvents:UIControlEventTouchUpInside];     
              
                if(icon!=nil)
                {
                    if(![[ImageCache sharedImageCache] hasImageWithKey:icon])
                    {
                        cell.myImageView.image = [UIImage imageNamed:@"fggtgf.png"];
                        NSArray *myArray = [NSArray arrayWithObjects:cell.myImageView,icon,@"fgfgfg.png",[NSNumber numberWithBool:NO],nil];
                        AppDelegate *appDelegate = (AppDelegate  *)[[UIApplication sharedApplication] delegate];
                        [appDelegate performSelectorInBackground:@selector(updateImageViewInBackground:) withObject:myArray];   
                    }
                    else 
                    {
                        cell.myImageView.image = [[ImageCache sharedImageCache] getImagefromCacheOrUrl:icon];
                    }
                }
                else
                {
                    cell.myImageView.image = [UIImage imageNamed:@"fgfgfg.png"];  
                  
                } 
         
                cell.label.text=sub;   
                return cell;
            }
            else
            {
                Customcellwithimage *cell = (Customcellwithimage *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
             
                if (cell == nil)
                {
                    cell = [[[Customcellwithimage alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier2] autorelease];
                    cell.backgroundView = [ [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"white_deal_bg.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]autorelease];
                }
             
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                cell.datelabel1.text=subtitle;
                cell.nameLabel1.text=head;
                cell.msg1.text=head1;
                if(icon!=nil)
                {
                    if(![[ImageCache sharedImageCache] hasImageWithKey:icon])
                    {
                        cell.myImageView1.image = [UIImage imageNamed:@"fgfgfgf.png"];
                        NSArray *myArray = [NSArray arrayWithObjects:cell.myImageView1,icon,@"fgfgfgf.png",[NSNumber numberWithBool:NO],nil];
                        MFAppDelegate *appDelegate = (MFAppDelegate  *)[[UIApplication sharedApplication] delegate];
                        [appDelegate performSelectorInBackground:@selector(updateImageViewInBackground:) withObject:myArray];   
                       
                    }
                    else 
                    {
                        cell.myImageView1.image = [[ImageCache sharedImageCache] getImagefromCacheOrUrl:icon];
                    }
                }
                else
                {
                    cell.myImageView1.image = [UIImage imageNamed:@"fgfgfgf.png"];  
                } 
              
                cell.cellview1.image=[UIImage imageNamed:@"vf.png"];
                cell.label1.text=sub;
                [cell.button1 addTarget:self action:@selector(callAction:) forControlEvents:UIControlEventTouchUpInside]; 
                cell.bannerview1.image=[UIImage imageNamed:@"vfgf.png"];
                return cell;
            }   
        }                 
    }  
}
       

 セルごとに異なる行の高さを達成するのを手伝ってくれる人はいますか?.高さ- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPathを調べる必要があり、現在の行のセルオブジェクトを見つける必要がある方法を試しましたが、成功しませんでした.誰かが私を案内してくれますか?正しい方向に

4

3 に答える 3

0

このメソッドを使用して、アプリケーションの可変高さをサポートできます。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;

高さを動的に設定したいときはいつでも、このメソッドを tableview オブジェクトで呼び出します。

詳細については、ここをクリックしてください

または、このメソッドを他のテーブルビュー デリゲート メソッドと共に使用します。

于 2012-12-12T07:55:06.367 に答える
0

特に UITextViews が関係している場合、セルのサイズ変更はかなり複雑になる可能性があります。すべてのセルがコンテンツに合わせて自動的にサイズ変更されるSensible TableViewなどのテーブル ビュー フレームワークを使用することをお勧めします。無料版もあると思います。

于 2012-12-12T08:24:25.110 に答える
0

あなたが探していると思います- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath-ここでは、個々の行の高さを計算できます。

計算が大量の場合、パフォーマンスが低下する可能性があることに注意してください。この場合、値を事前に計算して保存できます。

于 2012-12-12T07:53:18.567 に答える