-1

私はiPhone開発者の初心者ですが、

を使用しています。LHS の 3 つのラベルと RHSの3 つのUITableViewラベルのうち、合計 6 つのラベルが必要です。CellUITableView

これが私のコードスニペットです。

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 180;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        
        CGRect frameL;
        frameL.origin.x = 10; 
        frameL.origin.y = 10;
        frameL.size.height = 50;
        frameL.size.width = 200;
        
        CGRect frameR;
        frameR.origin.x = 200; 
        frameR.origin.y = 10;
        frameR.size.height = 40;
        frameR.size.width = 180;
        
        
        UILabel *AlertNameLHS = [[UILabel alloc] initWithFrame:frameL];
        AlertNameLHS.font=[UIFont systemFontOfSize:16.0];
        AlertNameLHS.backgroundColor=[UIColor clearColor];
        AlertNameLHS.textColor=[UIColor redColor];
        AlertNameLHS.text=@"Alert Name :";
        [cell.contentView addSubview:AlertNameLHS];
        
        frameL.origin.y += 60;
        UILabel *AlertMonthLHS = [[UILabel alloc] initWithFrame:frameL];
        AlertMonthLHS.font=[UIFont systemFontOfSize:16.0];
        AlertMonthLHS.backgroundColor=[UIColor clearColor];
        AlertMonthLHS.textColor=[UIColor redColor];
        AlertNameLHS.text=@"Alert Month :";
        [cell.contentView addSubview:AlertMonthLHS];
        
         frameL.origin.y += 120;
         UILabel *DueOnLHS = [[UILabel alloc] initWithFrame:frameL];
         DueOnLHS.font=[UIFont systemFontOfSize:16.0];
        AlertNameLHS.text=@"Due On :";
         [cell.contentView addSubview:DueOnLHS];
        
    AlertNameRHS = [[UILabel alloc] initWithFrame:frameR];
    AlertNameRHS.backgroundColor=[UIColor greenColor];
    AlertNameRHS.textColor=[UIColor redColor];
    AlertNameRHS.textColor=[UIColor redColor];
    AlertNameRHS.font=[UIFont systemFontOfSize:18.0];
    [cell.contentView addSubview:AlertNameRHS];
    
     frameL.origin.y += 80;
    AlertMonthRHS = [[UILabel alloc] initWithFrame:frameR];
    AlertMonthRHS.font=[UIFont systemFontOfSize:18.0];
    [cell.contentView addSubview:AlertMonthRHS];
    
     frameL.origin.y += 120;
    DueOnRHS = [[UILabel alloc] initWithFrame:frameR];
    DueOnRHS.font=[UIFont systemFontOfSize:18.0];
    [cell.contentView addSubview:DueOnRHS];
    }
    
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
    
    AlertNameRHS.text = [Myarray objectAtIndex:indexPath.row];
  
    return cell;
}

しかし、UILabel を正しく表示できません。

どんな助けでも大歓迎です。

編集: 1 :

4

3 に答える 3

1

