我想实现一个功能,如果在tableviewCell中点击了profileImage,则segue到detailView。我添加tapGesture但我仍然无法弄清楚如何让indexPath传递数据。 我试过这样但应用程序会崩溃。
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "detailSegue" {
let next: DetailViewController = segue.destination as! DetailViewController
// pattern1
let indexPath = tableView.indexPath(for: sender as! CustomTableViewCell)
// pattern2
let indexPath = tableView.indexPathForSelectedRow! as NSIndexPath
next.data = datas[indexPath.row]
}
}
我该如何解决这个问题?提前谢谢!
答案 0 :(得分:3)
如果您将
UITapGestureRecognizer
添加到UIImageView
,请不要 忘记将isUserInteractionEnabled
属性设置为true
。
创建一个变量来存储选定的indexPath.row
var selectedCell:Int!
在cellForRow
方法中,向图像视图添加手势。
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.tapBtnAction(_:)))
cell.imageView.tag = indexPath.row
cell.imageView.addGestureRecognizer(tapGesture)
在目标方法中执行segue
func tapBtnAction(_ sender: UITapGestureRecognizer) {
print("\(sender.view.tag) Tapped")
self.selectedCell = sender.view.tag
self.performSegueWithIdentifier("detailSegue", sender: self)
}
在prepareforsegue
方法中从数组中获取数据。
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "detailSegue" {
let next: DetailViewController = segue.destination as! DetailViewController
next.data = datas[self.selectedCell]
}
}
答案 1 :(得分:3)
解决方案1:使用touchBegun方法,确保启用imageView UserInteraction
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
if let cell = touch?.view?.superview?.superview as? YourCellClass {
let indexPath = yourTablView.indexPath(for: cell)
//Do whatever you want
}
}
解决方案2:您可以使用按钮。
@IBAction func onBtnClick(_ sender : UIButton) {
if let cell = sender.superview?.superview as? UITableViewCell {
let indexPath = tbl_songsInfo.indexPath(for: cell)
//Do whatever you want
}
}
解决方案3:与上述相同,但使用sender.tag
@IBAction func onBtnClick(_ sender : UIButton) {
let data = yourDataArray[sender.tag]
//Do whatever you want
}
答案 2 :(得分:2)
我已经制作了一个示例项目来演示解决方案。它工作得非常好。看到gif!
您可以使用该链接下载示例项目代码 https://drive.google.com/file/d/1zF8Uq2H6eQYyrHU6KqnZu7b5cU_HtZY0/view?usp=sharing
实际上,您必须为图像添加手势识别器并将动作附加到此手势。请参阅cellforRowAt的实现。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier:"TableViewCellUser") as! TableViewCellUser
//handle image and its gesture
cell.imgUser.image = userImagesArray[indexPath.row]
//enable the interaction so that touch could be captured.
cell.imgUser.isUserInteractionEnabled = true
let gesture = UITapGestureRecognizer(target: self, action: #selector(userImageTapped(gesture:)))
gesture.numberOfTouchesRequired = 1
gesture.delegate = self as? UIGestureRecognizerDelegate
cell.imgUser.tag = indexPath.row
cell.imgUser.addGestureRecognizer(gesture)
//handle label
cell.lblUserName.text = userNames[indexPath.row]
return cell
}
//用户点击操作
guard let indexPathRow = gesture.view?.tag else {
return
}
print(indexPathRow)
guard indexPathRow >= 0 else {
print("Array index must be greater than zero. Going to return")
return
}
selectedUserImage = userImagesArray[indexPathRow]
selectedUserName = userNames[indexPathRow]
self.performSegue(withIdentifier: "UsersToDetail", sender: self)
您可以从上面提到的链接下载完整的代码。如果发生任何问题,请在评论中告诉我
此链接也会有所帮助 [UITapGestureRecognizer tag]: unrecognized selector sent to instance
答案 3 :(得分:1)
1:在cellForRowAtIndexpath方法中设置ImageView的标签,标签将等于indexPath.row
int dynamicHeight = 0;
int padding = 10;
for (int i = 0; i <= dt.Columns.Count - 1; i++)
{
foreach (DataColumn column in dt.Columns)
{
CheckBox[] chk = new CheckBox[dt.Columns.Count];
chk[i] = new CheckBox();
chk[i].Name = column.ColumnName;
chk[i].Text = column.ColumnName;
chk[i].AutoCheck = true;
chk[i].Bounds = new Rectangle(10, 20 + padding + dynamicHeight, 40, 22);
panelCol.Controls.Add(chk[i]);
dynamicHeight += 20;
panelCol.Size = new Size(120, dynamicHeight);
panelCol.Controls.Add(chk[i]);
chk[i].Location = new Point(0, dynamicHeight);
chk[i].Size = new Size(120, 21);
panelCol.BackColor = Color.White;
panelCol.AutoScroll = true;
//panelCol.AutoScrollMinSize = new Size (0, 1200);
}
}
2:将目标添加到imageView
上附带的tapGuestureRecognizer3:在该方法中获取imageView的标签
imgView.tag = indexPath.row
4:相应地获取数据,然后按
let imgView = sender as! UIImageView
let tag = imgView.tag
答案 4 :(得分:1)
有多种方法可以实现这一点,但最简单的方法是在UIImageView上方添加UIButton。 并根据UITableViewCell的IndexPath设置UIButton的标记,并添加UIButton的click事件。
cell.btnImage.tag = indexPath.row
当用户尝试单击UIImage时,将单击图像上方的UIButton并触发按钮的单击事件,在这里您可以获得与该特定单元格中的UIImageView相同的UIButton tapped的索引
@IBAction func BtnClick(_ sender : UIButton) {
print(sender.tag)//here you will get the IndexPath for that particular UIImageView in Cell.
}