6

私はiPhone開発の新人です。

私の質問に関連する回答があるかもしれませんが、何も助けてくれませんでした。

タイトルによると、スタイルがグループ化された UITableView があります。背景画像を設定したいのですがうまく設定できません。

しかし、問題は、私のスクリーンショットに記載されていることです。

ここに画像の説明を入力

さてcellsUITableViewグループ化された)で説明する画像ビューのみの領域を表示したい

これが私のコードです:

self.tblView = [[UITableView alloc]initWithFrame:CGRectMake(0, 125, 320, 320) style:UITableViewStyleGrouped];

    UIImageView *bgTableImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0,0, 320, 320)];
    bgTableImageView.image = [UIImage imageNamed:@"bgImage.png"];

    self.tblView.backgroundColor=[UIColor clearColor];
    [self.tblView setBackgroundView:bgTableImageView];

    //self.tblView.backgroundView = nil;
   // self.tblView.opaque = NO;
    //self.tblView.bounces = NO;
    //self.tblView.scrollEnabled = YES;
    self.tblView.delegate=self;
    self.tblView.dataSource=self;
    self.tblView.separatorColor = [UIColor darkGrayColor];
    self.tblView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [self.view addSubview:self.tblView];

他のすべては正常に機能し、適切に機能しました。背景画像を設定する方法の問題のみがありUITableViewStyleGroupedますか?

4

7 に答える 7

4

これを試して:

UIImageView *av = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 277, 58)];
av.backgroundColor = [UIColor clearColor];
av.opaque = NO;
av.image = [UIImage imageNamed:@"categorytab1.png"];
cell.backgroundView = av;
于 2013-04-10T10:23:03.513 に答える
3

次のコードを使用しないでくださいUIImageView

self.tblView.backgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"YourImage.png"]];

それ以外は、

self.tblView.backgroundView.inputView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"tblbg.png"]];

2 番目のオプション(このオプションはStyleに依存UITableViewない場合に役立ちます)

1) あなたのUITableViewスタイルを 変えてくださいstyle:UITableViewStylePlain。2)フレームワーク
を追加する 3)などの フレームを変更する 4) 丸みを帯びた半径を与える#import <QuartzCore/QuartzCore.h>
UITableViewCGRectMake(10, "asYouNeed", 300, "asYouNeed")
UITableView

self.tblView.layer.cornerRadius = 10; // set Radius as you need

そしてのように設定UIImageViewし ます。(この質問に入力したコードに従ってくださいBackGroundViewUITableView

この上記の手順は、角を丸くして作成さUITableViewます。ここで、このコードはスタイルに依存していない場合に使用されることを言及します。それが重要な場合、このオプションは役に立ちません。UITableViewTableView Style:Gropped

ありがとう :)

于 2013-04-10T09:51:03.667 に答える
3

カスタムセルUIを作成したい場合は、試してみてください

 _tblNews.backgroundColor = [UIColor clearColor];
        _tblNews.backgroundView = nil;
        _tblNews.separatorColor = [UIColor clearColor];
        _tblNews.separatorStyle = UITableViewCellSeparatorStyleNone;

そして、セルを作成し、そのセルをデータソースメソッドにロードしたいので、セルのペン先を作成します。

負荷xib用

+ (id)loadNibNamed:(NSString *)NibName {
    NSObject *cl = nil;
    if (NSClassFromString(NibName) != nil) {
        NSArray *arr = [[NSBundle mainBundle] loadNibNamed:NibName owner:self options:nil];
        for (id cls in arr) {
            if([cls isKindOfClass:NSClassFromString(NibName)])
            {
                cl = cls;
                break;
            }
        }
    }
    return cl;
}

xib で、セルを再利用できるように識別子を設定していることを確認してください。それを使用すると、セルのレイアウトを簡単にカスタマイズできます。

于 2013-04-19T06:03:07.207 に答える
3

まず、テーブルがどのように機能するかについて少し説明します。

  1. UITableView背景があります。これが細胞の後ろに見えるものです。
  2. すべてのセルには、 、 、 の 3 つの特別なビューがcontentViewありbackgroundViewますselectedBackgroundView
  3. backgroundViewセルが選択されている場合、セルの内容の下に表示される が代わりselectedBackgroundViewに使用されます。
  4. 通常、すべてのコンテンツはcontentsView. コンテンツから背景を分離することで、テーブルは選択されていない/選択されているトランジションを適切にアニメーション化できます。また、セルが編集モードに入るときも重要です。コンテンツは縮小/移動され、編集コントロール (削除ボタンやセル選択など) はコンテンツ上で独立して表示できます。
  5. セルには、区切りビューも直接含まれています (通常、セルの下部に 1 または 2 ピクセルの高さのビューがありますseparatorStyle)。そのため、セルは常にそのセルよりも少なくとも 1 ピクセル高くなりますcontentsView

