0

私のアプリではUITableView、私の1つにあります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

次のコードを使用できます。

- (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 に答える
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 に答える
0

コードに加えて、次のコードを追加します。

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

cell.selectedBackgroundView = bgView;

詳細については、UITableViewCellのドキュメントを確認してください。selectedBackgroundView

于 2012-12-10T08:14:08.167 に答える