0

在我的应用程序中,我有UITableView一个。我UIView为所有单元格添加了背景图像。我的问题是,当我选择一个特定的单元格时,我想单独更改该特定单元格的背景图像,而所有其他单元格都应该有一个旧的背景图像。我怎样才能做到这一点。请分享你的想法。

这是我的cellForRowAtIndexPath代码

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

    // Configure the cell.
    UIImageView *bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"MenuItem3"]];

    cell.backgroundView = bgView;



    cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:14];



    cell.textLabel.textColor = [UIColor whiteColor];
    cell.textLabel.text = [arrayValue objectAtIndex:indexPath.row];
    [cell setAccessoryType:UITableViewCellAccessoryNone];
    cell.selectionStyle =  UITableViewCellSelectionStyleNone;
    return cell;
}
4

4 回答 4

1

无需重新加载所有表格,只需像这样重新加载 PreviousInxed

- (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];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }


    cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bck.png"]];

    UITableViewCell *celld = (UITableViewCell *)[tableView cellForRowAtIndexPath:table.indexPathForSelectedRow];
    celld.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"raj_star.png"]];

    return cell;
}

NSIndexPath *perviouseIndexPathaf;

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

    if (perviouseIndexPathaf)
    {
        [table reloadRowsAtIndexPaths:[NSArray arrayWithObject:perviouseIndexPathaf]
                     withRowAnimation:UITableViewRowAnimationNone];
    }
    UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
    cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"raj_star.png"]];
    perviouseIndexPathaf = indexPath;

}
于 2012-12-10T08:46:35.627 回答
1

您可以使用以下代码。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (tableView.tag == 3)// We are checking if it's the desire tableview. If you have only one tableview then you can remove this if.
        {
            [tableView reloadData];
            UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
            cell.backgroundView = bgView; // Here set the particular image u want.
        }
    }
于 2012-12-10T07:43:09.983 回答
1

试试这个代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *objCustomCell = (UITableViewCell *)[tableView  cellForRowAtIndexPath:indexPath];
    UIImageView *bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"NewImage"]];
    objCustomCell.backgroundView = bgView;
}
于 2012-12-10T07:47:53.013 回答
0

除了您的代码,添加以下代码。

UIImageView *bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"SelectedMenuItem3"]];

cell.selectedBackgroundView = bgView;

有关更多信息,请查看UITableViewCell的文档selectedBackgroundView

于 2012-12-10T08:14:08.167 回答