0
我想在UITableView
中構建一個包含5個單元格的應用程序。我爲表格視圖的主頁面分配了一個背景圖像;但是,我想爲點擊單元格後出現的每個視圖分配不同的背景圖像。我如何實現這一目標?如何分別向每個DetailViewControllers添加背景圖像?
謝謝!
我想在UITableView
中構建一個包含5個單元格的應用程序。我爲表格視圖的主頁面分配了一個背景圖像;但是,我想爲點擊單元格後出現的每個視圖分配不同的背景圖像。我如何實現這一目標?如何分別向每個DetailViewControllers添加背景圖像?
謝謝!
您可以創建一個數組,其中包含要設置爲背景的五個不同圖像。然後,您可以使用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