0

我现在想知道当我单击特定单元格时如何在单元格背景上添加图像并且我的表格被分组我有三个部分和 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 回答