2012-05-24 88 views
0

我想現在如何添加圖像的單元格背景當我點擊特定單元 我的表被分組我有三個部分和5行 我試試這個代碼,但它不是爲我工作的正確 我在這兩種方法writen兩個驗證碼如何更改單元格背景圖像在iphone

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

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

UIImage *rowBackground; 
    UIImage *selectionBackground; 
    rowBackground = [UIImage imageNamed:@"topAndBottomRow.png"]; 
    selectionBackground = [UIImage imageNamed:@"topRowSelected.png"];   
    cell.backgroundView = [[[UIImageView alloc] init] autorelease]; 
    cell.selectedBackgroundView = [[[UIImageView alloc] init] autorelease];  
    ((UIImageView *)cell.backgroundView).image = rowBackground; 
    ((UIImageView *)cell.selectedBackgroundView).image = selectionBackground; 


    return cell; 

} 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 

    UIImage *rowBackground; 
    UIImage *selectionBackground; 
    rowBackground = [UIImage imageNamed:@"topAndBottomRow.png"]; 
    selectionBackground = [UIImage imageNamed:@"topRowSelected.png"];   
    cell.backgroundView = [[[UIImageView alloc] init] autorelease]; 
    cell.selectedBackgroundView = [[[UIImageView alloc] init] autorelease];  
    ((UIImageView *)cell.backgroundView).image = rowBackground; 
    ((UIImageView *)cell.selectedBackgroundView).image = selectionBackground; 

} 
+0

我有同樣的問題,如果你得到了回答份額所有。 – Ayaz

回答

1

嘗試: -

- (void)tableView: (UITableView*)tableView willDisplayCell: (UITableViewCell*)cell1 forRowAtIndexPath: (NSIndexPath*)indexPath 
{ 
cell1.backgroundColor = indexPath.row % 2 ? [UIColor colorWithPatternImage:[UIImage imageNamed:(@"back2.png")]]: [UIColor colorWithPatternImage:[UIImage imageNamed:(@"back1.png")]]; 
UIView *myBackView = [[UIView alloc] initWithFrame:cell1.frame]; 
myBackView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:(@"back3.png")]]; 
cell1.selectedBackgroundView = myBackView; 
} 

它可以幫助你謝謝:)

+0

嘿,我嘗試你的代碼,但圖像顯示不正確我的問題是我摸索tableviewcell如何工作 – Rocky

+0

但這裏工作正常,我也使用分組一。 –

0

做這樣

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

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

    UIImage *rowBackground; 
    UIImage *selectionBackground; 
    rowBackground = [UIImage imageNamed:@"topAndBottomRow.png"]; 
    selectionBackground = [UIImage imageNamed:@"topRowSelected.png"];   
    backgroundImageView = [[[UIImageView alloc] init] autorelease]; 
    backgroundImageView.image = rowBackground ; 
    selectedBackgroundImageView = [[[UIImageView alloc] init] autorelease]; 
    selectedBackgroundImageView.image =selectionBackground ;  
    cell.backgroundView = backgroundImageView; 
    cell.selectedBackgroundView = selectedBackgroundImageView; 
    return cell; 

} 

無需編寫在didSelect我猜想的一樣..

iphone的Objective-C

相關問題