0

選択時にセルの上に画像を表示したい。私は成功せずにそうするための無限の方法を試しました. これをに追加しようとしましたdidSelectRowAtIndexPath

UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
cell.imageView.image = [UIImage imageNamed:@"deselected_image.png"];
cell.imageView.highlightedImage = [UIImage imageNamed:@"selected_image.png"];

ただし、プッシュしても画像は変わりません。

ありがとう

4

3 に答える 3

0

リクエストに応じて各セルにUIColorを適用しました。UIColorコードを置き換えて画像を適用できます。これが私のコードです

- (void)tableView: (UITableView*)tableView willDisplayCell: (UITableViewCell*)cell forRowAtIndexPath: (NSIndexPath*)indexPath
{
    if(tableView==tblLanguage)
    {
           cell.backgroundView = [[UIView alloc] init];

          ((UIView *)cell.backgroundView).backgroundColor = !(indexPath.row % 2)?[UIColor colorWithRed:(float)231/255 green:(float)231/255 blue:(float)231/255 alpha:1.0]:[UIColor colorWithRed:(float)218/255 green:(float)218/255 blue:(float)218/255 alpha:1.0];
          ((UIView *)cell.backgroundView).alpha=1.0;
    //ilangSelectedIndex is set in the didselect row of the tableview delegate function
     if(ilangSelectedIndex==[indexPath row])
      {
            ((UIView *)cell.backgroundView).backgroundColor=[UIColor colorWithRed:(float)171/255 green:(float)177/255 blue:(float)213/255 alpha:1.0];       
       }
            cell.textLabel.backgroundColor = [UIColor clearColor];
            cell.detailTextLabel.backgroundColor = [UIColor clearColor];
     }


}   

テーブルのDidselect関数で、 ilangSelectedIndexをテーブルのIndexpath.rowとして設定し、テーブルをリロードして効果を確認します。

于 2013-01-24T05:40:38.303 に答える
0

didSelectRowAtIndexPath で次のコードを試してください。

 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.selectedBackgroundView=[[UIImageView alloc]init];
cell.backgroundView =[[UIImageView alloc] init];
((UIImageView *)cell.selectedBackgroundView).image=[UIImage imageNamed:@"selected.png"];
于 2013-01-24T05:43:54.507 に答える
0

あなたが持っているコードは、新しいテーブルビューセルを作成しています.選択された現在のセルを取得する必要があります. 代わりに次のことを試してください。

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

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.imageView.image = [UIImage imageNamed:@"deselected_image.png"];
    cell.imageView.highlightedImage = [UIImage imageNamed:@"selected_image.png"];

}

于 2013-01-23T21:52:31.167 に答える