如何将侧边栏中 table 视图单元格的第一行设置为不同于所有其他行 - swift

How to set first row of table view cell in sidebar different from all other rows - swift

我创建了侧边菜单,如 this 所示。我只希望第一行带有全名标签的图像视图,这样如果有人点击第一行的图像视图,允许用户从图库中选择图像。此外,所有其他行应该只包含重定向到另一个视图控制器的文本。请建议任何 link 或任何一段代码仅在侧面菜单的第一行获取图像视图,并带有一个按钮以从图库中选择图像。这是我的代码

var arrdata = ["Full name", "Home", "Favourite", "Logout"]
var arrimg = [#imageLiteral(resourceName: "hello.png"), #imageLiteral(resourceName: "hello.png"), #imageLiteral(resourceName: "hello.png"), #imageLiteral(resourceName: "hello.png")]

    @IBOutlet var sidebar: UITableView!
    @IBOutlet var sideview: UIView!

        var isSideViewOpen: Bool = false

  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return arrdata.count
    }

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:TableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TableViewCell
        cell.img.image = arrimg[indexPath.row]
        cell.lbl.text = arrdata[indexPath.row]
        return cell
    }

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.row == 1
        {
            let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
            let HomeViewController = storyBoard.instantiateViewController(withIdentifier: "Home") as! HomeViewController
            self.present(HomeViewController, animated:true, completion:nil)
        }

        if indexPath.row == 3
        {
            let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
            let ViewController = storyBoard.instantiateViewController(withIdentifier: "Login") as! ViewController
            self.present(ViewController, animated:true, completion:nil)
        }
    }
   override func viewDidLoad() {
        super.viewDidLoad()
        sideview.isHidden = true
        sidebar.backgroundColor = UIColor.groupTableViewBackground
        isSideViewOpen = false
    }

首先,您需要包含图像和文本的自定义单元格。

Guide for Custom Cells

之后我会建议有一个配置列表,例如

let shouldShowImage = [true, false, false, false, ...]

在您的函数 cellForRowAt 中,您应该检查您的单元格是否必须显示像

这样的图像
if shouldShowImage[indexPath.row] {

    cell.img.image = arrimg[indexPath.row]
} else {

    cell.img.isHidden = true
}

cell.lbl.text = arrdata[indexPath.row]

嘿,我更新了答案,请检查。 好吧,如果你想要两个不同的视图,你将需要创建 2 个不同的 UITableViewCells。

一个单元格将包含您的 imageView 和标签,另一个单元格将只包含一个按钮。

使用

注册两个单元格的笔尖

tableView.register(UINib(nibName: "your_nib_name", bundle: nil), forCellReuseIdentifier: "your_cell_identifier") 方法。

现在只需在 cellForRowAt 方法中遵循此方法

if indexPath.row == 0 {
    let cell = tableView.dequeueReusableCell(withIdentifier: "your_image_label_cell", for: indexPath) as! your_image_label_cell_className

    //your code here 
    return cell
} else {
    let cell = tableView.dequeueReusableCell(withIdentifier: "your_button_cell_identifier", for: indexPath) as! your_button_cell_className

   //your code here  
   return cell
}

我所做的是检查如果它是第一行 indexpath.row == 0 它将 return 带有图像单元格的标签,否则对于所有其他行它将 return 一个带有图像单元格的单元格一个按钮,您可以在该按钮上添加用于导航到不同屏幕的点击操作事件。

同样,如果你想在第一行中使用不同的点击,你在 didSelectRowAt 方法中使用 indexPath.row == 0 就完成了。希望这对您有所帮助。

隐私 - 媒体库使用说明
隐私 - 相机使用说明

1) 首先将这两个密钥添加到您的 Info.plist 文件中...
2)然后在 UI.. 中的图像视图上添加按钮。 并给他们约束...
1) 图像视图垂直居中
2) 与您的图片视图高度相同。
3) 与您的图像视图宽度相同。
4) 对你的图像视图给予领先的尊重

3)class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,UIImagePickerControllerDelegate, UINavigationControllerDelegate {

var arrdata = ["Choose Image","Full name", "Home", "Favourite", "Logout"]
var imgVw = UIImageView()
var arrImg = [UIImage(named: "home_bg")]
@IBOutlet var tblVw: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    imgVw.image = UIImage(named: "GalleryIcon")

    tblVw.delegate = self
    tblVw.dataSource = self
    // Do any additional setup after loading the view, typically from a nib.
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return arrdata.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "DemoCell") as! DemoCell
    if indexPath.row == 0 {
        cell.imgvw.image = imgVw.image
        cell.lbl.text = "Choose Image"
    }
    else
    {
        cell.lbl.text = arrdata[indexPath.row]
        cell.imgvw.image = arrImg[0]
    }
    cell.selectionStyle = .none
    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if indexPath.row == 0
    {
        let indexPh = tableView.indexPathForSelectedRow
        let cell = tableView.cellForRow(at: indexPh!) as! DemoCell
        cell.btn.isHidden = false
        cell.btn.addTarget(self, action: #selector(btnChooseImageAction), for: .touchUpInside)
    }
}

@objc private func btnChooseImageAction() {
    let alert = UIAlertController(title: "Choose Image", message: "", preferredStyle: .actionSheet)
    alert.addAction(UIAlertAction(title: "Camera", style: .default, handler: { (camera) in
        self.openCamera()
    }))
    alert.addAction(UIAlertAction(title: "Gallery", style: .default, handler: { (gallery) in
        self.openGallery()
    }))
    alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
    self.present(alert, animated: true, completion: nil)
}

//MARK: -Camera image picker functionality
func openCamera()
{
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.camera)
    {
        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerController.SourceType.camera
        imagePicker.allowsEditing = false
        self.present(imagePicker, animated: true, completion: nil)
    }
    else
    {
        let alert = UIAlertController(title: "Warning", message: "You don't have camera", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
        self.present(alert, animated: true, completion: nil)
    }
}

//MARK: - Gallery image picker functionality:
func openGallery()
{
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.photoLibrary){
        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerController.SourceType.photoLibrary
        self.present(imagePicker, animated: true, completion: nil)
    }
    else{
        let alert = UIAlertController(title: "Warningq", message: "You don't have perission to access gallery.", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
        self.present(alert, animated: true, completion: nil)
    }
}

//MARK:-- ImagePicker delegate
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    if let pickedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
        imgVw.image = pickedImage
        tblVw.reloadData()
    }
    picker.dismiss(animated: true, completion: nil)
}

}