0

イメージビューを含むカスタム テーブルビュー セルで構成されるテーブルビューがあります。でcellForRowAtIndexPath各セルの画像を設定します。セルが選択されたときに画像を変更したいのでdidSelectRowAtIndexPath、セルを取得して画像を変更しても問題ありません。ただし、テーブルをスクロールすると (読み取り: テーブルがセルをリロードします)、新しい画像が表示されなくなります。また、セルが選択されなくなったら、画像を元に戻したいと思います。

で次のことを試しましたcellForRowAtIndexPath

if(cell.isSelected){
cell.imageview.image = [UIImage ImageNamed: @"selected.png"];
}
else
cell.imageview.image = [UIImage ImageNamed: @"not selected.png"];

cell.highlightedまた、BOOL 値を使用しようとしましたが、役に立ちcell.selectedませんでした。

どんな考えでも大歓迎です。

4

2 に答える 2

2

selectedCellIndexPathtypeのクラス変数を使用してみてくださいNSIndexPath。そのdidSelectRow...値を設定し、次のcellForRow...ように記述します。

if([selectedCellIndexPath isEqual:indexPath]){
    cell.imageview.image = [UIImage ImageNamed: @"selected.png"];
} else {
    cell.imageview.image = [UIImage ImageNamed: @"not selected.png"];
}

編集:

または、単に次のように書くこともできます。

if([indexPath isEqual:[tableView indexPathForSelectedCell]]){
    cell.imageview.image = [UIImage ImageNamed: @"selected.png"];
} else {
    cell.imageview.image = [UIImage ImageNamed: @"not selected.png"];
}

しかし、最初の解決策はもう少し効率的です。

于 2013-01-22T18:49:17.807 に答える
0

画像の名前を持つ配列がある場合、配列内の選択されたインデックスで名前を変更し、テーブルをリロードするのが最も簡単です...このように:

myImageArray = [[NSMutableArray alloc] init];
[myImageArray addObject:@"name1.jpg"];
[myImageArray addObject:@"name2.jpg"];
// etc..

そして cellForRowAtIndexPath メソッド内:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}

[cell.imageView setImage:[UIImage imageNamed:[myImageArray objecAtIndex:indexPath.row]]];

}

didSelectRowAtIndexPath で:

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

[myImageArray setObject:@"newName.jpg" atIndexedSubscript:indexPath.row];
[tableView reloadData];

}
于 2013-01-22T19:00:53.480 に答える