2012-05-24 2 views
0

나는 특정 셀을 클릭하면 셀 배경에 이미지를 추가하는 방법을 지금 알고 싶습니다. 테이블이 그룹화되어 있으며 세 섹션과 5 행을 가지고 있습니다. 하지만 적절한 으로 나를 위해 작동하지 않는 코드 내가 두 방법 모두에서이 코드를 writeniphone의 그룹 tableview에서 선택된 셀 배경 이미지를 변경하는 방법

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

안녕하세요 당신의 코드를 시도하지만 이미지가 제대로 표시되지 않습니다 내 문제는 내가 그것을 – Rocky

+0

에서 작동하는 방법을 tableviewcell groped하지만 여기 잘하고, 나는 또한 그룹화 된 하나를 사용하고 있습니다 !!! –

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에서 동일한을 쓸 필요를 수행하지 ..

아이폰의 목표 - C

관련 문제