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- 在这里您可以计算每一行的高度。

请注意,如果计算工作量很大,您可能会看到性能下降。如果是这种情况,您可以预先计算值并存储它们。

于 2012-12-12T07:53:18.567 回答
0

您可以使用此方法为您的应用程序提供可变高度支持。

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

每当您想动态设置高度时,请使用您的 tableview 对象调用此方法。

更多信息请点击这里

或将此方法与其他 tableview 委托方法一起使用。

于 2012-12-12T07:55:06.367 回答
0

调整单元格大小会变得相当复杂,尤其是在涉及 UITextViews 时。我建议您简单地使用表格视图框架,例如Sensible TableView,其中所有单元格都会自动调整大小以适应内容。我相信他们现在也有免费版本。

于 2012-12-12T08:24:25.110 回答