2012-05-19 47 views
1

在我的項目我有靜態細胞tableViews以及動態細胞tableViews。爲了定製,我設法在單元格上獲得了漸變背景(分組模式)。靜態的UITableViewCell backgroundView

它工作正常動態TableViews爲我設置cellForRowAtIndex背景視圖...根據該行的位置(頂部,底部,中部或單)。

然而,當我嘗試實現它的靜態的tableview細胞,這是行不通的。我試圖實現cellForRowAtindex ...但它崩潰。

有人有想法嗎?

更新:對於cellForRow代碼..

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  

    UACellBackgroundView *bgw=[[UACellBackgroundView alloc]init]; 

    if (indexPath.row==0) { 

     bgw.position = UACellBackgroundViewPositionTop; 
     cell.backgroundView=bgw; 

    }else if (indexPath.row==2){ 

     bgw.position = UACellBackgroundViewPositionBottom; 
     cell.backgroundView=bgw; 

    }else { 
     bgw.position = UACellBackgroundViewPositionMiddle; 
     cell.backgroundView=bgw; 
    } 

    // cell.backgroundView=bgw; 


    return cell; 
} 

順便說一句,背景圖我是從這裏:http://code.coneybeare.net/how-to-make-custom-drawn-gradient-backgrounds這裏:http://pessoal.org/blog/2009/02/25/customizing-the-background-border-colors-of-a-uitableview/

如果有人有興趣

+0

崩潰什麼?種類的日誌或斷點? – CodaFi

+0

原因:「*** - [__ NSArrayI objectAtIndex:]:索引0超越界限空陣」 我覺得編譯器要我來實現數據源等。 – Marcal

+0

錯誤的代碼。查找方法-objectAtIndex。 – CodaFi

回答

1

它看起來不像你在分配UITablViewCell,你需要分配單元格。

例如:如果你有一個UITableViewController子類有一個靜態表你不應該試圖離隊細胞

if (cell == nil) { 
    // alloc the UITableViewCell 
    // remeber if you are not using ARC you need to autorelease this cell 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 
+1

謝謝。這實際上起作用,但它破壞了單元格的靜態內容。我所做的是爲每個靜態單元創建一個插座,並在viewDidLoad上添加背景視圖。只要單元格的樣式設置爲「自定義」,它就會工作。 – Marcal

+0

@Marcal我不明白爲什麼你不會爲cellForRowAtIndexPath添加背景到單元格中,該方法的全部目的是爲表設置單元格。只需根據它是哪個單元添加不同的背景。第一個將具有頂部背景,最後一個將具有底部背景,並且每個其他單元將具有中間背景。 – Vikings

+1

那麼,我已經做到了,它的工作原理。但正如我所說,如果我在cellForRow上設置了這個,我已經將其拖入Storyboard單元格中的靜態內容消失了,因爲代碼否決了故事板。 – Marcal

1

- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     // alloc the UITableViewCell 
     // remeber if you are not using ARC you need to autorelease this cell 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    cell.textLabel.text = @"Cell Name"; 
    cell.detailTextLabel.text = @"Cell Detail"; 

    return cell; 
} 

添加這種說法。
相反,你應該問super的細胞。超類將從故事板中獲取單元格,並且可以對其進行配置。

事情是這樣的:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath]; 

    UIView *selectedBackgroundView = [[UIView alloc] init]; 
    cell.selectedBackgroundView = selectedBackgroundView; 
    cell.selectedBackgroundView.backgroundColor = [UIColor mb_tableViewSelectionColor]; 
    return cell; 
} 

適用於所有其他屬性爲好。

相關問題