0

カスタムUITableViewCellクラスがあり、画像と文字列を直線的に表示したいと考えています。例えば:

行 1: [画像 1] 文字列 1 [画像 2] 文字列 2 [画像 3]

行 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サブビューを右に移動して、前のサブビューの隣に画像/テキストを配置できるようにする必要があります。

4

1 に答える 1

0

あなたは正しい軌道に乗っています。UITableViewCellサブクラスでは、 をオーバーライドし、それぞれまたは手動でlayoutSubviews定義し、それぞれのビューのフレームとして設定する必要があります。CGRectsUIImageViewUILabel

CGRectGetMaxXを確認してください。これは、このコンテキストで非常に役立ちます。

于 2011-08-24T05:51:28.773 に答える