如何从 iCarousel 中删除视图并重新加载数据
How can I remove view from iCarousel and reload data
我是 IOS 开发的新手,我已经在我的 ViewController 之一中实现了 iCarousel。我通过字符串数组传递一些数据,当我单击 carouselView 上的按钮时,它应该会打开照片库。一旦我 select 照片库中的图像,我想从数组中删除值并删除 iCarousel 卡片的那个实例。
我环顾四周,只在 obj-c 中找到了代码,但我正在使用 Swift。
我附上了 iCarousel 的图片。当我点击相机按钮时,它会带我到照片库,但是一旦我 select 照片,我想删除轮播卡的那个实例。 ViewControllerImage
这是我目前在 ViewController
中用于 iCarousel 的代码
class ChallengesViewController: UIViewController, TabItem, iCarouselDelegate, iCarouselDataSource {
var tabImage: UIImage?
var image:UIImage? = imageLiteral(resourceName: "camera")
var masterchallengeList = [String]()
var masterSubtitles = [String]()
var weeklySelect = [String]()
var weeklySubtitleSelect = [String]()
enum StorageType {
case userDefaults
case fileSystem
}
@IBOutlet weak var carouselView: iCarousel!
override func awakeFromNib() {
masterchallengeList = [“A “, “B”, “C”, “D”, “E”, F”, “G”]
masterSubtitles = [“A “, “B”, “C”, “D”, “E”, F”, “G”]
weeklySelect = ["","","","","","",""]
weeklySubtitleSelect = ["","","","","","",""]
for n in 0...(weeklySelect.count - 1){
weeklySelect[n] = masterchallengeList[n]
}
for n in 0...(weeklySubtitleSelect.count - 1){
weeklySubtitleSelect[n] = masterSubtitles[n]
}
}
@objc func didTapCameraImage() {
presentPhotoActionSheet()
}
override func viewDidLoad() {
super.viewDidLoad()
carouselView.type = .coverFlow
navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
navigationItem.titleView?.tintColor = UIColor.init(red: 3, green: 25, blue: 82, alpha: 1.0)
navigationController?.navigationBar.prefersLargeTitles = true
navigationItem.title = "Challenges"
}
func numberOfItems(in carousel: iCarousel) -> Int {
return weeklySelect.count
}
func carousel(_ carousel: iCarousel, viewForItemAt index: Int, reusing view: UIView?) -> UIView {
let tempView = UIView(frame: CGRect(x: 0, y: 0, width: 250, height: 350))
let lightBlue = UIColor.init(red: 94, green: 122, blue: 255, alpha: 1.0)
tempView.clipsToBounds = true
tempView.translatesAutoresizingMaskIntoConstraints = true
tempView.layer.cornerRadius = 25.0
let actionLabel = UILabel(frame: CGRect(x: 20, y: 0, width: 200, height: 150))
actionLabel.numberOfLines = 0
actionLabel.font = actionLabel.font.withSize(25.0)
actionLabel.textColor = UIColor.white
actionLabel.text = weeklySelect[index]
tempView.addSubview(actionLabel)
let subtitleLabel = UILabel(frame: CGRect(x: 20, y: -30, width: 200, height: 150))
subtitleLabel.center.y = tempView.center.y
subtitleLabel.numberOfLines = 0
subtitleLabel.font = actionLabel.font.withSize(14)
subtitleLabel.textColor = UIColor.white
subtitleLabel.text = weeklySubtitleSelect[index]
tempView.addSubview(subtitleLabel)
let photoButton = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
photoButton.center.x = tempView.center.x
photoButton.center.y = tempView.center.y + 92.5
photoButton.setImage(image, for: .normal)
photoButton.setImage( imageLiteral(resourceName: "camera"), for: .normal)
let gesture = UITapGestureRecognizer(target: self,
action:#selector(didTapProfilePic))
photoButton.addGestureRecognizer(gesture)
tempView.addSubview(photoButton)
return tempView
}
func carousel(_ carousel: iCarousel, valueFor option: iCarouselOption, withDefault value: CGFloat) -> CGFloat {
if option == iCarouselOption.spacing{
return value * 2
}
return value
}
}
extension ChallengesViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate{
func presentPhotoActionSheet() {
let actionSheet = UIAlertController(title: "Profile Picture", message: "How would you like to select a profile picture", preferredStyle: .actionSheet)
actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
actionSheet.addAction(UIAlertAction(title: "Take Photo", style: .default, handler: { [weak self] _ in
self?.presentCamera()
}))
actionSheet.addAction(UIAlertAction(title: "Choose Photo", style: .default, handler: { [weak self] _ in
self?.presentPhotoPicker()
}))
present(actionSheet, animated: true)
}
func presentCamera() {
let vc = UIImagePickerController()
vc.sourceType = .camera
vc.delegate = self
vc.allowsEditing = true
present(vc, animated: true)
}
func presentPhotoPicker(){
let vc = UIImagePickerController()
vc.sourceType = .photoLibrary
vc.delegate = self
vc.allowsEditing = true
present(vc, animated: true)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
picker.dismiss(animated: true, completion: nil)
guard let selectedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage else{
return}
image = selectedImage
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
override func present(_ viewControllerToPresent: UIViewController,
animated flag: Bool,
completion: (() -> Void)? = nil) {
viewControllerToPresent.modalPresentationStyle = .fullScreen
super.present(viewControllerToPresent, animated: flag, completion: completion)
}
@objc func dismissAlert(){
customAlert.dismissAlert() }
}
如果你只想隐藏它 iCarousel
carouselView.isHidden = true
但是,如果您想将其从 superView
中删除,您必须这样做
carouselView.removeFromSuperView()
此行从 superView
中删除 carouselView
所以在这种情况下它是 self.view
也许您不想在从 superView
中删除后使用 carouselView
。
在这种情况下,您可以选择;
carouselView = nil
所以使用
carouselView.removeFromSuperView()
carouselView = nil
weeklySelect.remove(at: selectedRowIndex)
我是 IOS 开发的新手,我已经在我的 ViewController 之一中实现了 iCarousel。我通过字符串数组传递一些数据,当我单击 carouselView 上的按钮时,它应该会打开照片库。一旦我 select 照片库中的图像,我想从数组中删除值并删除 iCarousel 卡片的那个实例。
我环顾四周,只在 obj-c 中找到了代码,但我正在使用 Swift。
我附上了 iCarousel 的图片。当我点击相机按钮时,它会带我到照片库,但是一旦我 select 照片,我想删除轮播卡的那个实例。 ViewControllerImage
这是我目前在 ViewController
中用于 iCarousel 的代码 class ChallengesViewController: UIViewController, TabItem, iCarouselDelegate, iCarouselDataSource {
var tabImage: UIImage?
var image:UIImage? = imageLiteral(resourceName: "camera")
var masterchallengeList = [String]()
var masterSubtitles = [String]()
var weeklySelect = [String]()
var weeklySubtitleSelect = [String]()
enum StorageType {
case userDefaults
case fileSystem
}
@IBOutlet weak var carouselView: iCarousel!
override func awakeFromNib() {
masterchallengeList = [“A “, “B”, “C”, “D”, “E”, F”, “G”]
masterSubtitles = [“A “, “B”, “C”, “D”, “E”, F”, “G”]
weeklySelect = ["","","","","","",""]
weeklySubtitleSelect = ["","","","","","",""]
for n in 0...(weeklySelect.count - 1){
weeklySelect[n] = masterchallengeList[n]
}
for n in 0...(weeklySubtitleSelect.count - 1){
weeklySubtitleSelect[n] = masterSubtitles[n]
}
}
@objc func didTapCameraImage() {
presentPhotoActionSheet()
}
override func viewDidLoad() {
super.viewDidLoad()
carouselView.type = .coverFlow
navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
navigationItem.titleView?.tintColor = UIColor.init(red: 3, green: 25, blue: 82, alpha: 1.0)
navigationController?.navigationBar.prefersLargeTitles = true
navigationItem.title = "Challenges"
}
func numberOfItems(in carousel: iCarousel) -> Int {
return weeklySelect.count
}
func carousel(_ carousel: iCarousel, viewForItemAt index: Int, reusing view: UIView?) -> UIView {
let tempView = UIView(frame: CGRect(x: 0, y: 0, width: 250, height: 350))
let lightBlue = UIColor.init(red: 94, green: 122, blue: 255, alpha: 1.0)
tempView.clipsToBounds = true
tempView.translatesAutoresizingMaskIntoConstraints = true
tempView.layer.cornerRadius = 25.0
let actionLabel = UILabel(frame: CGRect(x: 20, y: 0, width: 200, height: 150))
actionLabel.numberOfLines = 0
actionLabel.font = actionLabel.font.withSize(25.0)
actionLabel.textColor = UIColor.white
actionLabel.text = weeklySelect[index]
tempView.addSubview(actionLabel)
let subtitleLabel = UILabel(frame: CGRect(x: 20, y: -30, width: 200, height: 150))
subtitleLabel.center.y = tempView.center.y
subtitleLabel.numberOfLines = 0
subtitleLabel.font = actionLabel.font.withSize(14)
subtitleLabel.textColor = UIColor.white
subtitleLabel.text = weeklySubtitleSelect[index]
tempView.addSubview(subtitleLabel)
let photoButton = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
photoButton.center.x = tempView.center.x
photoButton.center.y = tempView.center.y + 92.5
photoButton.setImage(image, for: .normal)
photoButton.setImage( imageLiteral(resourceName: "camera"), for: .normal)
let gesture = UITapGestureRecognizer(target: self,
action:#selector(didTapProfilePic))
photoButton.addGestureRecognizer(gesture)
tempView.addSubview(photoButton)
return tempView
}
func carousel(_ carousel: iCarousel, valueFor option: iCarouselOption, withDefault value: CGFloat) -> CGFloat {
if option == iCarouselOption.spacing{
return value * 2
}
return value
}
}
extension ChallengesViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate{
func presentPhotoActionSheet() {
let actionSheet = UIAlertController(title: "Profile Picture", message: "How would you like to select a profile picture", preferredStyle: .actionSheet)
actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
actionSheet.addAction(UIAlertAction(title: "Take Photo", style: .default, handler: { [weak self] _ in
self?.presentCamera()
}))
actionSheet.addAction(UIAlertAction(title: "Choose Photo", style: .default, handler: { [weak self] _ in
self?.presentPhotoPicker()
}))
present(actionSheet, animated: true)
}
func presentCamera() {
let vc = UIImagePickerController()
vc.sourceType = .camera
vc.delegate = self
vc.allowsEditing = true
present(vc, animated: true)
}
func presentPhotoPicker(){
let vc = UIImagePickerController()
vc.sourceType = .photoLibrary
vc.delegate = self
vc.allowsEditing = true
present(vc, animated: true)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
picker.dismiss(animated: true, completion: nil)
guard let selectedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage else{
return}
image = selectedImage
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
override func present(_ viewControllerToPresent: UIViewController,
animated flag: Bool,
completion: (() -> Void)? = nil) {
viewControllerToPresent.modalPresentationStyle = .fullScreen
super.present(viewControllerToPresent, animated: flag, completion: completion)
}
@objc func dismissAlert(){
customAlert.dismissAlert() }
}
如果你只想隐藏它 iCarousel
carouselView.isHidden = true
但是,如果您想将其从 superView
中删除,您必须这样做
carouselView.removeFromSuperView()
此行从 superView
中删除 carouselView
所以在这种情况下它是 self.view
也许您不想在从 superView
中删除后使用 carouselView
。
在这种情况下,您可以选择;
carouselView = nil
所以使用
carouselView.removeFromSuperView()
carouselView = nil
weeklySelect.remove(at: selectedRowIndex)