0
UITableView
に5つのセルを持つアプリを作成したいとします。私は、テーブルビューのメインページに背景画像を割り当てました。ただし、セルをタップした後に表示される各ビューに異なる背景画像を割り当てたいと思います。どのように達成するのですか?BackViewイメージを各DetailViewControllerに個別に追加するにはどうすればよいですか?
ありがとうございます!
UITableView
に5つのセルを持つアプリを作成したいとします。私は、テーブルビューのメインページに背景画像を割り当てました。ただし、セルをタップした後に表示される各ビューに異なる背景画像を割り当てたいと思います。どのように達成するのですか?BackViewイメージを各DetailViewControllerに個別に追加するにはどうすればよいですか?
ありがとうございます!
背景として設定する5つの異なる画像を含む配列を作成できます。次に、UITableViewDelegateのdidSelect関数を使用して、選択したインデックスを取得できます。このインデックスを使用すると、イメージを取得して背景として設定できます。
// do not forget to set delegate for tableview inside your viewcontroller
tableView.delegate = self
スウィフト
let images = [image1, image3, image3, image4, image5]
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedIndex = indexPath.row
let backgroundImage = images[selectedIndex]
YOUR_IMAGE_VIEW.image = backgroundImage
}
のObjective-C
NSArray *images = [NSArray arrayWithObjects:image1, image2, image3, image4, image5, nil];
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
int selectedIndex = indexPath.row;
UIImage *backgroundImage = images[selectedIndex];
YOUR_IMAGE_VIEW.image = backgroundImage;
}
は見事に働いた、ありがとう! –
@KeremYilmazより答えとして受け入れることができます – abdullahselek