0

私は、IOS 開発を始めて、最初のクライアント プロジェクトに取り組んでいます。

私は次の場所で立ち往生しています:

したがって、今月 10 人の誕生日がある場合、10 個のイメージビューを 1 行に表示する必要があります (3 つの写真、3 つの写真、1 つの写真)。

誰かが上記で私を助けることができますか?それは非常に高く評価されます。

4

2 に答える 2

3

テーブルビューに画像/ラベル/ボタンを追加する場合、最も簡単な方法は UITableViewCell をサブクラス化することです。たとえば、5 つの画像 (3 つの画像、3 つの画像、1 つの画像を理解していない) があり、それらを表示したい場合テーブル行の場合、次のようなことができます。

  1. 次のように、UITableviewCell のサブクラスである CustomCell という名前のプロジェクトに新しいファイルを追加します。

    @interface CustomCell : UITableViewCell {  
    
        UIImageView *imageView1;  
        UIImageView *imageView2;  
        UIImageView *imageView3;  
        UIImageView *imageView4;  
        UIImageView *imageView5;  
    }  
    @property (nonatomic, retain) UIImageView *imageView1;  
    @property (nonatomic, retain) UIImageView *imageView2;  
    @property (nonatomic, retain) UIImageView *imageView3;  
    @property (nonatomic, retain) UIImageView *imageView4;  
    @property (nonatomic, retain) UIImageView *imageView5;  
    @end  
    
  2. CustomCell.m には、このコードを次のように配置します。

    @synthesize imageView1, imageView2, imageView3, imageView4, imageView5;
    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
    {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) 
        {           
            self.imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(10, 8, 50, 45)];
            [self.contentView addSubview:imageView1];
    
            self.imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(65, 8, 50, 45)];
            [self.contentView addSubview:imageView2];
    
            self.imageView3 = [[UIImageView alloc] initWithFrame:CGRectMake(120, 8, 50, 45)];
            [self.contentView addSubview:imageView3];
    
            self.imageView4 = [[UIImageView alloc] initWithFrame:CGRectMake(170, 8, 50, 45)];
            [self.contentView addSubview:imageView4];
    
            self.imageView5 = [[UIImageView alloc] initWithFrame:CGRectMake(225, 8, 50, 45)];
            [self.contentView addSubview:imageView5];
        }
    
    return self;
    }  
    

これで、UITableViewCell のサブクラス化が完了しました。View Controller でそれらを使用する方法を教えてください。
View Controller クラスに CustomCell.h をインポートする必要があります。そして、cellForRowAtIndexPath デリゲート メソッドで次のようなことを行います。

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
    {  
        static NSString *CellIdentifier = @"Cell";  

        //our custom cell  
        CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
        if (cell == nil) {  
            cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];  
        }  

        // Configure the cell.  

        //cell.imageView1.image = [UIImage imageNamed:@"testImg1"];  
        //cell.imageView2.image = [UIImage imageNamed:@"testImg2"];  
        //cell.imageView3.image = [UIImage imageNamed:@"testImg3"];  
        //cell.imageView4.image = [UIImage imageNamed:@"testImg4"];  
        //cell.imageView5.image = [UIImage imageNamed:@"testImg5"];  
        //return cell;  

あなたはより良いものを使うことができました:

    NSMutableArray *imgArray = [[NSMutableArray alloc] initWithObjects:cell.imageView1, cell.imageView2,cell.imageView3, cell.imageView4, cell.imageView5, nil];

    for (int imageCounter = 0; imageCounter<5; imageCounter++) {
        [[imgArray objectAtIndex:imageCounter] setImage:[UIImage imageNamed:[NSString stringWithFormat:@"testImg%d", imageCounter]]];
            }  
return cell; 
}

これは、問題のコンテキストに対する非常にプロトタイプのソリューションです。
それがあなたを助けることを願っています:)

于 2012-05-01T14:54:53.260 に答える
0
- (UITableViewCell *)tableView:(UITableView *)tTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryNone;
        for (int t =0; t<cell.subviews.count; t=t) {
            if (![[[cell.subviews objectAtIndex:t].class]isEqualToString:@"UIImageView"]) {
                t++;
            }
            else {
                [((UIView*)[[cell.subviews objectAtIndex:t])removeFromSuperview];
            }
        }
    }
    for (int t = 0; t<10; t++) {
        UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(cell.frame.size.width/10*t, 0, cell.frame.size.width/10, cell.frame.size.height)];
        [imageView setImage:[UIImage imageNamed:<#(NSString *)#>]];
        [cell addSubview:imageView];
    }
    return cell;
}

(3枚の写真、3枚の写真、1枚の写真)の意味はわかりませんが、そこから理解できるはずです。

于 2012-05-01T14:28:26.673 に答える