4

ここに画像の説明を入力UITableViewのようなセクションの背景画像を設定できますかUITableViewCell backGroundView。私の調査によると、それは不可能であり、行のようなセクションをカスタマイズする必要があります。
これとは別に、より良い解決策を得ることができます。

前もって感謝します!

4

6 に答える 6

3

誰かがまだ興味を持っている場合は、実際にはかなり簡単な解決策があります。UITableView は、セレクター rectForSection を使用して、各セクションのフレームを通知します。

各セクションに背景画像を追加すると、次のようになります (View Controller のどこかに):

  for(int i = 0;i < [self numberOfSectionsInTableView:self.tableView]; i++) {
        UIImage *img = [[UIImage imageNamed:@"background"] resizableImageWithCapInsets:UIEdgeInsetsMake(30.0, 30.0, 40.0, 40.0) ]; //In this example the image is stretchable
        UIView *borderView = [[UIImageView alloc] initWithImage:img];
        CGRect section_rect = [self.tableView rectForSection:i];
        borderView.frame = section_rect;
        [self.tableView addSubview:borderView];
    }

テーブルビューにデータをリロードすると、これもやり直す必要があることに注意してください。

于 2015-05-14T13:48:00.687 に答える
1

はい、私は次の方法でこれを行っています、私はそれがあなたにもうまくいくと思います、....

        -(void)addBackgroundViewForSectionIndex:(int)tagIndex {

           for (UIView * subview in tableView.subviews) {
             if(subview.tag == tagIndex)
               [subview removeFromSuperview];
           }

          CGRect sectionFrame = [tableView rectForSection:tagIndex];
          UIImageView *newView = [[UIImageView alloc]initWithFrame:sectionFrame];
          newView.tag = tagIndex;
          [newView setImage:[UIImage imageNamed:@"image.PNG"]];
          [tableView addSubview:newView];
          [tableView sendSubviewToBack:newView];

        }

      // Customize the appearance of table view cells.

       - (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:      (NSIndexPath *)indexPath {

           static NSString *CellIdentifier = @"Cell";

         UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:CellIdentifier];
          if (cell == nil) {
                  cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
                           }

                 cell.textLabel.text = [collection objectAtIndex:indexPath.row];


             [self addBackgroundViewForSectionIndex:indexPath.section];

                return cell;
             }

問題があれば教えてください...:)

于 2013-02-13T13:17:13.683 に答える
1

いいえ、UITableView意図したとおりに使用することはできません。他の人が示唆しているように、セクション ヘッダーは、ヘッダーが表示されている間だけ表示されるため、機能しません。上にスクロール中に消えるとすぐに、背景画像が消えます。

あなたの2つのオプションは次のとおりです。

  • UIImageViewインスタンスをサブビューとしてテーブル ビューに直接追加します。手動でサイズを調整して配置する必要があるため、楽しくありません。これはちょっと難しいことです。
  • 装飾ビューをサポートする を使用UICollectionViewします (まさにあなたが探しているものです)。

このUICollectionView方法の方がおそらく優れていますが、正直なところ、どちらのオプションも多くの作業を必要とします。

于 2013-02-13T14:36:24.303 に答える
-1

TableViewのセクションの背景を設定できます。

以下のようにデリゲートメソッドを見てください:

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{
    UIImageView *bgImgView = [[[UIImageView alloc] initWithFrame:CGRectMake(0.0,0.0,tableView.bounds.size.width,40.0)] autorelease];
    bgImgView.image = [UIImage imageNamed:@"yourimage.jpg"];

    return bgImgView;
}

これがお役に立てば幸いです。

乾杯!

于 2013-02-08T12:47:07.957 に答える
-1
table= [[UITableView alloc] initWithFrame:CGRectMake(7,0, 307, 124) style:UITableViewStylePlain];

[table  setSeparatorStyle:UITableViewCellSeparatorStyleNone];

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tablebg_pt12.png"]];

imageView.frame=CGRectMake(0, 0, 307, 124);

imageView.image= [UIImage  imageNamed:@"tablebg_pt12.png" ];


    table.backgroundView = imageView;
    [imageView release];


    table.delegate = self;
    table.dataSource = self;

    // table.backgroundColor=[UIColor clearColor];
    [viewTable addSubview:table];
于 2013-02-13T14:15:52.147 に答える