2012-10-19 56 views
1

我有一個放置在導航控制器中的表格視圖。表視圖的單元格的寬度小於原始單元格的寬度,而背景是圖像。我怎樣才能設置「選擇的顏色」?這裏是我到目前爲止的代碼:UITableViewCell寬度

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


    static NSString *CellIdentifier = @"ApplicationCell"; 

    ApplicationCell *cell = (ApplicationCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

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



    } 
return cell; 
} 


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) 
    { 
     cellContentView = [[CompositeSubviewBasedApplicationCellContentView alloc] initWithFrame:CGRectInset(self.contentView.bounds, 0.0, 1.0) cell:self]; 
     cellContentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 
     cellContentView.contentMode = UIViewContentModeRedraw; 

     //here i'm making the cells smaller in width than the rest of the tableView 
     CGRect framme = cellContentView.frame; 

     framme.size.width = framme.size.width-58; 
     //set the left space 
     framme.origin.x = framme.origin.x+29; 

     [cellContentView setFrame:framme]; 
     [self.contentView addSubview:cellContentView]; 
    } 
    return self; 
} 

here is my table view unselected

selected

回答

2

TableViewCell■找一個backgroundViewselectedBackgroundView。將這些用於背景並僅將標籤和圖像視圖置於contentView中。

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) 
    { 
     UIView* backgroundView = [[UIView alloc] initWithFrame:self.bounds]; 
     UIView* visibleBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(29.0f, 0.0f, backgroundView.bounds.size.width - 58.0f, backgroundView.bounds.size.height)]; 
     // configure the visibleBackgroundView with the color you want for unselected cell here 
     [backgroundView addSubview:visibleBackgroundView]; 
     self.backgroundView = backgroundView; 

     UIView* selectedBackgroundView = [[UIView alloc] initWithFrame:self.bounds]; 
     UIView* visibleSelectedBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(29.0f, 0.0f, backgroundView.bounds.size.width - 58.0f, backgroundView.bounds.size.height)]; 
     // configure the visibleSelectedBackgroundView with the color you want for selection here 
     [selectedBackgroundView addSubview:visibleSelectedBackgroundView]; 
     self.selectedBackgroundView = selectedBackgroundView; 

     // configure your content view with all the labels you need here 
    } 
    return self; 
} 
+0

沒問題,但選定的框架不是較小的! – Filip

+0

不,您必須設置一個全尺寸的'selectedBackgroundView',然後像在您的示例中那樣,將子視圖添加到具有適當大小的backgroundView。這也是你應該用'backgroundView'屬性來做的。 –

+0

啊哈,讓我試試看,讓你知道!非常感謝您的幫助 – Filip

1

的UITableViewCell是的UIView的子類,所以你可以在上面使用所有UIView類的屬性,如的backgroundColor等 作爲Madboy提到它也backgroundView和selectedBackgroundView也爲它的屬性,所以你可以用論文的屬性玩讓它成爲一個定製的單元格

+0

非常感謝您的幫助! – Filip