修正されたコードを参照してください。

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

        CGRect frameL;
        frameL.origin.x = 10; 
        frameL.origin.y = 10;
        frameL.size.height = 50;
        frameL.size.width = 200;

        CGRect frameR;
        frameR.origin.x = 200; 
        frameR.origin.y = 10;
        frameR.size.height = 40;
        frameR.size.width = 180;


        UILabel *AlertNameLHS = [[UILabel alloc] initWithFrame:frameL];
        AlertNameLHS.font=[UIFont systemFontOfSize:16.0];
        AlertNameLHS.backgroundColor=[UIColor clearColor];
        AlertNameLHS.textColor=[UIColor redColor];
        AlertNameLHS.text=@"Alert Name :";
        [cell.contentView addSubview:AlertNameLHS];

        frameL.origin.y += 60;
        NSLog(@"fr %f", frameL.origin.y);
        UILabel *AlertMonthLHS = [[UILabel alloc] initWithFrame:frameL];
        AlertMonthLHS.font=[UIFont systemFontOfSize:16.0];
        AlertMonthLHS.backgroundColor=[UIColor clearColor];
        AlertMonthLHS.textColor=[UIColor redColor];
        AlertMonthLHS.text=@"Alert Month :";
        [cell.contentView addSubview:AlertMonthLHS];

        frameL.origin.y += 60;
        NSLog(@"fr %f", frameL.origin.y);
        UILabel *DueOnLHS = [[UILabel alloc] initWithFrame:frameL];
        DueOnLHS.font=[UIFont systemFontOfSize:16.0];
        DueOnLHS.text=@"Due On :";
        [cell.contentView addSubview:DueOnLHS];

        AlertNameRHS = [[UILabel alloc] initWithFrame:frameR];
        AlertNameRHS.backgroundColor=[UIColor greenColor];
        AlertNameRHS.textColor=[UIColor redColor];
        AlertNameRHS.textColor=[UIColor redColor];
        AlertNameRHS.font=[UIFont systemFontOfSize:18.0];
        AlertNameRHS.text = @"l1";
        [cell.contentView addSubview:AlertNameRHS];

        frameR.origin.y += 60;
        AlertMonthRHS = [[UILabel alloc] initWithFrame:frameR];
        AlertMonthRHS.font=[UIFont systemFontOfSize:18.0];
        AlertMonthRHS.text =@"l2";
        [cell.contentView addSubview:AlertMonthRHS];

        frameR.origin.y += 60;
        DueOnRHS = [[UILabel alloc] initWithFrame:frameR];
        DueOnRHS.font=[UIFont systemFontOfSize:18.0];
        DueOnRHS.text = @"l3";
        [cell.contentView addSubview:DueOnRHS];
    }

    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

    AlertNameRHS.text = [NSString stringWithFormat:@"%d",indexPath.row];

    return cell;
}

あなたはあまりにも多くの間違いを犯しました:

LHS1は表示されますが、そのテキストをLHS3テキストで上書きします。 AlertNameLHS.text=@"Due On :";

LHS2は、初期化してサブビューとして追加するだけで、代わりにLHS1を構成しているため、表示されません。

セルの高さが180で、y座標が190であるため、LHS3は表示されません。代わりに、LHS1テキストを設定するテキストも設定しません。

RHSラベルのフレームが正しくなく、セルフレーム外です。frameRを使用することになっていますが、frameLを使用しているため、すべてのステップでy座標に60ピクセルを追加することをお勧めします。

于 2012-07-12T10:14:45.737 に答える
1

この種の作業を実行するには、1 つのカスタム セルを作成することをお勧めします。(新しいファイルを追加 -> Objective-C クラス -> UItableViewCell)

プロパティで 6 つのラベルを定義する

#import <UIKit/UIKit.h>

@interface DemoCell : UITableViewCell
{
    IBOutlet UILabel *lblOne;
}

@property (nonatomic,retain) IBOutlet UILabel *lblOne;

新しい空のビュー (nib ファイルのみ) を作成し、Democell という名前を付けます (便宜上) その中で、nib ファイルからビューを削除し、ライブラリから、nib ファイルのビューの場所にテーブル ビュー セルをドラッグ アンド ドロップします。

次に、 tableviewcell を選択し、その ID インスペクターで、そのクラスを DemoCell に変更します。これにより、nib ファイルがカスタム セル クラスにリンクされます。

次に、nib ファイルで 6 ラベルをドラッグ アンド ドロップし、必要に応じて配置します。ファイル オーナーで、すべての lbls を lblOne、lblTwo でリンクします。,,,....

これで、カスタム セルが完成しました。テーブルの cellforrowatindexpath でこのセルを使用するには

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    DemoCell *cell= (DemoCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell ==nil)
    {
        cell=[[[NSBundle mainBundle]loadNibNamed:@"DemoCell" owner:self options:nil] lastObject];
    }


cell.lblone.textlabel.text = @"lblOne";
于 2012-07-12T09:11:04.010 に答える
-1

6 つのラベルを定義し、そのフレームを設定します。次に、SubView を tableViewCell contentView に追加します。

于 2012-07-12T09:14:24.707 に答える