2012-08-14 64 views
1

我想分配一個自定義backgroundView到UITableViewCell。表格是分組的。有沒有辦法自定義UITableViewCell的backgroundView的大小?

我是一個開拓者的一點,所以,而不是使用背景視圖,302像素的典型寬度,我試圖使用300像素寬圖像。

這裏是我的代碼:

- (void)tableView:(UITableView*)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 
    // a 300 pixel wide image 
    UIImage *backgroundImage = [UIImage imageNamed:@"tableViewCellBackgroundTop"]; 
    UIImageView *theBackgroundView = [[UIImageView alloc] initWithImage:backgroundImage]; 
    theBackgroundView.frame = CGRectMake(10, 0, 300, 49); 
    cell.backgroundView = theBackgroundView; 
} 

如果我運行這在模擬器與xScope測量,細胞似乎仍然是302個像素寬。是什麼賦予了?是否有一些其他屬性需要設置,以使框架符合我分配給背景視圖的框架,還是不希望以此方式定製表格視圖單元格?

+1

對不起。我覺得我很累。 – 2012-08-14 18:06:23

+0

不要'UITableViewCell'只是繼承'UITableView'的寬度? – James 2012-08-14 19:13:45

+0

我相信如此,但這是我關心的backgroundView的寬度。我也不想改變表視圖​​的寬度,因爲它有自己的背景視圖。 – bpapa 2012-08-14 19:19:05

回答

3

您可以創建一個UIView實例,使theBackgroundView它的子視圖,然後設置cell.backgroundView到創建的視圖:

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(10, 0, 300, 49)]; 
[view addSubview:theBackgroundView]; 
cell.backgroundView = view; 
+0

完美的作品 – 2015-05-14 21:56:12

相關問題