0

特定のセルをクリックしたときにセルの背景に画像を追加する方法と、テーブルがグループ化されている3つのセクションと5つの行がありますこのコードを試してみましたが、適切に機能しません両方の方法で両方にこのコードを書きました

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

}
4

2 に答える 2

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

それはあなたに感謝するかもしれません:)

于 2012-05-24T06:18:28.510 に答える
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の目的-c

于 2012-05-24T06:31:56.070 に答える