2011-08-24 72 views
0

我有一个自定义UITableViewCell类,并希望线性显示图像和字符串。例如:在子类UITableViewCell内动态创建子视图

第1列:[IMAGE1]字符串1 [图像2]字符串2 [的Image3]

第2列:[图像4] STRING3 [图像5]

这些图像具有不同的宽度,但我想要等间距。我将如何做到这一点?我试图操纵子视图和CGRectMake无济于事。

此外,我使用NSDictionary来保存内容,并且每个单元格的图像/字符串数量不是固定的。

CustomCell类:

#import "CustomCell.h" 


@implementation CustomCell 

@synthesize primaryLabel,secondaryLabel,image1; 


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { 
    if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) { 
     // Initialization code 
     primaryLabel = [[UILabel alloc]init]; 
     primaryLabel.textAlignment = UITextAlignmentLeft; 
     primaryLabel.font = [UIFont systemFontOfSize:16]; 
     secondaryLabel = [[UILabel alloc]init]; 
     secondaryLabel.textAlignment = UITextAlignmentLeft; 
     secondaryLabel.font = [UIFont systemFontOfSize:14]; 
     image1 = [[UIImageView alloc]init]; 

     [self.contentView addSubview:primaryLabel]; 
     [self.contentView addSubview:secondaryLabel]; 
     [self.contentView addSubview:image1]; 

    } 
    return self; 
} 

- (void)layoutSubviews { 
    [super layoutSubviews]; 
    CGRect frame; 
    frame= CGRectMake(0 ,5, 200, 25); 
    primaryLabel.frame = frame; 

    frame= CGRectMake(0 ,30, 200, 25); 
    secondaryLabel.frame = frame; 

    frame= CGRectMake(0, 60, 23, 20); 
    image1.frame = frame; 

...

RootViewController

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

    static NSString *CellIdentifier = @"Cell"; 

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    } 

    // Set up the cell... 
    NSDictionary *dictionary = nil; 


//Search 
    if (tableView == self.searchDisplayController.searchResultsTableView) 
    { 
     dictionary = [self.filteredListContent objectAtIndex:indexPath.row]; 
    } 
    else 
    { 
     dictionary = [self.tableDataSource objectAtIndex:indexPath.row]; 
    } 


//Original 
    cell.primaryLabel.text = [dictionary objectForKey:@"Title"];  


    for (NSArray *keystroke in [dictionary objectForKey:@"Strokes"]) { 


      for (int i = 0; i < 2; i++) { 


       if ([(NSString *)keystroke isEqualToString:@"string1"] || [(NSString *)keystroke isEqualToString:@"string2"]) { 
       cell.secondaryLabel.text = (NSString *)keystroke; 
       } 

       else { 
      NSString *imageFilePath = [NSString stringWithFormat:@"%@.png", keystroke]; 
      NSLog(@"%@", imageFilePath); 
      UIImage *myimage = [UIImage imageNamed:imageFilePath]; 

       cell.image1.image = myimage; 

     } 
     } 
    } 
    return cell; 


} 

...

显然有很多孔的位置。首先,当我循环访问字典时,我需要将子视图的CustomCell右移,以便将图像/文本放置在上一个子视图旁边。

回答

0

您正处于正确的轨道上。在您的UITableViewCell子类中,您需要手动覆盖layoutSubviews,为每个UIImageViewUILabel定义CGRects,并将它们设置为相应视图的框架。

结账CGRectGetMaxX。在这方面它将非常有用。

+0

我已经添加了一些代码,因为我仍然丢失。谢谢回复! –