Rocky
Rocky

Reputation: 1423

how to change cell background image on selected in group tableview in iphone

i want to now how to add image on cell background when i click on particular cell And my table is grouped i have three section and 5 row i try this code but it not working for me as proper i writen this code on both in both method

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

}

Upvotes: 0

Views: 2883

Answers (2)

EXC_BAD_ACCESS
EXC_BAD_ACCESS

Reputation: 2707

Do like this

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

}

No need to write the same in didSelect I Guess..

iphone objective-c

Upvotes: 0

Nikhil Bansal
Nikhil Bansal

Reputation: 1545

Try it:-

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

It may help you thanks :)

Upvotes: 1

Related Questions