2013-02-08 142 views
4

enter image description here我們可以設置背景圖片爲UITableView部分如UITableViewCellbackGroundView。根據我的研究,它不可能&我們需要自定義行的部分。
甘蔗我們能夠得到更好的解決方案,除此之外。我們可以在UITableview中爲Section設置backgroundImage嗎?

在此先感謝!

+0

這有幫助嗎? http://stackoverflow.com/questions/10857241/set-backgroundimage-to-uitableview-section-viewforheaderinsection – Rahul 2013-02-08 12:43:48

+0

我需要整個部分,而不是部分的headerview。 – Madhu 2013-02-09 08:15:25

+1

@Madhu你是否嘗試過這樣?裁剪你的單個圖像爲幾個,並分別添加爲部分標題背景和每個單元格背景。我認爲它會看起來像你需要的.. – 2013-02-19 11:57:20

回答

-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; 
} 

希望這對您有所幫助。

乾杯!

+2

它的作品爲Section Header視圖,但我需要整個部分。 – Madhu 2013-02-09 08:13:22

+0

我不明白你的意思。你的意思是,只有一個標題或其他東西。請詳細說明這一點。所以,我可以更好地幫助你。截圖將不勝感激。 – 2013-02-11 05:21:19

+0

@Rushi:y座標(0.0)表示,它將從標題開始開始。意味着它需要自己協調標題。 – 2013-02-11 06:07:32

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; 
      } 

讓我知道你的任何問題,... :)

-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]; 
+0

你可以創建表格手動添加圖像在表背景 – SanjayPatel 2013-02-13 14:16:44

+0

請不要發表部分標題視圖和背景圖像邏輯her.e我知道BackgroundImage和節標題視圖。 – Madhu 2013-02-14 12:10:04

1

不,這不可能像使用UITableView一樣。正如其他人所建議的章節標題將不起作用,因爲它們只在標題可見時纔可見。只要滾動時消失,背景圖像就會消失。

你有兩個選項:

  • 添加UIImageView情況下直接作爲子視圖表視圖。不好玩,因爲你必須手動調整大小和位置,這很難做到。
  • 使用UICollectionView,它支持裝飾視圖(正是你要找的)。

UICollectionView方法可能會更好,但說實話,任一選項都需要大量的工作。

2

如果任何人仍然被捕,實際上有一個相當直接的前瞻性解決方案。 UITableView的會告訴你每使用選擇rectForSection你部分的框架:

添加化背景圖像的每個部分,然後會是這個樣子(某處在視圖控制器):

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]; 
    } 

只要記住如果你重新加載tableview中的數據,你也必須重做這個。

+0

已經發展了這麼多年,但不知道那種方法曾經存在:) – Danchoys 2016-04-01 13:21:04

相關問題