次に、グループ化されたテーブルがどのように機能するかについて少し説明します。

  1. テーブルには特別なデフォルトの背景があります。backgroundViewと を使用して削除/変更できますbackgroundColor
  2. グループ化されたテーブル ビューのセルは小さくなりcontentViewます。セルの幅はテーブル全体と同じですが、contentView左右にオフセットがあります (iPhone では約 10 ポイント)。
  3. backgroundViewが変更され、セルの位置に応じて境界線を描画するレイヤーが含まれます (最初、最後、および中間のセルで異なります) 。ボーダーは、テーブルの で指定された色を持ちますseparatorColor

コード内ですべてを変更できます。

境界線を削除する最も簡単な方法の 1 つは、に設定separatorColorすること[UIColor clearColor]です。これにより、セルセパレーターが削除されますが、独自のセパレーターを追加できます。

cell = ...
UIView* separator = [[UIView alloc] init];
separator.backgroundColor = ...
separator.frame = CGRectMake(0.0f, table.bounds.size.width, table.rowHeight - 1.0f, 1.0f);
separator.autoresizingMask = (UIViewAutoresizingMaskFlexibleTopMargin | UIViewAutoresizingMaskFlexibleWidth);
[cell addSubview:separator]; //note we are adding it directly to the cell, not to contentsView

UIImageView単色ビューの代わりに、セパレーターにイメージ ( ) を使用することもできます。

別の方法は、各セルにbackgroundViewtoを設定することです。nil

実際には、を完全に無視してcontentsView、すべてをセルに直接追加できます。その後、次のセル メソッドをカスタマイズして、状態が変化したときにセルを更新できます。

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    //update the cell text colors, backgrounds etc for the selected/not selected state
    //you don't have to call super
    // (the default implementation changes between backgroundView and selectedBackgroundView)
}

- (void)setHighlighted:(BOOL)highlited animated:(BOOL)animated {
    //update the cell text colors, backgrounds etc for the selected/not highlighted state
    //you don't have to call super
    // (the default implementation changes between backgroundView and selectedBackgroundView)
    //by giving empty implementation, you will block highlighting of cells
}

- (void)setEditing:(BOOL)highlited animated:(BOOL)animated {
  //you can use this method to add custom editing controls
}

- (void)willTransitionToState:(UITableViewCellStateMask)state {
  //use this method to change the layout of your contents
  //when the cells goes into one of the editing states
}

- (void)layoutSubviews {
  //update the frames of cell contents to match the new cell size
  //note that the cell doesn't have the correct size until it is added to the table,
  //this method is called when the cell already has the final size

  //you can also use this method to change the size/position of the `contentsView`
}

編集: 特定の問題に対処するには、おそらく最良の解決策は次のとおりです。

  1. 表からデフォルトを削除し、backgroundView独自の背景を追加します (例: クリア カラー、ホワイト カラー、パターンから作成されたカラー、UIImageViewまたはbackgroundView.
  2. backgroundViewすべてのセルからデフォルトを削除し、 UIImageView. 最初、最後、および中間のセルには、3 つの特別な画像が必要です。
于 2013-04-16T11:45:43.837 に答える
2

UITableView背景ビューをnilに設定して透明にします。

[tableView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"backgroundImage.png"]]];
tableView.opaque = NO;
tableView.backgroundView = nil;

それがあなたを助けることを願っています。

于 2013-04-16T10:29:43.890 に答える
2

こうなりたいか

ここに画像の説明を入力

ここにコードがあります:-

UITableView の背景ビューを任意の場所に設定します。viewDidload で実行しました。

[_tblView setBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"url.jpg"]]];

そして、cellForRowAtIndexPathでtableViewCellBackgroundの色を透明に設定します

    static NSString *cellIdentifier=@"cellIdentifier";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (cell ==nil) {
    cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.backgroundColor=[UIColor clearColor];
cell.textLabel.text=@"goup styled";
return cell;
于 2013-04-16T10:48:20.467 に答える