我已经创建了侧边菜单,如 this 所示。 ..我只希望第一行带有全名标签的 ImageView ,这样如果有人点击第一行的 ImageView ,允许用户从图库中选择图像。此外,所有其他行应该只包含重定向到另一个 View Controller 的文本。请建议任何链接或任何代码段仅在侧面菜单的第一行获取 ImageView ,并带有一个按钮以从图库中选择图像。这是我的代码
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
}
最佳答案
首先,您需要包含图像和文本的自定义单元格。
之后我建议有一个配置列表,例如
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]
关于ios - 如何设置侧边栏中表格 View 单元格的第一行与所有其他行不同 - swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53428918/