0

我有一个自定义UITableViewCell类,想线性显示图像和字符串。例如:

第 1 行:[Image1] string1 [Image2] string2 [Image3]

第 2 行:[Image4] 字符串 3 [Image5]

图像有不同的宽度,但我想要相等的间距。我该怎么做?我试过操纵子视图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向右移动我的子视图,以便我可以将图像/文本放在前一个子视图旁边。

4

1 回答 1

0

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

查看CGRectGetMaxX。在这种情况下它将非常有用。

于 2011-08-24T05:51:28.773 回答